How do I save global variable values?

  • Say the user earns 100 coins and then closes the game, how should the coins value be saved so that he reloads the game. This is for Mobile Games for iOS, Android and WP8.1. The save action inbuilt doesnt work...

  • Hi, I referred to it. But it didnt state anything about global variables. I tried that method. But it doesnt work. I have also included the Eventsheet of globals in the event sheet where the save occurs.

    If this doesnt work, then can I use Local Storage to save and retrieve each variable?

  • Using local storage is the easiest way and should work something like this:

    local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

    on start of layout - local storage get Item "Playercoins"

    on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)

  • Hi, I referred to it. But it didnt state anything about global variables. I tried that method. But it doesnt work. I have also included the Eventsheet of globals in the event sheet where the save occurs.

    If this doesnt work, then can I use Local Storage to save and retrieve each variable?

    If you want to save just one variable you can do this like the way mentioned above. But if you want to save more than 1 variable I would prefer to use an Array and save it asJSON in the LocalStorage.

    I made an example file if you don't know how to handle that.

    Local Storage

    I'm currently updating all my games using WebStorage to LocalStorage. I will release a LevelSelection working with LocalStorage instead of WebStorage. If you have any question feel free to send me a PM.

    Regards,

    Andreas

  • Thanks for the file! AndreasR. Will definitely try to implement it.

  • Thanks for the file! AndreasR. Will definitely try to implement it.

    You're welcome Don't hesitate to send me a PM if you stuck or need help! Glad to help you out then.

    Regards

    AndreasR

  • Using local storage is the easiest way and should work something like this:

    local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

    on start of layout - local storage get Item "Playercoins"

    on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)

    I got this working but still not getting two points .....

    As why are we using key to store the same value as in global variable & what is itemvalue used for ?

  • > Using local storage is the easiest way and should work something like this:

    >

    > local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

    >

    > on start of layout - local storage get Item "Playercoins"

    >

    > on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)

    >

    I got this working but still not getting two points .....

    As why are we using key to store the same value as in global variable & what is itemvalue used for ?

    You must imagine the LocalStorage as a Table. The key is the ID and the value is the value of your saved variable.

    LocalStorage

    So you can save each variable individually or you can save all variables in an array and then the array via asJSON into the localstorage.

    In order to load the saved item at a certain key you need to run 'Check item exists (your key)' -> If key exists -> Get Item (your key). Then the value of the fetched key can be set to the variable by 'LocalStorage.ItemValue'.

  • Using local storage is the easiest way and should work something like this:

    local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

    on start of layout - local storage get Item "Playercoins"

    on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)

    Thanks for quick refrence that helped me a lot .....

    so is playercoin a variable or integer or key ? as you want to set the global variable score to it am i right ?

    So if possible could you just report the same code using the table reference if possible as it would help me to understand much easily .

    also I think this two statement should be called in start of the layout .....

    local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

    local storage get Item "Playercoins"

    so is it necessary to call them randomly or just in a order to get the value of globalvariable to key playercoin ?

  • > Using local storage is the easiest way and should work something like this:

    >

    > local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

    >

    > on start of layout - local storage get Item "Playercoins"

    >

    > on item get "Playercoins" - set GlobalVariable = int(localstorage.itemvalue)

    >

    Thanks for quick refrence that helped me a lot .....

    so is playercoin a variable or integer or key ? as you want to set the global variable score to it am i right ?

    So if possible could you just report the same code using the table reference if possible as it would help me to understand much easily .

    also I think this two statement should be called in start of the layout .....

    local storage set Item key = "PlayerCoins" value = (the name of your GlobalVariable)

    local storage get Item "Playercoins"

    so is it necessary to call them randomly or just in a order to get the value of globalvariable to key playercoin ?

    I made an example here. Just download it from my dropbox account

    https://www.dropbox.com/s/iz8cvjw86vfh8mm/local_storage_playercoins.capx?dl=0

    This should answer your questions

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I made a tutorial for LocalStorage <img src="{SMILIES_PATH}/icon_e_wink.gif" alt=";)" title="Wink">

    https://www.scirra.com/tutorials/1461/h ... age-plugin

  • Thanks that really help me to sort out my problem & confusion tooo !!!

  • Thanks that really help me to sort out my problem & confusion tooo !!!

    Glad to hear that You're welcome!

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