How to use the LocalStorage plugin

3

Attached Files

The following files have been attached to this tutorial:

.capx

local-storage-playercoins.capx

Download now 171.31 KB

Stats

17,525 visits, 28,837 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 9 May, 2015. Last updated 19 Feb, 2019

About

Here I'll show you a way (sure there are diffrent ways to work it out though) for saving and loading numeric variables or string variables from the LocalStorage.

If you want to support me you can get a full working example capx file here

Game Template 8 - LocalStorage

(The premium capx example using an array and a dictionary to store more than one variable to the LocalStorage at once, just check it out)

Free examples

Moreover I worked out some free examples using LocalStorage. You can download those examples from my Dropbox folder or from my Webpage.

Dropbox: LocalStorage examples (Dropbox)

Webpage: LocalStorage examples (Shatter-Box)

Please note that english isn't my mother tonuge, but I will give my best to write it in a correct english. If you anything wrong, please notify me via a PM.

Thanks!

The Local Storage Plugin

General

Note: Please check first if you have downloaded the latest version of Construct 2. Otherwise the plugin isn't included in your editor.

You can imagine the LocalStorage as a simple table with two rows (Key, Value). See the illustration above.

The Key

The key will be used to identify and fetch the value saved along to the key from the LocalStorage. You can save as many keys as you want into your LocalStorage. Each key can save it's own value.

(This key will be used later to fetch or to update the value saved at a certain key.)

The Value

Is the value saved for a certain key. The value can be everything like a numeric value, a string or even an array as JSON (allows you to save a whole array into the LocalStorage using only 1 key).

Code Example

Now to the code. First we need to add our LocalStorage plugin to the project.

If the plugin doesn't appear check if you have installed the latest version of Construct 2.

Then we let the LocalStorage check if the key already exists. So just add following code to your project.

This will check if our key already exists in the LocalStorage or not.

- playercoins - is a numeric variable

- playercoins_key is the key where the value of the variable 'playercoins' will be saved later (you can call the key like you want, I just added '_key' to it to show that the key doens't need to match with the variable)

The next step is to add the function what should be done if the key exists or not.

If the key exists

If the key exist we will fetch it from the LocalStorage by using the action 'Get Item (playercoins_key)'. Then if the key was fetched successfully we'll update the value of our variable 'playercoins' with the saved value in the LocalStorage.

Note #1: There is always the latest fetched value saved at 'LocalStorage.ItemValue'. So if you want to fetch another value you need first to perform 'Get Item (other key)'. Only then the value of the other key is saved in the 'LocalStorage.ItemValue'.

Note #2: Thanks to an user comment. It isn't necessary to call 'Get Item' on 'Check if item exists'. So you can use 'LocalStorage.ItemValue' directly in the 'Check if item exists' event.

If the key doesn't exist

On the first start of your project the LocalStorage is empty. So in this case we need to add following action to the event 'if item X missing'.

This function will create a new item with the key 'playercoins_key' and will set its value to the value in the 'playercoins' variable.

Questions?

If you have any questions or if you are missing something in this tutorial, please use this topic to inform me.

LocalStorage - Forum

.CAPX

local-storage-playercoins.capx

Download now 171.31 KB
  • 5 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hi, I want to create an offline game, there will be one form to capture users personal detail and then play a game, I need to know how can I extract stored data (user details) from local storage.

  • excuse me, I'm sorry to bother you a little. I have downloaded all of the above capx but I don't understand it yet. My question is which event, when local storage stores global variables?

    thank you

    pras

    • Hi Pras,

      When you fetch the key from the localstorage, the value for this key is stored in the "LocalStorage.ItemValue" expression.

      So simple set at "On Item "[key]" get" => System -> Variable [YourVar] -> Value = "LocalStorage.ItemValue".

      You can see an example in the attached capx file

      s1.construct.net/tutorials/downloads/24272/local-storage-playercoins.capx

      Best,

      Andreas

      • oke thank you mr Andreas. ihave fix that about local storage. my app have publised in play store. i have new problem, "is online in browser plugin" not working. Are you have solution about this? :(

        thank you mr Andreas

        • Hi, sorry for the long wait. I can remember that it didn't work with Cocoon, however cocoon is dead now and it should work for Cordova. Have you installed the "Inappbrowser" plugin at your cordova project?

          This is needed if you want to use the browser functions.

          Best,

          Andreas