Keeping track of time played even after app closed

Get help using Construct 2

Post » Thu Mar 09, 2017 10:16 pm

I found the Local storage and it works well in terms of storing values, but how do you add to it? Like if I have 10 stored in local storage, how to add say 2? As of now, the 2 replaces the 10. Im trying to figure out how to make time played persist across the app being closed. I wish local storage had an addition feature. You can store values, but those values get overwritten. Is there any way to add to local storage?

Or is there another way of keeping track of how long someone has played a game?
B
20
S
7
G
3
Posts: 302
Reputation: 3,634

Post » Thu Mar 09, 2017 10:20 pm

Like the number is supposed to keep going up. How do you make it so the current value Key isn't overwritten? Where the Key value is added onto, not replaced.
Last edited by Phoenixbowman on Thu Mar 09, 2017 10:37 pm, edited 1 time in total.
B
20
S
7
G
3
Posts: 302
Reputation: 3,634

Post » Thu Mar 09, 2017 10:27 pm

The way LocalStorage works is the same as Dictionaries work:
Each key can only exist once.
This means, that if you write to an existing key, it will always overwrite the current value.
This is the way it works and can't be changed.

There is a workaround though:
  1. When the app is started, get the current value from the Local Storage
  2. Save the value to a variable and increment the variable regularly, as desired
  3. In regular intervals, or before you quit the app, save the variable as the value of the Local Storage key
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Thu Mar 09, 2017 10:46 pm

Is there another way to keep track of time played across the app being closed? I know about Variables and every x seconds doing something. And storing in global variables. But global variables get reset when the app is closed, and I have is so Global Variables reset when they start a new game. Is there another way to store numbers besides Local Storage, or is local storage the only way to go for keeping track of time played across the app being closed?
B
20
S
7
G
3
Posts: 302
Reputation: 3,634

Post » Fri Mar 10, 2017 5:15 am

It works just fine, unless you remove the Key then it says NaN I set up a text box to show the Key value, Every second it updates a global variable. When I have it it works fine but say they uninstall and then re-install the game, the Key is missing. Using that method up there the Key value shows NaN. Like where its supposed to update the global variable with the Key value it says NaN. I've tried various combos of check to see if Key exists etc but its stuck on NaN.

When the Key is there it works great. but when the key is removed, say they uninstall the game and re-install, the Key is gone. i try checking if exists but it says NaN.
B
20
S
7
G
3
Posts: 302
Reputation: 3,634

Post » Fri Mar 10, 2017 5:22 pm

Is there another way to keep track of time played across the app being closed?

Yes, I recommend @rexrainbow 's TimeAway plugin.
It works with the Local Storage, but saves you all the tedious saving and loading.
This won't work for the total time in the app though, obviously.

________________________________-

Is there another way to store numbers besides Local Storage

If you want your app/game to work offline and don't want to work with the system time, you will have to work with locally saved files.
Apart from LocalStorage, there is also the possibility of saving, loading and parsing files with NW.js & AJAX.
This will be more complicated, but will also give you more freedom when it comes to the workflow.

__________________________________________

but say they uninstall and then re-install the game, the Key is missing

That's the way, LocalStorage works. The keys/values saved by LocalStorage are part of the app, so they get deleted as well if you delete the app.
If you don't want to work with online servers and you are not on mobile, you could use the registry, but apart from that, I don't know about any other ways of saving and keeping values even when the app is reinstalled.
(That's just me though, there might be others out there who know better)

___________________________________

Like where its supposed to update the global variable with the Key value it says NaN

As mentioned above, when uninstalling the app on mobile, the LocalStorage keys will be deleted since they are stored in the same location as the app.
NaN in connection with LocalStorage will appear when there is no data available. (Or if you try to combine strings and numbers, but this doesn't seem to be the case here)
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Fri Mar 10, 2017 6:41 pm

I had it working............now it just says NaN >< I have check item exists on start of layout. If it exists get key. after getting key set global variable to the key value.

I noticed you can do LocalStorage.Key and LocalStorage.CurrentValue but there is no way of referring to a key name in those.

I keep getting NaN when i run it.
B
20
S
7
G
3
Posts: 302
Reputation: 3,634

Post » Fri Mar 10, 2017 6:51 pm

I had it working before but was checking what happened if the Key got removed. Now it's all messed up. No matter what i do in terms of Key existing, not existing, getting key, on key get, etc, it always shows NaN ><
B
20
S
7
G
3
Posts: 302
Reputation: 3,634

Post » Fri Mar 10, 2017 8:42 pm

Clear the local storage for that project.
B
33
S
18
G
28
Posts: 2,477
Reputation: 20,918

Post » Fri Mar 10, 2017 9:04 pm

The process of getting a value from the LocalStorage:
  1. Action: Check item [key] exists
  2. Event: On item [key] exists
    -> Action: Get item [key]
  3. On item [key] get
    -> (now, the value is available under the expression LocalStorage.ItemValue)

Also, as soon as the event On item exists, you know that the key exists. If On item missing triggers, that means that it doesn't exist.

Here is an example .capx in case you are stuck: Download
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 2 guests