Initializing a variable only once?

Is there a way to initialize a variable only once and then not initialize it anymore on app startup and close?

Ex. I need the below code to only be called once throughout the apps lifespan is there a way to do this without resetting the variable to “0” every time the scene is called?

[lua]

local score = 0

[/lua]

I have a plugin that i’m releasing next week that will help with doing this. 

But this is how i’d do it.

Have a file that reads “0” So now in your code you do this.

if file == 0 then score = 0 file = 1 elseif file == 1 then --Do nothing end

Now when the game ends you can save the file number that was set to 1 so now every time your if statement fires it checks “file” and now since it reads “1” so it just doesn’t do anything (Doesn’t make score equal 0 anymore.).

–SonicX278 

Sonic’s answer is very different than the idea that I immediately had upon reading this post. Just to be thorough, though, here is what I immediately thought of. I am not sure if this what you are looking for. …

score = score or 0

Notice that this is setting a globally scoped variable since it is not scoped as local.

If you need to modify “score” and save/load its value on each startup then you will need to write the value to disk.  See here

Yes @JonPM. That’s what I was getting at.

I wrote a plugin that lets you read, write in just 2-3 lines of code. Not read for release yet though.

–SonicX278

There is also GGDatawhich does the same thing.

Ahh I see. Mine seems a lot easier though. Well it was just a “for me” plugin but I thought I’d share it.

I used Sonic’s method and it worked really a pretty ingenious idea to test conditions against a files saved data did not think of that at all. Oh and looking forward to that new plugin :smiley:

Awesome! Glad to help!

I have a plugin that i’m releasing next week that will help with doing this. 

But this is how i’d do it.

Have a file that reads “0” So now in your code you do this.

if file == 0 then score = 0 file = 1 elseif file == 1 then --Do nothing end

Now when the game ends you can save the file number that was set to 1 so now every time your if statement fires it checks “file” and now since it reads “1” so it just doesn’t do anything (Doesn’t make score equal 0 anymore.).

–SonicX278 

Sonic’s answer is very different than the idea that I immediately had upon reading this post. Just to be thorough, though, here is what I immediately thought of. I am not sure if this what you are looking for. …

score = score or 0

Notice that this is setting a globally scoped variable since it is not scoped as local.

If you need to modify “score” and save/load its value on each startup then you will need to write the value to disk.  See here

Yes @JonPM. That’s what I was getting at.

I wrote a plugin that lets you read, write in just 2-3 lines of code. Not read for release yet though.

–SonicX278

There is also GGDatawhich does the same thing.

Ahh I see. Mine seems a lot easier though. Well it was just a “for me” plugin but I thought I’d share it.

I used Sonic’s method and it worked really a pretty ingenious idea to test conditions against a files saved data did not think of that at all. Oh and looking forward to that new plugin :smiley:

Awesome! Glad to help!