Save variables after closing the app

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Spat'
Posts: 2
Joined: Wed Jul 01, 2020 5:56 pm

Save variables after closing the app

Post by Spat' » Wed Jul 01, 2020 5:58 pm

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 ?

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Save variables after closing the app

Post by Klaus » 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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Save variables after closing the app

Post by dunbarx » 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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Save variables after closing the app

Post by jmburnod » Wed Jul 01, 2020 6:21 pm

Hi,
specialfolderpath("documents") works for Mac and Windows too.
Best
Jean-Marc
https://alternatic.ch

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Save variables after closing the app

Post by Klaus » Wed Jul 01, 2020 6:54 pm

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.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Save variables after closing the app

Post by richmond62 » Wed Jul 01, 2020 7:20 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Save variables after closing the app

Post by dunbarx » Wed Jul 01, 2020 7:22 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Save variables after closing the app

Post by FourthWorld » 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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Spat'
Posts: 2
Joined: Wed Jul 01, 2020 5:56 pm

Re: Save variables after closing the app

Post by Spat' » Wed Jul 01, 2020 8:16 pm

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'

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Save variables after closing the app

Post by bogs » 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" ?
Image

SWEdeAndy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 261
Joined: Sat Aug 16, 2008 9:48 am
Location: Stockholm, Sweden
Contact:

Re: Save variables after closing the app

Post by SWEdeAndy » Wed Jul 01, 2020 9:13 pm

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
Andreas Bergendal
Independent app and system developer
WhenInSpace: https://wheninspace.se

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Save variables after closing the app

Post by Klaus » Wed Jul 01, 2020 9:22 pm

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

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Save variables after closing the app

Post by SparkOut » Wed Jul 01, 2020 9:26 pm

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. :wink:
http://lessons.livecode.com/m/4071/l/17 ... pplication

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Save variables after closing the app

Post by Klaus » Wed Jul 01, 2020 9:38 pm

SparkOut wrote:
Wed Jul 01, 2020 9:26 pm
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. :wink:
http://lessons.livecode.com/m/4071/l/17 ... pplication
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")!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Save variables after closing the app

Post by FourthWorld » Wed Jul 01, 2020 9:39 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Talking LiveCode”