Resetting variables, cards, etc

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
ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Resetting variables, cards, etc

Post by ColeBKray » Wed Apr 24, 2013 11:14 am

Hi,

How can I reset the cards, variables, etc, to the basic state, as I set them up?

I mean after testing out my app, things don't reset when I get back to edit mode and it's a bit annoying that I have to put back everything by hand.

Thank you.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Resetting variables, cards, etc

Post by jmburnod » Wed Apr 24, 2013 1:08 pm

Hi ColeBKray ,
I think you need a initMyCard handler to use at preopencard
Bestregards
Jean-Marc
https://alternatic.ch

ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Re: Resetting variables, cards, etc

Post by ColeBKray » Wed Apr 24, 2013 1:16 pm

And how do I initalize the card? What code should I Put in?

Can you explain in more detail?

Sorry I'm a complete beginner.

Thank you.
Cole

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Resetting variables, cards, etc

Post by jmburnod » Wed Apr 24, 2013 2:27 pm

Hi Cole,

I understand your cd can be changed by the user and there is a default state of this cd
For exemple, loc of obj if move, field content, state of btn etc....
You can save this default state, store it in a customprop of the cd or in a file
and restore the default state at preopencard.
You can even do what you asked about "initiate a card" in an other thread :D
Best regards
Jean-Marc
https://alternatic.ch

ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Re: Resetting variables, cards, etc

Post by ColeBKray » Wed Apr 24, 2013 4:49 pm

OMG this seems complicated. LiveCode should have something built-in for this, like a menu "Save card state as", and then a command initCardState "CardStateName".

I'm still learning a lot of commands and features of LiveCode, so I'll get back to the documentation on how to save states of objects in a file, and then recall it from there, etc...

Anyway, thanks :)

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Resetting variables, cards, etc

Post by magice » Wed Apr 24, 2013 5:14 pm

Actually it's very simple. The first question you must ask though is will the end user be working within LC or from a standalone. If it is a standalone then all fields will always revert to the state they were in when it was compiled and all variables will clear when the stack is newly opened. Since a standalone can not save itself it is kind of a built in default state. So on openstack you just reinitialize variables that are saved as custom properties, a text file, or a rev stack. Now I am sure that will bring up lots of questions on how to save, but I will let you try to figure that out yourself first. The fun is just beginning.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Resetting variables, cards, etc

Post by jmburnod » Wed Apr 24, 2013 5:33 pm

Hi Cole
OMG this seems complicated.LiveCode should have something built-in for this, like a menu "Save card state as"
Sorry no "Save card state as" menu in LC but you can build it. Magice is right, that is very simple (in LiveCode :D )
There is a lot of ways to do that
The following script use customproperties of controls to reset the cd

Code: Select all

on preopencard
   InitCard
end preopencard

--•• restore default properties of the target controls
on InitCard
   set the rect of image "myimage" to  the uMyrect of image "myImage"
  put  the uContent of field "myField" into field "myField" 
end InitCard

--•• save some properties of the target controls
on SaveStateCard
   put the rect of image "myimage" into tRect
   set the uMyrect of image "myImage" to tRect
   put fld "myField" into tContFld
   set the uContent of fld "myField" to tContFld
end SaveStateCard
Best
Jean-Marc
https://alternatic.ch

ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Re: Resetting variables, cards, etc

Post by ColeBKray » Wed Apr 24, 2013 6:00 pm

Good to hear these, and thanks for the help, really great community here, fast, helpful, and friendly, I like that :)

ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Re: Resetting variables, cards, etc

Post by ColeBKray » Wed Apr 24, 2013 7:59 pm

Hi guys,

I did what you suggested, but I'm running into problems.
I guessed that the preOpenCard is called when I go to the card, so I created a new card with a button "Start" that has 'go to card xy' where xy is the card that actually does the thing I'm working on, put some initializing stuff in the preOpenCard of the xy card, but it seems like nothing is happening.

Here's the code that I've put in the xy card script:

Code: Select all

on preOpenCard
   set the width of templateField to 250
   set the location of button "AddNew" to 200,150
   repeat with i=1 to POVNum                                     --The AddNew button creates fields with name "POV1", "POV2", etc... 
                                                                               --I want to delete these when starting
      put "POV" & i into dField
      delete field dField
   end repeat
   
   put 1 into POVNum
   end preopencard
Actually, nothing from the script really happens. What am I doing wrong?

Thanks :)

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Resetting variables, cards, etc

Post by Simon » Wed Apr 24, 2013 11:21 pm

Hi,
I don't see in your above script.
global POVNum
With what you have there POVNum is empty. (I'm guessing that you have it populated as a global)

Everywhere you use a global you have to state it, so even in a button you need:

Code: Select all

global POVNum 
on mouseUp
put 5 into POVNum 
end mouseUp
The same with your card script and then they can share it.
I guessed that the preOpenCard is called when I go to the card
Yes, you have to take LC very literally (sometimes).

Edit:

Code: Select all

repeat with i = 1 to the number of fields of this cd
      if there is a field ("POV" & i) then delete fld ("POV" & i)
   end repeat
better no global to worry about.


Simon
Last edited by Simon on Wed Apr 24, 2013 11:29 pm, edited 1 time in total.
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Resetting variables, cards, etc

Post by jmburnod » Wed Apr 24, 2013 11:29 pm

Hi Cole,
Actually, nothing from the script really happens. What am I doing wrong?
Nothing is wrong for me.
Are you sure the location of btn "aadNew" changed and field "POV1" exists ?
Best
Jean-Marc
https://alternatic.ch

ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Re: Resetting variables, cards, etc

Post by ColeBKray » Fri Apr 26, 2013 7:38 am

Thank you guys, I made it work.

Post Reply