Script variable not being reported correctly

Report bugs. Post bug workarounds or fixes

Moderator: MSandro

Post Reply
SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Script variable not being reported correctly

Post by SimpleSi » Aug 4th, '17, 15:47

I'm was expecting the say to produce 0 not 1
0.png

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Script variable not being reported correctly

Post by JohnM » Aug 4th, '17, 16:00

You're right, this is a bug; your example should produce 0.

Did you happen to have a shared or instance variable with the same name as the local variable? It looks as though there is a cache that isn't getting cleared.

I couldn't reproduce this starting with a clean project, but I was able to create a similar problem by making a shared variable named 'var3' and then renaming the variable in the "let" block to 'var3'. Do you have a way to recreate the problem from an empty project?

In my case, switching to a different class and back seems to fix the problem (it clears the cache). Does that work for you?

Thanks for reporting this!

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: Script variable not being reported correctly

Post by SimpleSi » Aug 4th, '17, 16:22

I hadn't created any other variables - I was checking to see if Script Variables were just local to the script they were in or in scope across the whole instance

I see if I can re-create sequence

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: Script variable not being reported correctly

Post by SimpleSi » Aug 4th, '17, 16:35

This was my test script - running gave no says at all (so I assumed var and var2 were not in scope when I used say
0.png
So I then re-arranged to this (just to make sure the vars were actually being set) and just got the 0 reported as in orig bug report


0.png
Attachments
1.png

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Script variable not being reported correctly

Post by JohnM » Aug 4th, '17, 17:31

Got it! Following your sequence I was able to reproduce the problem and am on the track of a fix.

Glad you found this -- it's a very confusing bug.

Incidentally, you're correct that the variables were "out of scope" in your first test. The scope of a script variable is just the script in which it appears. Because of that, it's fine for multiple scripts to have script variables with the same name; they don't conflict with each other. In your first example, that's exactly what was happening: the two scripts with "say" blocks actually had their own, uninitialized script variables named 'var' and 'var2'. An uninitialized variable has the value 'nil' and "say nil" has the same behavior as "say nothing".

Script variables are extremely useful; I often use them to store intermediate results, with descriptive names to make the code easier to understand. However, the fact that their scope is limited to a single script, that they only exist when the script is running, and that in cases of multiple calls or recursion, every invocation of a script has its own copy of the script variables, makes script variables less easily "discoverable" than either shared or instance variables. That's why they are not available except in developer mode.

One way to understand script variables is to add a "halt" command to the script in which they appear. All the script variable names will appear in the bottom-left pane of the debugger that pops up. Selecting one of them will show it's value in the bottom-right pane.

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: Script variable not being reported correctly

Post by SimpleSi » Aug 4th, '17, 21:44

That's why they are not available except in developer mode.
That explains why I only noticed them today!

I've only been working in developer mode for a week and hadn't scrolled down that far in the variable category until today

Post Reply