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

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

Question about states.

Post by Opaquer » Sat Aug 17, 2013 4:26 am

Hey everyone.

For the app I'm making, it's going to be run by people in a few states. Is there a way to somehow make the app able to ask which state the user comes from the first time it's run, and remember that as the default choice (unless changed) for future times it runs? Since there's 3 main states that it's going to be used in, I'd like to have something pop up (maybe with radio buttons, though something else might be better) asking what state the user is from, and depending on which state they choose, do something else from there.

I've looked into the ask function, but from what I've seen, that's only good for users to type in information, such as their name, or passwords etc.

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 5:10 am

See if you can figure out this thread:
http://forums.runrev.com/phpBB2/viewtop ... =7&t=14621
They call them Preference of Data files (the place were you save info for later use).

If you have trouble with that just say.

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 5:36 am

Hi Simon

Jacque's reply seems quite handy. I do have a question about it though. Since the app will be run on Android (and hopefully iOs, but I've got to look into that more), would I use something like:

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
save stack "myPrefs"
end if
set the pPref of stack "myPrefs" to pValue
end savePref

Or am I missing something? I wasn't quite sure what the "asup" was in Jacque's reply, but after doing some reading, it seems that the documents is available on both Android and iOs, which is handy.

Although, if people do happen to look through their phone documents, it's possible for them to come across the file and, not knowing what it is, delete it, right? Perhaps there's a better place to put it other than documents?

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 5:48 am

put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
Yes, that is the correct location.
As far as I know there isn't the ability to search the applications documents folder. It's not the same as say a "My Documents" folder.

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 5:53 am

Cool! That bit is sorted then! And here I was thinking it would be much harder!

Do you happen to have any useful links or ideas for the pop up radio button thing too :)?

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 6:01 am

mobilePick is good.
There are many other (manual) ways if you don't like that one.

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 6:08 am

Perfect! Coming from VB, all I did was make a dialog and have it show that when it started. Unfortunately, I didn't have the same amount of luck in Livecode when I tried to do something similar!

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

Re: Question about states.

Post by Opaquer » Sat Aug 17, 2013 6:26 am

Ah, I seem to have come across a bit of an issue now that I've been working on it a bit!

The mobilepick is perfect, so that's good! But I'm having a bit of an issue with the saving. I've got the Jacque's code in, and to save it I'm using savePref(defaultstate,state) [where defaultstate is obviously their default state, and state is the answer to the mobilepick they chose]. The issue I just realised I have is I'm not sure how to tell if they've already made a choice. So, if they run the program and chose state xyz, it saves it. But when they run it again, I'm not sure how to check first if they've chosen state xyz :(

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 6:40 am

hmmm... I was just thinking this might happen.
What does:
if there is no stack tPrefsFile then
Do only once?

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 6:46 am

Wouldn't I need to specify where to look for tPrefsFile? So, something like:

put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
if there is no stack tPrefsFile then
{do mobilepick stuff}
savepref
else
{get state}
end if

Only issue, is how do I get the state from tPrefsFile?

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 6:59 am

go invisible stack tPrefsFile
put the pPref of stack "myPrefs" into pValue
close stack tPrefsFile

So much fun learning LC.
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 7:02 am

Ah, so:

put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
if there is no stack tPrefsFile then
{do mobilepick stuff}
savepref
else
go invisible stack tPrefsFile
put the defaultstate of stack "myPrefs" into state
close stack tPrefsFile
end if

That makes so much sense! And yes, this is the reason I love programming! You learn so many new things, and it's so much fun doing stuff like this :D!

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

Re: Question about states.

Post by Opaquer » Sat Aug 17, 2013 7:27 am

Hmm. Slight problem. This is the code I have

put specialFolderPath("documents") & "/MyPrefs.lc" into tPrefsFile
if there is no stack tPrefsFile then
{do mobilepick stuff}
savePref(defaultstate,state)
else
go invisible stack tPrefsFile
put the defaultstate of stack "myPrefs" into state
close stack tPrefsFile
end if

It works the first time, and when it saves, I put answer pPref in the save option, just to see what it spits out. Comes out with something like "defaultstate,VIC" (where VIC is the state I chose)

The issue is when I open it again the next time. It doesn't work. Not only that, but this is in my splash screen card (which works fine otherwise). But for some reason, when I go to open it again, it doesn't even open the card it's meant to :(. So, I've got:

on opencard
wait 2 seconds
{Code from above}
go to card "Home"
end opencard

The first time I try it, it works fine. The second time I try and load it up, it doesn't go to card "Home" :(. I even put a "answer state" in the code after "put the defaultstate of stack "myPrefs" into state" just to see what it spits out, but it just comes up with a blank messagebox :(

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 7:41 am

ooops...
go invisible stack tPrefsFile
put the pPref of stack "myPrefs" into pValue -- it's still this, not defaultstate or state
close stack tPrefsFile

so it would be:
put the pPref of stack "myPrefs" into state

Do you purposefully leave out a local prefix? Some people do, but to make sure I know it's a var I'd use tState and tDefaultState or gState for a global.

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 7:48 am

So in the else statement:

go invisible stack tPrefsFile
put the pPref of stack "myPrefs" into state
close stack tPrefsFile

Because when I do that, it still doesn't work. And when I do answer state, it still comes up blank :(.

And yes, I don't use local prefixes. Not quite sure why. When I started coding in VB, I never used it. I guess I just kept not using local prefixes :P

Post Reply