Local Storage Array's

0 favourites
  • 5 posts
From the Asset Store
"Easy Local Notifications" enhances games with customizable local notifications.
  • Hey I'm wondering if there is any way to use arrays or matricies with Local Storage. So I have 3 save accounts, A, B, and C. and each save account has the following, Level, Checkpoint, Health, and Power. My original thought was to, when the game is saved, have Local storage Set Item Account (In other words Set item "A" or "B" or "C" depending on what the global Account Variable is) to "L"&Level&"C"&Checkpoint&"H"&PlayerHealth&"P"&Power . So for example, it may set A = L3C2H99P40 Meaning level 3, checkpoint 2, 99 health, and 40 power. Rather than having each be a different item. BUT the poroblem i run into here, is that once I get the value of A.... I'm not sure how to separate those values, not sure how to use that to set Level=3 and Checkpoint=2 and Health=99 and P=40. I'm hoping that there is some way to perform this using syntax changes, but I'm wondering if anyone can think of anything.

  • You can use text expressions find(), mid() to separate values from your string "L3C2H99P40".

    However it would be much easier to use one or several dictionaries.

    For example, write this data to the dictionary:

    Key="A_Level", value=3

    Key="A_Health", value=100

    Key="B_Level", value=7

    Key="B_Health", value=34

    To save it to local storage: LocalStorage set item "SaveGame" to Dictionary.AsJSON

    To load from local storage: LocalStorage on item "SaveGame" get: Dictionary load from JSON LocalStorage.ItemValue

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can use text expressions find(), mid() to separate values from your string "L3C2H99P40".

    However it would be much easier to use one or several dictionaries.

    For example, write this data to the dictionary:

    Key="A_Level", value=3

    Key="A_Health", value=100

    Key="B_Level", value=7

    Key="B_Health", value=34

    To save it to local storage: LocalStorage set item "SaveGame" to Dictionary.AsJSON

    To load from local storage: LocalStorage on item "SaveGame" get: Dictionary load from JSON LocalStorage.ItemValue

    I've thought about the dictionary, but I would honestly rather do it this way. But for the use of find and mid , could I have an event, "On ItemGet 'A'"-> Set 'Level' to mid(LocalStorage.ItemValue, find(LocalStorage.ItemValue,"L"), 1) ?

  • Yep, that's correct if you have only levels from 0 to 9.

    If you want to store multiple values in a single string, I recommend separating them with some symbol and using tokenat() expression.

    For example:

    s="3;10;100;1"

    Set level to tokenat(s, 0, ";")

    Set checkpoint to tokenat(s, 1, ";")

    Set health to tokenat(s, 2, ";")

    Set power to tokenat(s, 3, ";")

  • Yep, that's correct if you have only levels from 0 to 9.

    If you want to store multiple values in a single string, I recommend separating them with some symbol and using tokenat() expression.

    For example:

    s="3;10;100;1"

    Set level to tokenat(s, 0, ";")

    Set checkpoint to tokenat(s, 1, ";")

    Set health to tokenat(s, 2, ";")

    Set power to tokenat(s, 3, ";")

    Sweet thank you! I had no idea about that expression, this is very useful. Thank you so much!

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