Persistence of Script Local Variable value

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
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Persistence of Script Local Variable value

Post by sritcp » Sat Feb 23, 2013 7:52 pm

The User Guide says that a script local variable retains its value even after a handler finishes executing.
How persistent is this retention?
If there is a script local variable sVar on a card script, will it retain its value if we move away from the card (or stack, for that matter) and come back to it?
That is, when does it get deleted or reset to empty?

Thanks,
Sri.

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

Re: Persistence of Script Local Variable value

Post by Klaus » Sat Feb 23, 2013 8:36 pm

I think it will only be "reset" when you quit Livecode.

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

Re: Persistence of Script Local Variable value

Post by dunbarx » Sat Feb 23, 2013 8:38 pm

Hi.

If you explicitly declare a script local variable, this declaration being made outside any particular handler within a script, is accessible to all handlers within a script, at any time. If you "declare" a variable within a handler, by just, say, putting a value into it, it is accessible only within that handler. That is the difference.

In each case the variable is "local", in the sense that only a handler within the script (or only the handler itself, in the case of an ordinary local variable) can access it. In either case, the value is not "retained" after the handler ends. For example, unlike a global variable, you cannot get its value, for example, from the message box. But you can:

Code: Select all

local dd

on mouseUp
   answer dd
end mouseUp

on mouseDown
put random(99) into dd
end mouseDown
In the old days, persistent values had to be stored in either object containers, like fields or buttons, or in globals. The advent of custom properties changed all that.

Craig Newman

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

Re: Persistence of Script Local Variable value

Post by sritcp » Sat Feb 23, 2013 9:07 pm

Hi Craig:

I understand that a script local var is like a local var when it comes to accessing its value (inside all the handlers within the script only vs inside one handler only).

But I was talking about value persistence. The user guide says of the ordinary local variable "Once the handler finishes executing, the variable is deleted."
If the value of the script local var is retained (so it could be accessed after going away to another stack and returning) until LiveCode is quit (as Klaus says), then the script local var is similar to global var with respect to value persistence.
You say
In either case, the value is not "retained" after the handler ends.
which disagrees with Klaus' view.

Here's my doubt:
-- create a script local called sVar in card script
-- a handler createSVar assigns a value to sVar
-- another handler in the same card script called useSVar uses this value.
-- after leaving the card (and stack) and returning to the card, is the value still available for the handler useSVar ? (as a global var would be, but in this case I mean only within the card script).

Regards,
Sri.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Persistence of Script Local Variable value

Post by sturgis » Sat Feb 23, 2013 9:33 pm

If you declare the local inside a handler it clears when the handler exits.
If you declare it as a script local at the top of the script, all handlers can use it as a local and it is persistant. As mentioned above, not when the app is closed of course, that will clear it.

There are also some quirks to this.

Consider the following script

Code: Select all

on mouseUp
   put "Pre init in mouseup:" && test & cr 
   put "fred" into test
   put "Post init in mouseUp:" && test & cr after msg
   testCheck
end mouseUp

local test

command testCheck
   put "Pre init in testcheck:" && test & cr after msg
   put "Joe" into test
   put "Post init in testcheck:" && test & cr after msg
end testCheck
The result of this script the FIRST time the button is clicked is this:
Pre init in mouseup: test -- test hasn't been declared so the engine decides its an unquoted string
Post init in mouseUp: fred -- fred has a value so it is seen as a variable
Pre init in testcheck: -- the variable has been declared as local, but not inited (other than above which is the quirk) so it shows as empty
Post init in testcheck: Joe -- joe is in the variable so it shows here
The second time you click the button..
Pre init in mouseup: test -- since the script local is declared AFTER the handler, it still thinks its empty
Post init in mouseUp: fred
Pre init in testcheck: Joe -- since the script local was declared BEFORE this handler it retains its value
Post init in testcheck: Joe
Slightly freaky huh?

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

Re: Persistence of Script Local Variable value

Post by Klaus » Sat Feb 23, 2013 9:40 pm

Ah, yes, sorry, I had thought you had declared the LOCAL explicitely at the top of the script:
-----------------------
local tVar

on xyz
...
---------------------
Otherwise the docs are correct, it is GONE after the handler has finished. :D

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

Re: Persistence of Script Local Variable value

Post by sritcp » Sat Feb 23, 2013 10:48 pm

I did mean a script local that is declared at the top of the script, but your test is fascinating, sturgis!

So, it means the value is persistent for those handlers that come below the variable declaration statement on the script page and not available to those above!

Thanks,
Sri.

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

Re: Persistence of Script Local Variable value

Post by Klaus » Sun Feb 24, 2013 6:38 pm

Hi Sri,

you can get the values of these local variables from OUTSIDE the script by
addind a function to that script that retunrs the local variable and with
something like this (from the docs under VALUE):
...
answer value("TheFunctionThatReturnsTheLocalVar()", the long id of <your object refence here>)
...
:D


Best

Klaus

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

Re: Persistence of Script Local Variable value

Post by dunbarx » Sun Feb 24, 2013 6:59 pm

Klaus.

That is cool. Sort of a back door into a script, even though it requires a piece of code planted inside that script.

Craig

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

Re: Persistence of Script Local Variable value

Post by sritcp » Sun Feb 24, 2013 7:35 pm

Hi Klaus:

Good trick to know!

Regards,
Sri.

Post Reply