Page 1 of 1

Value persistence of script local variables

Posted: Wed Apr 08, 2015 4:05 am
by sritcp
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.

Re: Value persistence of script local variables

Posted: Wed Apr 08, 2015 4:14 am
by FourthWorld
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.

Re: Value persistence of script local variables

Posted: Wed Apr 08, 2015 1:09 pm
by sritcp
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.

Re: Value persistence of script local variables

Posted: Wed Apr 08, 2015 2:29 pm
by dunbarx
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