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