A code to destroy the value of a variable

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
Lina
Posts: 11
Joined: Fri May 01, 2020 12:52 pm

A code to destroy the value of a variable

Post by Lina » Tue May 12, 2020 9:04 pm

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 :)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: A code to destroy the value of a variable

Post by richmond62 » 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.

Lina
Posts: 11
Joined: Fri May 01, 2020 12:52 pm

Re: A code to destroy the value of a variable

Post by Lina » Tue May 12, 2020 9:17 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: A code to destroy the value of a variable

Post by dunbarx » 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

Lina
Posts: 11
Joined: Fri May 01, 2020 12:52 pm

Re: A code to destroy the value of a variable

Post by Lina » Tue May 12, 2020 10:17 pm

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 ?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: A code to destroy the value of a variable

Post by dunbarx » 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
Last edited by dunbarx on Wed May 13, 2020 1:45 pm, edited 1 time in total.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: A code to destroy the value of a variable

Post by Thierry » Wed May 13, 2020 6:52 am

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: A code to destroy the value of a variable

Post by jacque » Wed May 13, 2020 5:49 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: A code to destroy the value of a variable

Post by FourthWorld » Wed May 13, 2020 6:17 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Lina
Posts: 11
Joined: Fri May 01, 2020 12:52 pm

Re: A code to destroy the value of a variable

Post by Lina » Wed May 13, 2020 8:03 pm

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 :)

Lina
Posts: 11
Joined: Fri May 01, 2020 12:52 pm

Re: A code to destroy the value of a variable

Post by Lina » Wed May 13, 2020 8:10 pm

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:

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: A code to destroy the value of a variable

Post by dunbarx » Thu May 14, 2020 12:03 am

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”