Question about states.

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

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

Re: Question about states.

Post by Simon » Sat Aug 17, 2013 8:21 am

Try this:

Code: Select all

on openCard
   put "thisDestination" into tDestination
   put "thisValue" into tValue
   put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
   if there is no stack tPrefsFile then
      savePref tDestination,tValue
   else
      go invisible stack tPrefsFile
      answer the thisDestination of stack "myPrefs"
      delete stack "myPrefs"
   end if
end openCard

on savePref pPref,pValue
put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
create invisible stack "myPrefs"
set the filename of it to tPrefsFile
save stack "myPrefs"
set the pPref of stack "myPrefs" to pValue
end savePref
From that you should be able to figure out if something else in the splash stack is causing problems.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Question about states.

Post by Simon » Sat Aug 17, 2013 8:24 am

slight error:
set the filename of it to tPrefsFile
save stack "myPrefs"
set the pPref of stack "myPrefs" to pValue

should be:
set the filename of it to tPrefsFile
set the pPref of stack "myPrefs" to pValue
save stack "myPrefs"

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Question about states.

Post by Opaquer » Sat Aug 17, 2013 8:40 am

Hmm.

So, I put your code in, and it now goes off the splash screen to the main screen. The only issue is that it's still not quite getting the right variable for some reason :(. It's still coming up with a blank bit for the answer line :(

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

Re: Question about states.

Post by Simon » Sat Aug 17, 2013 8:44 am

Ackk, I'm too sloppy:

Code: Select all

on openCard
   put "thisDestination" into tDestination --just for testing
   put "thisValue" into tValue --just for testing
   put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
   if there is no stack tPrefsFile then
      --menuPick stuff here
      savePref tDestination,tValue
      --more values
      savePref tOtherDestination,tOtherValue -- if it's more then a US state that you need to record
      delete stack "myPrefs" --only removes it from memory
   else
      go invisible stack tPrefsFile
      answer the thisDestination of stack "myPrefs"
      put the thisDestination of stack "myPrefs" into tState
      put the thisOtherDestination of stack "myPrefs" into tOtherVar-- if it's more then one value that you need to record
      delete stack "myPrefs" --only removes it from memory
   end if
end openCard

on savePref pPref,pValue
   put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
   create invisible stack "myPrefs"
   set the filename of it to tPrefsFile
   set the pPref of stack "myPrefs" to pValue
   save stack "myPrefs"
end savePref
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Question about states.

Post by Simon » Sat Aug 17, 2013 8:47 am

make sure you delete the myPrefs.lc stack using windows explorer.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Question about states.

Post by Opaquer » Sat Aug 17, 2013 8:52 am

Hmm. Still not working for some odd reason D:. Here's the code for my splashscreen card

global state

on opencard
wait 2 seconds
put "thisDestination" into tDestination
put "thisValue" into tValue
put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
if there is no stack tPrefsFile then
{mobilepick stuff}
savePref(defaultstate,state)
delete stack "myPrefs"
else
go invisible stack tPrefsFile
answer the thisDestination of stack "myPrefs"
put the thisDestination of stack "myPrefs" into State
delete stack "myPrefs"
end if
go to card "Home"
end opencard

on savePref pPref,pValue
put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
create invisible stack "myPrefs"
set the filename of it to tPrefsFile
set the pPref of stack "myPrefs" to pValue
save stack "myPrefs"
end savePref

Sorry for no code tags, I can't seem to use them for some reason :(. And I'm uninstalling the app and reinstalling it on my phone because of mobilepick. Windows doesn't like that, so I can no longer test the splashscreen out on my computer :P

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

Re: Question about states.

Post by Simon » Sat Aug 17, 2013 9:06 am

nope nope.

Try just the last code I posted so you can see what is going on. Run it from the desktop.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Question about states.

Post by Opaquer » Sat Aug 17, 2013 9:31 am

OK, so the first time it doesn't do anything, as expected because there's no answer command for it to do. But when I run it again, it asks if I want to purge or save the stack, as there's already a stack called myPrefs (I think that's my bad for not deleting it in Windows Explorer first :P). And then it has a messagebox with thisValue, which is what you put in. So, it worked? Then why would it be different with the code I had?

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

Re: Question about states.

Post by Simon » Sat Aug 17, 2013 9:49 am

I don't know what is in defaultstate...
note:
put "thisDestination" into tDestination
and
answer the thisDestination of stack "myPrefs"

Do you see what is happening there?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Question about states.

Post by Opaquer » Sat Aug 17, 2013 11:02 am

Sorry, was having dinner!

But I think so... So: savePref tDestination,tValue saves tdestination (which in our example is thisdestination), and saves the value of tvalue (which, here is thisvalue).

