global and local variables?

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

Post Reply
xfrios
Posts: 24
Joined: Wed May 25, 2011 11:54 pm

global and local variables?

Post by xfrios » Mon May 30, 2011 1:38 am

I am a livecode beginner. I have good experience in 'C' so technical explanations are fine. I am finding it difficult to navigate the pdf docs and dictionary to find even simple information about syntax and scope.

I have code something like this:

Code: Select all

on pSet   
   put 6 into termOne
   put 4 into termTwo
end pSet

on pVerify
  put 10 into x
  if x = termOne + termTwo
       then
      put "Match" into field fResult
  end if       
end pVerify
The first problem - the values of variables I create in "Set" are not available in "Verify".
The second problem - syntax for the evaluation "if x = termOne + termTwo" is definitely incorrect

Please point me in the right direction.

Thanks!

yachris
Posts: 20
Joined: Sat May 21, 2011 1:26 am

Re: global and local variables?

Post by yachris » Mon May 30, 2011 2:10 am

Hi xfrios,

Welcome! I'm a newby here myself.

In the userguide.pdf file, section 5.5 "Variables", page 145+, you'll find the variable information. Notably,
  • global var
makes a variable global and
  • local var
makes sure it's local.

Interestingly, if you put these inside a function, that makes their scope just that function. If you put them outside any function, then they're local/global for all the functions defined in that script area. The 'global' at script level is obvious, IMHO, but the 'local' at script level is something like declaring a 'static' variable inside a 'C' file; all the functions in that file see it, but no functions outside that file see it.

xfrios
Posts: 24
Joined: Wed May 25, 2011 11:54 pm

Re: global and local variables?

Post by xfrios » Mon May 30, 2011 4:10 am

Thanks for that, very clear. The LiveCode documentation is so insanely verbose and without any concise summaries like that : ).

I think I'll put some documentation feature requests in.

Gosh, I had an easier time with the MS Win32 API : )

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: global and local variables?

Post by Klaus » Mon May 30, 2011 12:31 pm

Hi xfrios,
xfrios wrote:...Gosh, I had an easier time with the MS Win32 API : )
HA! I strongly doubt this! 8) :D


Best

Klaus

xfrios
Posts: 24
Joined: Wed May 25, 2011 11:54 pm

Re: global and local variables?

Post by xfrios » Mon May 30, 2011 1:15 pm

Well um now you mention it, I did have Petzold's api programming book for bedtime reading - maximum information with minimum verbiage.

So yes, I wasn't entirely relying on the documentation in that case now I think about it ; )

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4164
Joined: Sun Jan 07, 2007 9:12 pm

Re: global and local variables?

Post by bn » Mon May 30, 2011 3:56 pm

Interestingly, if you put these inside a function, that makes their scope just that function. If you put them outside any function, then they're local/global for all the functions defined in that script area. The 'global' at script level is obvious, IMHO, but the 'local' at script level is something like declaring a 'static' variable inside a 'C' file; all the functions in that file see it, but no functions outside that file see it.
to declare a variable inside a handler with local myVar is a way to ensure that it is declared if you use 'Strict Compilation Mode' that you can turn on in the Preferences -> Script Editor. If you don't declare it first and try to use variables on the fly in strict compilation mode the Script Editor will throw an error.

If you do not use the strict compilation mode you don't have to declare a variable inside a handler, it is optional.

If you declare a variable with local sVar outside of a handler the scope of that variable is all handlers inside that script:
This type of variable is referred to as a 'script local variable'. This is different from a local or a global variable.

Code: Select all

local sVar

on something
  put "xyz" into sVar
end something

on somethingElse
  answer sVar 
end somethingElse
as for global variables you have to declare them, but you can declare them inside a handler for use in that handler or outside of all handlers of a script to be accessible for all handlers in that script.

Code: Select all

global gVar

on something
  put "xyz" into gVar
end something

on somethingElse
  answer gVar 
end somethingElse
or

Code: Select all

on something
  global gVar
  put "xyz" into gVar
end something

on somethingElse
  global gVar
  answer gVar 
end somethingElse
Many people here have adapted the habit to prepend the variables with a letter depending on what type of variables they are:
t (like in temp) for local variables
s (like in script local) for script local variables
g (like in global) for global variables

The beauty of script local variables is that they persist. And that their scope is restricted to the script. Global variables also persist but are accessible from every open stack. That can lead to confusion if you're not careful with the naming etc. I prefer to store stuff that can be accessed from every handler in custom properties.

I hope this helps a little to sort this out.

Kind regards

Bernd

xfrios
Posts: 24
Joined: Wed May 25, 2011 11:54 pm

Re: global and local variables?

Post by xfrios » Mon May 30, 2011 11:03 pm

Thank you Bernd, nice to see that confirmed & the naming convention.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4164
Joined: Sun Jan 07, 2007 9:12 pm

Re: global and local variables?

Post by bn » Mon May 30, 2011 11:24 pm

Hi xfrios,

there is one thing you have to watch out for if you use script local variables.

I always declare them at the top of the script -> no problem.

If you declare them outside of a handler just above a handler that is say the fifth handler of 10 in a script only the handlers below the declaration of the script local variable will have access to the script local variable, not the ones above

Code: Select all

on putSomething
   put sVar
end putSomething

local sVar
on loadVar
   put "xyz" into sVar
end loadVar

on putSomethingElse
   put sVar
end putSomethingElse
you can see this if you put this script inside a card script. Then make three buttons. The first one with loadVar, to fill the script local variable, the second one with putSomething and a third one with putSomethingElse
only the one with putSomethingElse will put xyz into the message box, the one with putSoemething will put sVar into the message box since it does not "see" the script local variable.

As I said if you declare the script locals above all handlers in a script then all handlers see the script local

Kind regards

Bernd

xfrios
Posts: 24
Joined: Wed May 25, 2011 11:54 pm

Re: global and local variables?

Post by xfrios » Mon May 30, 2011 11:40 pm

Excellent, thank you!

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: global and local variables?

Post by Mag » Wed Apr 09, 2014 11:26 am

Excellent explanation Bernd!

MarcVanCauwenberghe
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 142
Joined: Thu Feb 21, 2013 8:47 am

Re: global and local variables?

Post by MarcVanCauwenberghe » Sat Apr 26, 2014 3:36 pm

Very nice explanation Brend thank you. Still have trouble with those scopes.

To add a global is not global to the whole program but can be if you add it to every script? Am I saying that correct?

Marc

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10306
Joined: Wed May 06, 2009 2:28 pm

Re: global and local variables?

Post by dunbarx » Sat Apr 26, 2014 4:33 pm

Correct.

Globals must be declared in every script. There is no stack-wide or session-wide declaration. In fact, one might say that a custom property is a global that is session-wide. Do you know about these? I use custom properties far more than globals. We could live without globals; we cannot live without custom properties.

Know also that script local variables may be declared in a script, and that, like globals, must be declared ABOVE the handlers that reference them. This is why it is usual to see these gadgets declared at the top of the script, so all handlers "see" them.

Craig Newman

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10044
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: global and local variables?

Post by FourthWorld » Sat Apr 26, 2014 4:35 pm

xfrios wrote:Thanks for that, very clear. The LiveCode documentation is so insanely verbose and without any concise summaries like that : ).
For verbosity, see the User Guide. For conciseness, see the Dictionary.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply