Local Storage: use get and immediately set and goes in loop

Get help using Construct 2

Post » Wed Apr 13, 2016 9:47 am

I get the value Local Storage "Total_coins" and i put them into the 'Get_Total_Coins' Variable. I Add 'Get_Total_Coins' Variable value to 'Coins' Variable (toal coins get a the end of game). Finally i put 'Get_Total_Coins' variable value in the Local Storage "Total_coins". When i insert this variable the value enter into loop (add 'Get_Total_Coins' + 'Coins' infinity)

Example

Global number Coins = 100
Global number Get_Total_Coins = 0

LocalStorage --> Check Item "Total_Coins" Exist
LocalStorage --> On Item "Total_Coins" Exists
LocalStorage --> Get Item "Total_Coins"
LocalStorage --> Set Get_Total_Coins to LocalStorage.ItemValue

Set Get_Total_Coins to Get_Total_Coins + Coins

LocalStorage --> Set Item "Total_Coins" to Get_Total_Coins


Image
B
3
Posts: 5
Reputation: 185

Post » Wed Apr 13, 2016 11:37 am

You cant do it this way. (to my knowledge)

It takes time to load something from the local storage. A trigger will be set if the loading is done.
You can use the loaded data after the trigger is set, not during.

Also. The trigger 'On exist' you use to make a decisson in two cases.
During 'setting' to decide if a new key is needed.
During 'getting' to decide if data can be used or if you have to make the data another way.

Example: For 'Best_kmh' you check if it 'exists', and then there is no decission in the case that it dont exist. Meaning: the use of 'on exist' is in that case useless. Can as well just 'get' unconditional.

So, how do you 'get' data ?

First you the use action 'Get item'. It is fine to do that in a 'on exist' event. As long as you also cover the 'non exist' case. But its not necessary to use the 'on exits' event. Often u know it is there. Or, you can use 1 key in localstorage that sets a "local storage is previous used' state and then savely conclude that all the other data will be there, because you always store all the data in one pack.

So 'on exist' is NOT used to know if a value in the localstorage has been written, its used to see if 'the key' is in the localstorage. And that is al it does.

So: First you the use action 'Get item'.
Then you have to wait till that item is loaded.
So next you make an 'On item get' trigger. (or an 'On all gets complete' to do them all at once).
When the item is loaded, the trigger will go off an execute the actions. Actions will be things like: set variable to retreived data.

So in youre case, i would do it this way (the whole story again):

1/ Make a key in the local storage 'SavedState'. Give it the value '1'
Make all the other keys you need, give them the basevalue.

Now you know for sure that all keys are in the localstorage. And you can check this in a 'on exist' event. Preferable in the 'on start layout' section. If it dont exist then just make the needed keys.

2/ Make a condition to decide when to 'set' in the local storage. (like every 10 seconds, or anything you want)
Then do all the unconditional sets in actions. Be sure the 'gets' and 'sets' dont happen at the same time. So you need at least a timer, preferable a system that knows if the 'sets' are done.

2/ Make a condition to decide when to 'get' from the local storage.
Then do all the unconditional gets in actions.

3/ Make a 'On item get' condition.
Then assign the values in actions under that condition.

4/ Special case.
So you want to add the current earned coins to the previous earned coins (those stored in the local storage), and store the sum.

Then you need an get action for the previous earned coins, under whatever condition.
You need a ''On item get' condition, to wait for the loading to complete. And under that condition you add them up. And you set the new value in the locale storage.

Be aware that also writing in the localstorage takes time. So you need a 'On item set' trigger to be sure that there will be no 'get' happening during that time. You can (for example) make an action under the 'On item set' condition that assigns a value to a variable that will be checked in the condtion that handles the 'set' in the localstorage.

Hope i made some things clear.
B
33
S
18
G
27
Posts: 2,436
Reputation: 20,336

Post » Wed Apr 13, 2016 1:00 pm

Tnks for a reply!

But this is my solution:

In this moment i have 2 Layouts. First Layout with "Start" Button and the second Layout with game engine.


Layout Home

LocalStorage --> Check Item "Total_Coins" Exist

LocalStorage --> On Item "Total_Coins" Missing
Dictionary --> Add Key Get_Total_Coins with value 0
LocalStorage --> Set Item "Total_Coins" to 0

LocalStorage --> On Item "Total_Coins" Exists
LocalStorage --> Get Item "Total_Coins"
Dictionary --> Add Key Get_Total_Coins with valueLocalStorage.ItemValue



Layout Game

Global number Coins = 100
Global number Get_Total_Coins = 0



LocalStorage --> Check Item "Total_Coins" Exist

LocalStorage --> On Item "Total_Coins" Exists
Set Get_Total_Coins to Dictionary("Total_Coins")
Set Get_Total_Coins to Get_Total_Coins + Coins
LocalStorage --> Set Item "Total_Coins" to Get_Total_Coins
B
3
Posts: 5
Reputation: 185

Post » Wed Apr 13, 2016 1:07 pm

I Study your solution and i to try use in my project. Tnks.
B
3
Posts: 5
Reputation: 185

Post » Wed Apr 13, 2016 1:38 pm

Where is the ''On item get' in there ?
B
33
S
18
G
27
Posts: 2,436
Reputation: 20,336

Post » Wed Apr 13, 2016 3:14 pm

99Instances2Go wrote:Where is the ''On item get' in there ?


Image
B
3
Posts: 5
Reputation: 185

Post » Wed Apr 13, 2016 4:02 pm

Ther is NO 'on item get' condition in that screenshot. None . Zip.
B
33
S
18
G
27
Posts: 2,436
Reputation: 20,336

Post » Thu Apr 14, 2016 9:11 am

99Instances2Go wrote:Ther is NO 'on item get' condition in that screenshot. None . Zip.


Now Work. TNKS!!!

Image
B
3
Posts: 5
Reputation: 185


Return to How do I....?

Who is online

Users browsing this forum: Lancifer, MadSpy and 7 guests