How do I make LocalStorage work?

  • Hello, afaik LocalStorage is rather used than WebStorage, right? I am a bit confused because the following lines don't work at all:

    http://prntscr.com/dlcjet

    the gamescore doesn't set either case..am I missing smthg?

  • You need to run the "Check item exists" action at the start of your layout for your triggers to work. According to the manual:

    "Check if a key exists in storage. This triggers either On item exists if the key exists, or On item missing if the key does not exist. If the item exists, the ItemValue expression is set to the key value in the On item exists trigger, so there is no need to use a subsequent Get item action to read the value."

    Read more about LocalStorage here: https://www.scirra.com/manual/188/local-storage

  • oh, i completely missed that! thank you

  • But when do not exist, I want to create it, how do I do it?

    Because the "else" is not supported.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • On item missing

  • But when do not exist, I want to create it, how do I do it?

    Because the "else" is not supported.

    It will 'exist' after you set a trigger for it to exist.

    Example:

    Player - On destroyed > System - Set lives (lives is the global variable) to 4

    Then add the action to set lives to localstorage:

    Localstorage - set lives_key(1) to lives(2)

    Number 1, the lives_key is just a name for THAT data in localstorage. You can call it anything you want, but most people will use the global variable its saving and some will add the KEY after it, just as a quick reference so they don't, i mean me, get confused.

    So when you start the game again, that localstorage WILL exist now, and it will have the value of 4.

  • Thank you very much to both. I used "on item missing", and solved my problem.

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