Page 2 of 2

Re: keeping score

Posted: Thu Feb 25, 2016 3:00 pm
by Mikey
Ethan,
As you would expect, scope in LC is always confined to the lowest level, i.e. a handler (on mouseUp, on openCard, etc.), unless you specifically override it using either the local or global keywords. local variables can, at most, have scope within a script (remember, a script is represented by a tab in the Script Editor (a button, field, card, etc.)). global variables can have scope anywhere, but they have to be declared in each script or in each handler or they will not be known to that script or handler.

In any script, there are two ways to declare scope:
a) For the entire script: In the example below, the score variable has scope in the entire script, because it is declared outside of any handler.

Code: Select all

global score #putting it outside of a handler gives it scope in the entire script

on mouseup
...
end mouseUp


on mouseDown
...
end mouseDown
b) For a particular handler in a script. In the example below, the score variable only has scope in the mouseUp handler, but not in the mouseDown handler.

Code: Select all

on mouseup
global score #putting it in the mouseUp handler means it only has scope in the mouseUp handler
...
end mouseUp


on mouseDown
...
end mouseDown
local is not appropriate in your case, because local variables can only have scope within handlers in a script. In your case, if you want to have score be initialized in the openCard handler for the card, and then modified in the on mouseUp handler of a button, then declare it as global in both places.

Remember, scope is local to a handler by default. No matter what you do, the only way to access globals in a script is to declare them. A script is a tab in the Script Editor.

Re: keeping score

Posted: Fri Feb 26, 2016 3:40 am
by ethanCodes
so are variables automatically initialized to 0 then in LiveCode?

Re: keeping score

Posted: Fri Feb 26, 2016 3:50 am
by Simon
Nope, Empty
Throw in a breakpoint so you can see the variables changing. You have to be in the Variable tab of the Script Editor and you'll have to scroll all the way down to the bottom to see your variables but as you step through you'll see them change.

Simon

Re: keeping score

Posted: Fri Feb 26, 2016 8:11 am
by SparkOut
Just an observation about the variables tab in the debugger: although non-initalised variables are empty, the list shows the contents as if being the variable name until such point in the script will affect the value

Re: keeping score

Posted: Fri Feb 26, 2016 3:31 pm
by Mikey
I don't think that's exactly correct. I think that variables that are declared are instantiated empty (unless you are declaring them as a constant). Variables that are referenced before being assigned a value are treated as strings that you forgot to quote. This behavior is a fossil from a bygone era (the 1980's) when HyperCard, LC's great-grandfather, behaved that way.

Code: Select all

global a #will be empty

on mouseUp
   put b into c #c will get the value "b", because as far as LC knows you mean the string 'b', since the container 'b' doesn't hold anything
   put 1 into b #b will get the value 1
   put b into c #c will get the value 1
end mouseUp