keeping score

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: keeping score

Post by Mikey » Thu Feb 25, 2016 3:00 pm

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.

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: keeping score

Post by ethanCodes » Fri Feb 26, 2016 3:40 am

so are variables automatically initialized to 0 then in LiveCode?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: keeping score

Post by Simon » Fri Feb 26, 2016 3:50 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: keeping score

Post by SparkOut » Fri Feb 26, 2016 8:11 am

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

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: keeping score

Post by Mikey » Fri Feb 26, 2016 3:31 pm

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

Post Reply