Page 1 of 1

A code to destroy the value of a variable

Posted: Tue May 12, 2020 9:04 pm
by Lina
Hi everyone

I have a question , I noticed that when I use a variable to count the score of clicks of different objects . The value of the score that uses variable whether it was a global or a local variable maintain its value of the score that has reached and continues to count from that point even when I close and re-open the app and I reset the variable value to 0 with code (put 0 into _gScorePlayer ) for example when a user reaches score 15 and closes the app , next time the score continues from 15 and so on

I am a beginner in livecode
Thanks fro your continues help and support guys :)

Re: A code to destroy the value of a variable

Posted: Tue May 12, 2020 9:14 pm
by richmond62
A variable should clear its value when you QUIT LiveCode; if you only CLOSE the stack
the variable may remain in the computer's memory: and, if you REOPEN the stack
before QUITTING LiveCode what you describe may happen.

Re: A code to destroy the value of a variable

Posted: Tue May 12, 2020 9:17 pm
by Lina
richmond62 wrote:
Tue May 12, 2020 9:14 pm
A variable should clear its value when you QUIT LiveCode; if you only CLOSE the stack
the variable may remain in the computer's memory: and, if you REOPEN the stack
before QUITTING LiveCode what you describe may happen.
Ok thank you
I will make sure that I close/exit the entire livecode program then re-open my app again

Re: A code to destroy the value of a variable

Posted: Tue May 12, 2020 9:40 pm
by dunbarx
Hi.

Read about the "closeStack" message, which you can use here. That message is sent regardless of whether you close the stack or quit LiveCode entirely. Create a handler that resets the counter (pseudo):

Code: Select all

on closeStack
   put 0 into yourCounterVariable
   save this stack
end closeStack
Craig

Re: A code to destroy the value of a variable

Posted: Tue May 12, 2020 10:17 pm
by Lina
dunbarx wrote:
Tue May 12, 2020 9:40 pm
Hi.

Read about the "closeStack" message, which you can use here. That message is sent regardless of whether you close the stack or quit LiveCode entirely. Create a handler that resets the counter (pseudo):

Code: Select all

on closeStack
   put 0 into yourCounterVariable
   save this stack
end closeStack
Craig
Oh thank you will do this too
but where do I write this code , in the script of what ?

Re: A code to destroy the value of a variable

Posted: Wed May 13, 2020 2:31 am
by dunbarx
Hi.

The stack script is the place for such things. But here is homework for you.

Would it also work in the card script? What if your stack had more than one card? Why do I ask these sorts of things?

Craig

Re: A code to destroy the value of a variable

Posted: Wed May 13, 2020 6:52 am
by Thierry
Hi Linda,

Not sure to understand, but in case what you called your app
is a stack you're executing in the IDE, then you have nothing to do except
uncheck "Variable Preservation" in the IDE Preferences:

LC Prefs.png

HTH,

Thierry

Re: A code to destroy the value of a variable

Posted: Wed May 13, 2020 5:49 pm
by jacque
Another way is to set the destroystack of the stack to true. You can do that in the property inspector. Then when you close the stack, everything is removed from memory including all variable values (except global variables, which is why I don't use them.) You don't need any code with this method.

Re: A code to destroy the value of a variable

Posted: Wed May 13, 2020 6:17 pm
by FourthWorld
You can delete a variable with the "delete variable" command, e.g.:

Code: Select all

delete variable tMyVarName
https://livecode.com/resources/api/#liv ... e_variable

Re: A code to destroy the value of a variable

Posted: Wed May 13, 2020 8:03 pm
by Lina
dunbarx wrote:
Wed May 13, 2020 2:31 am
Hi.

The stack script is the place for such things. But here is homework for you.

Would it also work in the card script? What if your stack had more than one card? Why do I ask these sorts of things?

Craig
Thank you for your help

I think with my very little experience , it would work but not if I had more than just one card as you mentioned
And you are telling me this you to help me think more about it , maybe :)

Re: A code to destroy the value of a variable

Posted: Wed May 13, 2020 8:10 pm
by Lina
Thank you so much guys for your help
really appreciate it
Wow you all answered different solutions you are really legends in livecode :) :D :D :mrgreen:

Re: A code to destroy the value of a variable

Posted: Thu May 14, 2020 12:03 am
by dunbarx
Lina.
I think with my very little experience , it would work but not if I had more than just one card as you mentioned
And you are telling me this you to help me think more about it , maybe
I was wondering if you were the type of person who would make a test stack with a few small handlers and try it. :wink:

I find doing such things indispensable, whether for small gadgets like the one you brought up, or tests within a large project to prove or disprove one idea or another. I make these sorts of things all day. I do it for fun.

Craig