Keeping track of time played even after app closed

  • 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?

  • 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.

  • 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:

    • When the app is started, get the current value from the Local Storage
    • Save the value to a variable and increment the variable regularly, as desired
    • In regular intervals, or before you quit the app, save the variable as the value of the Local Storage key
  • 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?

  • 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.

  • [quote:3v3o2ju3]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.

    ________________________________-

    [quote:3v3o2ju3]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.

    __________________________________________

    [quote:3v3o2ju3]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)

    ___________________________________

    [quote:3v3o2ju3]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)

  • 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.

  • 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 ><

  • Clear the local storage for that project.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The process of getting a value from the LocalStorage:

    • Action: Check item [key] exists
    • Event: On item [key] exists -> Action: Get item [key]
    • 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

  • Thank you everyone! It works now. i was so confused when I saw NaN and I was like arrr (like a pirate but im not a pirate if that makes sense) but you all helped thank you it works now.

  • I was getting stuck on Key Missing because i figured I had to set something up for if the Key was missing, like if exists do this, if missing do that just in case it was missing. Like for a situation in case it was missing, but cleared the storage and did that set up and it works!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)