Page 1 of 2
Save variables after closing the app
Posted: Wed Jul 01, 2020 5:58 pm
by Spat'
Hello guy, I'm working on a new map and would to save the player score whitout any network connection.
I don't found any solution for that, can you help me ?
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 6:09 pm
by Klaus
Hi Spat',
welcome to the forum!
You need to write the data to disk when the stack closes and read them in again at next appstart.
Store it in
Mobile:
iOs/Android -> specialfolderpath("documents")
Desktop:
Mac -> specialfolderpath("preferences")
Win -> specialfolderpath("support")
Linux -> specialfolderpath("documents")
Best
Klaus
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 6:18 pm
by dunbarx
Hi.
You can also store the data in a custom property of the stack, which is automatically saved between sessions. This way everything stays inside the stack.
Craig
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 6:21 pm
by jmburnod
Hi,
specialfolderpath("documents") works for Mac and Windows too.
Best
Jean-Marc
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 6:54 pm
by Klaus
dunbarx wrote: ↑Wed Jul 01, 2020 6:18 pm
Hi.
You can also store the data in a custom property of the stack, which is automatically saved between sessions. This way everything stays inside the stack.
Craig
Sure, but not in a standalone.
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 7:20 pm
by richmond62
On my BBC Model B from 1981 everything evaporates between boots as it has
no hard disk and the OS is stored on a ROM chip.
So I store data on a cassette tape in a cassette player attached to the Beeb via a DIMM socket.
The idea of saving data between uses is at least that old.
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 7:22 pm
by dunbarx
Klaus.
We know we cannot save anything in the executable. Are we sure there only a single stack in this app?
I believe all standalones should have another stack file attached to the executable (the "splash" stack"), to allow for easy enhancement, and, er, the ability to save inside the attached stacks.
Craig
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 7:56 pm
by FourthWorld
Yeah, a stack file can be used. But an LSON file is easier to set up (no changes to architecture needed), and much easier to work with if the app ever transitions to multiple data sets
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 8:16 pm
by Spat'
Klaus wrote: ↑Wed Jul 01, 2020 6:09 pm
Hi Spat',
welcome to the forum!
You need to write the data to disk when the stack closes and read them in again at next appstart.
Store it in
Mobile:
iOs/Android -> specialfolderpath("documents")
Desktop:
Mac -> specialfolderpath("preferences")
Win -> specialfolderpath("support")
Linux -> specialfolderpath("documents")
Best
Klaus
Is there any method to execute this code when the player close he's app ?
Thanks for the help ^^
Spat'
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 9:12 pm
by bogs
FourthWorld wrote: ↑Wed Jul 01, 2020 7:56 pm
Yeah, a stack file can be used. But an LSON file is easier to set up (no changes to architecture needed), and much easier to work with if the app ever transitions to multiple data sets
Pardon my ignorance, but what on earth is an " LSON file" ?
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 9:13 pm
by SWEdeAndy
Spat' wrote: ↑Wed Jul 01, 2020 8:16 pm
Is there any method to execute this code when the player close he's app ?
Certainly! In the stack script, put:
Code: Select all
on shutdown
-- Script the saving to local file or stack here
end shutdown
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 9:22 pm
by Klaus
Hi Craig,
dunbarx wrote: ↑Wed Jul 01, 2020 7:22 pm
We know we cannot save anything in the executable.
yes, we do, but maybe Spat' doesn't?
dunbarx wrote: ↑Wed Jul 01, 2020 7:22 pm
I believe all standalones should have another stack file attached to the executable (the "splash" stack"), to allow for easy enhancement, and, er, the ability to save inside the attached stacks.
Yes, but that also requires to copy that namely stack to a place where we have write permission before we can open and save it. The application folder is usually taboo for these kind of things. So depending on the case, saving a little text file with some data in it may be an easier solution, especially for a newbie.
Best
Klaus
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 9:26 pm
by SparkOut
Here is a lesson on how to do this, remember this is just one approach and there are plenty of cats running round with no fur.
http://lessons.livecode.com/m/4071/l/17 ... pplication
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 9:38 pm
by Klaus
Yes, but saving will fail miserably if the resulting standalone is installed into the platforms application folder:
Mac -> Applications
Win -> Programm files...
That lesson, like a lot more, needs to be updated to make sense and to reflect the value of the "new" specialfolderpath("resources")!
Re: Save variables after closing the app
Posted: Wed Jul 01, 2020 9:39 pm
by FourthWorld
bogs wrote: ↑Wed Jul 01, 2020 9:12 pm
FourthWorld wrote: ↑Wed Jul 01, 2020 7:56 pm
Yeah, a stack file can be used. But an LSON file is easier to set up (no changes to architecture needed), and much easier to work with if the app ever transitions to multiple data sets
Pardon my ignorance, but what on earth is an " LSON file" ?
Sorry, I use the shorthand term so often I sometimes forget not everyone does.
LSON is an informal shorthand for encoded arrays. I've seen a growing number of others use it, but it has not yet found its way into the mother ship's documentation.
LSON is to LiveCode what JSON is to JavaScript and BSON is to MongoDB, a with-the-grain way to serialize non-serial data like arrays in a robust, efficient way highly optimized for the language.
Given how useful arrays are at runtime, storing and retrieving them has become my go-to format for most things, considering alternatives only when there's some reason LSON won't do what I need.
In this case, I don't know the details of the data so it may be even simpler to just use delimited text, with split/combine.
But since custom props were mentioned, and custom props can be expressed very well as arrays (they pretty much are arrays, with their own serialization bound to the object record on save), using an object-independent array seemed at least worth considering.