Value persistence of script local variables

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Value persistence of script local variables

Post by sritcp » Wed Apr 08, 2015 4:05 am

I was under the impression that script local variables were just like handler-local variables, except that they were available to all handlers within the script.

The handler-local ("temporary") variable doesn't retain its value once the handler is finished. If you run the same handler again, the temp variable is reset (to "empty"). I thought it would be the same with script local variable.

Not so. Not only its value persists after a handler is finished, but even after you leave the card and come back to it (e.g., in case of a card script). I tried out a number of variations.

In this way, the script local variable seems to resemble more the global variable (except for the fact that it is only accessible within a script).

This was a surprise.

Regards,
Sri.

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

Re: Value persistence of script local variables

Post by FourthWorld » Wed Apr 08, 2015 4:14 am

sritcp wrote:In this way, the script local variable seems to resemble more the global variable (except for the fact that it is only accessible within a script).

This was a surprise.
I hope a pleasant one. Script-locals are very powerful in this way, like private globals.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Value persistence of script local variables

Post by sritcp » Wed Apr 08, 2015 1:09 pm

FourthWorld wrote:I hope a pleasant one.
Yes, and a little disconcerting, too.
I found that the value (of the script local) is available to a handler in that script, even if that handler was called (or "sent") from another card.
Thus, a script local value can be accessed from outside that script (essentially, like a global).
I like your "private global" than "script local" as a descriptive.

Thanks,
Sri.

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

Re: Value persistence of script local variables

Post by dunbarx » Wed Apr 08, 2015 2:29 pm

You might even take this a step farther, and think of it as a way to store data in a script. Sort of like a private custom property.

Craig Newman

Post Reply