How do I Read javascript localStorage inside Construct 2

0 favourites
  • 4 posts
From the Asset Store
Casino? money? who knows? but the target is the same!
  • Hi, All...

    I Need a big help regading how to read a variable stored in my .html javascript localStorage.

    I have one page that stores some data inside localStorage:

    E.g:

    <script>

    localStorage.setItem("lastname", "Smith");

    </script>

    I would like to read this variable inside Construct 2.

    Is there any tutorial that could lead me on how to do this?

    Thanks in advance!!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hey !

    Same problem here. I've tried to get data from local storage into another page after setting it into Construct2 and vice versa.

    It seems Construct2 developpers use a standard javascript library called "Local Forage" (and not Local Storage). This library is similar to local storage but use asynchronous mechanisms and other storage methods like WebSQL or IndexedDB. That's probably why we need to wait specific events to retrieve our data and that's why we cannot get/set the information using local storage only.

    So if you need to set data into your HTML Page and use it into Construct2, you can check out the local forage library (available here : https://github.com/mozilla/localForage). Follow the examples available in the github page. For me it works like a charm. <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    It could be useful if a Scirra developper may confirm you can use local forage library safely in external web pages to get Construct2 information. It could be dangerous if they decide to make a bridge with specific keys or if the storage priorities are not the same between the HTML Page and the Construct2 source code.

    Cheers !

  • Local storage is asynchronous and relies on indexedDB as well.

    Also, local storage infos are not shared between browsers and domains.

    So unless your script is hosted on the very same domain your game is, C2's local storage is likely not going to be compatible with other Local storage sources.

    And even being hosted on the same domain, it probably is not something that will happen.

    Check the How do I FAQ at the AJAX category to know how to exchange datas between a C2 client application and an external script. There is also an item on how to pass variables from the HTML page to the application IIRC.

  • I need to do the opposite (pass information from construct 2 to an external web page) and informations don't need to be secure and will be cleared if user goes back to the Construct2 game page. So if I can avoid a server web page and use only static html/javascript page, it could be great.

    I've found two ways to do this :

      Local storage GET parameters

    With GET parameters it works but all data are stored in the URL which is not convenient for the user. But i'm pretty sure this is the best compatible solution for every browsers. I was not sure if Local Storage is a good solution (that's why i've asked if we can use local forage safely). If you say it could be dangerous, i'll go back to the GET solution (or another best solution if you have).

    Thanks for the answer BTW.

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