Page 1 of 1

Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 11:14 am
by ColeBKray
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.

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 1:08 pm
by jmburnod
Hi ColeBKray ,
I think you need a initMyCard handler to use at preopencard
Bestregards
Jean-Marc

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 1:16 pm
by ColeBKray
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

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 2:27 pm
by jmburnod
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

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 4:49 pm
by ColeBKray
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 :)

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 5:14 pm
by magice
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.

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 5:33 pm
by jmburnod
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

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 6:00 pm
by ColeBKray
Good to hear these, and thanks for the help, really great community here, fast, helpful, and friendly, I like that :)

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 7:59 pm
by ColeBKray
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 :)

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 11:21 pm
by Simon
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

Re: Resetting variables, cards, etc

Posted: Wed Apr 24, 2013 11:29 pm
by jmburnod
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

Re: Resetting variables, cards, etc

Posted: Fri Apr 26, 2013 7:38 am
by ColeBKray
Thank you guys, I made it work.