Then later when you're doing answer the thisDestination of stack "myPrefs", it's getting the value of thisdestination from myprefs, thereby giving a messagebox with the "thisvalue"

So, if I want my states to be saved. I have

menupick(blarg)
if result=1 then
state="state1"
else if result=2 then
state="state2"
end if

And so on. Then I want to have

savePref defaultstate,state

Thus saving the state to a variable called default state.

Then to call it later, I should use:

put the defaultstate of stack "myPrefs" into state

Which would let me get the value of defaultstate and put it into my variable state? Or am I missing something?

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

Re: Question about states.

Post by Simon » Sat Aug 17, 2013 11:26 am

Rats! It's clear I'm doing poorly at instructions.

put "myState" into defaultState

answer the myState of stack "myPrefs"

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Question about states.

Post by Opaquer » Sat Aug 17, 2013 1:06 pm

Ahh! I get it I think! I've edited the code to get it working too :D!

So, let me make sure I got it working and didn't just luck out with the code :P.

put "defaultstate" into tDestination

Means that when you go saveprefs tdestination,tvalue it saves it as "defaultstate". Then when you have a value, which in my case is state, when I go saveprefs tdestination,state it saves the value of state into tdestination, which is "defaultstate".

Then later, when I want to call it back, it puts the tdestination of stack "myprefs" into a variable, which I've just happened to use state for as well.

I think it all makes sense now :D!

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Question about states.

Post by Opaquer » Sat Aug 17, 2013 1:56 pm

I might have lost it :(. So, on top of adding the state of the user, I also want to have a section for leaders, that once they log in, they can see more functions, leader meetings etc etc. I got the login system working fine, the only issue is making sure the app remembers it :P. It's probably something small, but I can't quite see what's wrong :(. I have a login button with this code:

on mouseUp
ask password "If you are a leader, you can log in here." with "" titled "Leader login"
put it into loginans
put "leaderstate" into leaderstate
put url {dropbox link} into leaderpassword
if loginans=leaderpassword then
answer "Thank you for logging in!"
put true into leader
{appropriate dropbox event text files}
savePref leaderstate,true
delete stack "myPrefs"
else
answer "Sorry, that's not the password!"
end if
end mouseUp

on savePref pPref,pValue
put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
create invisible stack "myPrefs"
set the filename of it to tPrefsFile
set the pPref of stack "myPrefs" to pValue
save stack "myPrefs"
end savePref

It all works perfectly... Until I open the app again next time. Once I do, it seems to have forgotten the default state from before :(. If I then choose a new state, it forgets the leader status :(. I've even got this on my splashscreen:

go invisible stack tPrefsFile
put the defaultstate of stack "myPrefs" into state
put the leaderstate of stack "myprefs" into leader
delete stack "myPrefs"

It works until it has to remember the default state and whether the user is a leader or not :(. And here I was thinking I got the whole savepref thing :(

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Question about states.

Post by jacque » Sat Aug 17, 2013 6:33 pm

You don't need to "go" to a stack to set or retrieve its values, provided you use the full path to the stack on disk. You can just request them directly and the engine will find it and open it invisibly. So you can use this:

Code: Select all

put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
if there is a stack tPrefsFile then put the pPrefs of stack tPrefsFile into tValue
If I am going to be reading and writing to the prefs file throughout the session, I usually just leave it open. If it's a one-time thing, I get the pref and delete the prefs stack (which closes it and removes it from memory.) These handlers leave it open:

Code: Select all

on savePref pPref,pValue
  put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
  if there is no stack tPrefsFile then
    create invisible stack "myPrefs"
    set the filename of it to tPrefsFile
  end if
  set the pPref of stack "myPrefs" to pValue
  save stack "myPrefs"
end savePref

function getPref pPref
  put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
  if there is no stack tPrefsFile then
    return empty
  end if
  return the pPref of stack tPrefsFile
end getPref
In the script that needs to retrieve stored values:

Code: Select all

put getPref("leaderpassword") into tPass
put getPref("state") into tState
Use whatever names you're calling those custom properties. If you have them both in the same property, of course, you won't have to call the getPref function twice.

My inclination would be to treat an empty returned value the same way you are treating "defaultstate", but you could store "defaultstate" as you're doing now and change the function to return "defaultstate" instead of empty if there is no value assigned yet.

Unless there is more to the script than was posted, the retrieved values are lost because the handler isn't returning them or storing them in a place where other handlers can read them. Variables that exist only in one handler are available only to that handler and are deleted when the handler terminates. I'm not sure where you're storing those those so maybe this isn't a problem.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Question about states.

Post by Simon » Sat Aug 17, 2013 9:02 pm

Cool:
put getPref("leaderpassword") into tPass
put getPref("state") into tState
That's what I was looking for, knew it had to be simpler.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply