How do I make sense of the logic with local storage?

  • ok one specific question that i don't think i saw a direct answer too:

    - when you check if item exists and it doesn't exist, does it at that moment create it?

    I ask because the "on item exists" is a one time trigger event which makes me think it might get missed if the first time you run it and it checks if it exists, what if it's not done creating the new item, just seems sketchy compared to an action that's constantly looking to see if an item exists.

    - you don't ever have to use item missing condition?

    I can see why it would be nice to have as an option but making sure this isn't a required condition. I could see both sides of it. I think it's not from examples I've seen but part of me thinks it would make sense that a "doesn't exist" condition would be needed to identify it doesn't exist so you could then tell it to create the item.

  • when you check if item exists and it doesn't exist, does it at that moment create it?

    No, it does not create it. This is why you need the item missing condition.

    For example, in my games, I usually check if the item "PlayerData" exists at the start of the game.

    If the item exists, I know the player has played the game before, I can then load the PlayerData from local storage into a JSON object and use it (my PlayerData is a JSON object which stores data like levels unlocked and user preferences.)

    If the item does not exist, I understand that the player has not played before, so I create a default PlayerData profile and load it into my JSON object (e.g. only level 1 unlocked, all other user preferences as default.) After the PlayerData object is loaded. I then save the JSON object (as a compact string) to localstorage with a key of "PlayerData".

  • how you explain it makes sense to me mikal, but i'm not seeing how you would create the local storage object. i'm confused because here is an example that doesn't show any "create item". i'm just not seeing it.

    when the "is missing" is used it just gets used to set some text fields and blank.. no call to create anything.. and this example works. this example would be helpful but it doesn't work with a "on start of layout.." it's click driven.. i need a system that's automatic and done without user involvement.

    dropbox.com/s/rok73tjrrtezk8r/local%20storage%20working.JPG

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • When I say 'create' I really meant doing this function, this creates the item if it does not already exist.

    -> LocalStorage: Set item "PlayerData" to PlayerData.ToCompactString
    

    Also read over this a few times (as you see more examples, read over it again.)

    construct.net/en/make-games/manuals/construct-3/plugin-reference/local-storage

  • Here's an example from my game (I use Playfab to do network login, I also use a JSON object to store PlayerData.)

  • Ok so for anyone who comes across this thread, I wanted to give a working example of how you could manage simple on off settings and build up from here.

    I found the examples that were provided in this thread by others as helpful in seeing it working, but i never understood how the dictionary made things easier for my particular situation, so here is an example of two independent values being stored and recalled correctly and I threw a score value in there as well to show them all working together.

    I just like to avoid complicating anything if it doesn't need it and it felt like the dictionary just added more room for mistakes and confusion and for at least this situation it was the case.

    https://www.dropbox.com/s/na3zdgvvvez80wu/Local%20Storage%20Test%20with%203%20Values.c3p?dl=0

    Thanks everyone who made comments and shared their knowledge. It all helped me work through this. I banged my head on this one for awhile now and just haven't been posting questions. Hopefully this example will save others from experiencing similar frustration for something that may seem obvious to those who got it working but for someone new, is not always the case.

  • part12studios Dictionary allows to store multiple values in one Local Storage item, so you don't need as many events as when you store each value separately. When you have 2-3 values, it's not much of problem, but when you need to store 10-20 items, the code may become pretty messy.

    The example you posted has a few small issues - you don't need "Local storage get item" action in events number 2 and 4. And there is no point in setting items to 0 in events 3 and 5.

    Also, there is a new feature that can be used with local storage, see this video:

    construct.net/make-games/releases/beta/r149

    .

    So basically you can read everything in one event:

  • Interesting. That's definitely where reading about these objects lacks specifics in the documentation for Construct 3.. yes they explain what the fields are but i feel like they don't really explain the inner workings and some simple examples. this documentation would be well served to give some capx examples because reading about something and applying it are not one in the same.

    Sometime its easier to read if things are broken out more for me. i can see how grouping things would be more efficient but when my dictionary keys were not storing my values it proved to just be a layer of abstraction that was more trouble than it was worth to troubleshoot because i couldn't "see" where things were not working.. i would just have to run and test.. and keep getting nowhere. Now that i see this working with just local storage finally (all i really wanted) I may give dictionaries another pass and see.

    The examples in this thread seemed to deal with strings and that raised a question that I need to save values that are numbers, not strings.. maybe the dictionary string / json stuff was not casting the values in a way to be usable by a numbers variable. just lots of little issues like that.. ultimately i couldn't get dictionaries to work in my situation which was unlike the examples given.

    I'm definitely glad to know there are some things i can do without and just removed them.

    It just isn't clear to me in the documentation. we have to check if an item exists. and we have a if it's missing condition.. so does just checking if it exist then "instantiate" the item? or perhaps "set item" is what creates an item if it doesn't exist?

    So really "Is Missing" isn't needed at all for actual storage.

    I did see that video recently and it looks interesting, but for now it's more than i need in my situation.

    I cleaned up my capx and removed the audio stuff i was using for testing (i like to use sounds to identify if the thing "exists" or not when i was testing that.

    Thanks for that input! I just figured that it made sense that if something doesn't exist, i need to do something to make it become real, but i guess just setting a value and giving it a name creates it in that exact moment..

  • we have to check if an item exists. and we have a if it's missing condition.. so does just checking if it exist then "instantiate" the item? or perhaps "set item" is what creates an item if it doesn't exist?

    Checking if item exists doesn't create it. But if it does exist, you don't need the extra "Item get" step.

    So really "Is Missing" isn't needed at all for actual storage.

    It may be useful, for example as an indication that the game is launched for the first time and you may want to show some intro to the player or something like that.

    But in your example when items are missing, you are setting them to 0, which is already the default values for variables. So here this isn't really needed.

  • ah yes first time running check would be a nice thing to capture with that. good point. thanks, that's super helpful! :)

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