How do I save global variable values?

Get help using Construct 2

Post » Thu May 07, 2015 4:36 am

Say the user earns 100 coins and then closes the game, how should the coins value be saved so that he reloads the game. This is for Mobile Games for iOS, Android and WP8.1. The save action inbuilt doesnt work...
Something big is coming...
To the Windows Store, App Store, Play Store and 14 other app marketplaces near you! :)
B
15
S
2
Posts: 62
Reputation: 856

Post » Thu May 07, 2015 5:57 am

B
36
S
10
G
4
Posts: 250
Reputation: 7,963

Post » Thu May 07, 2015 12:46 pm

Hi, I referred to it. But it didnt state anything about global variables. I tried that method. But it doesnt work. I have also included the Eventsheet of globals in the event sheet where the save occurs.

If this doesnt work, then can I use Local Storage to save and retrieve each variable?
Something big is coming...
To the Windows Store, App Store, Play Store and 14 other app marketplaces near you! :)
B
15
S
2
Posts: 62
Reputation: 856

Post » Thu May 07, 2015 1:02 pm

Using local storage is the easiest way and should work something like this:

local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

on start of layout - local storage get Item "Playercoins"

on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Fri May 08, 2015 5:50 pm

akkthedev wrote:Hi, I referred to it. But it didnt state anything about global variables. I tried that method. But it doesnt work. I have also included the Eventsheet of globals in the event sheet where the save occurs.

If this doesnt work, then can I use Local Storage to save and retrieve each variable?


If you want to save just one variable you can do this like the way mentioned above. But if you want to save more than 1 variable I would prefer to use an Array and save it asJSON in the LocalStorage.
I made an example file if you don't know how to handle that.

Local Storage

I'm currently updating all my games using WebStorage to LocalStorage. I will release a LevelSelection working with LocalStorage instead of WebStorage. If you have any question feel free to send me a PM.

Regards,
Andreas
Image Image iTunes: Match3 | Google Play: Match3
B
100
S
35
G
30
Posts: 1,136
Reputation: 28,085

Post » Sat May 09, 2015 8:22 am

Thanks for the file! @AndreasR. Will definitely try to implement it.
Something big is coming...
To the Windows Store, App Store, Play Store and 14 other app marketplaces near you! :)
B
15
S
2
Posts: 62
Reputation: 856

Post » Sat May 09, 2015 9:10 am

akkthedev wrote:Thanks for the file! @AndreasR. Will definitely try to implement it.


You're welcome :) Don't hesitate to send me a PM if you stuck or need help! Glad to help you out then.

Regards
AndreasR
Image Image iTunes: Match3 | Google Play: Match3
B
100
S
35
G
30
Posts: 1,136
Reputation: 28,085

Post » Sat May 09, 2015 10:10 am

LittleStain wrote:Using local storage is the easiest way and should work something like this:

local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

on start of layout - local storage get Item "Playercoins"

on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)


I got this working but still not getting two points .....

As why are we using key to store the same value as in global variable & what is itemvalue used for ?
Image
B
36
S
13
G
1
Posts: 99
Reputation: 3,051

Post » Sat May 09, 2015 10:50 am

miketolsa wrote:
LittleStain wrote:Using local storage is the easiest way and should work something like this:

local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

on start of layout - local storage get Item "Playercoins"

on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)


I got this working but still not getting two points .....

As why are we using key to store the same value as in global variable & what is itemvalue used for ?


You must imagine the LocalStorage as a Table. The key is the ID and the value is the value of your saved variable.

LocalStorage
Image

So you can save each variable individually or you can save all variables in an array and then the array via asJSON into the localstorage.

In order to load the saved item at a certain key you need to run 'Check item exists (your key)' -> If key exists -> Get Item (your key). Then the value of the fetched key can be set to the variable by 'LocalStorage.ItemValue'.
Image Image iTunes: Match3 | Google Play: Match3
B
100
S
35
G
30
Posts: 1,136
Reputation: 28,085

Post » Sat May 09, 2015 11:22 am

LittleStain wrote:Using local storage is the easiest way and should work something like this:

local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

on start of layout - local storage get Item "Playercoins"

on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)


Thanks for quick refrence that helped me a lot .....

so is playercoin a variable or integer or key ? as you want to set the global variable score to it am i right ?

So if possible could you just report the same code using the table reference if possible as it would help me to understand much easily .

also I think this two statement should be called in start of the layout .....

local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

local storage get Item "Playercoins"

so is it necessary to call them randomly or just in a order to get the value of globalvariable to key playercoin ?
Image
B
36
S
13
G
1
Posts: 99
Reputation: 3,051

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 16 guests