How do I save and load a dynamic array?

0 favourites
  • 4 posts
From the Asset Store
Match the similar cards with each other and free all animals!
  • Hi everyone,

    In my game, I would like to display a few random story sentences that will be displayed after [x] levels.

    So far, I created a JSON file for all my sentences that will populate my ArrayStory.

    When the conditions are met, I randomly choose a RandomIndex from my ArrayStory to pick a sentence and then I delete this index, to make sure the Player won't see it again.

    The problem is:

    I generate my array on start of the level from the JSON and obviously, it fills the array with all sentences.

    So, how can I load the JSON file only once and populate my array and then, each time a randomindex is chosen and deletes the index from the array, it saves the array?

    I have an idea that I would like to share with you:

    If I create a variable: ArrayStoryFilled = 0

    Then, when I load the JSON file into my ArrayStory the first time it sets ArraStoryFilled=1.

    When ArraStoryFilled=1, I save this variable in LocalStorage.

    Can I save an array in LocalStorage?

    then each time a new sentence is picked and deleted from the array, I save the array.

    Does localStorage work also on mobile?

    Does it sound like a good approach?

    it's the first time for me that I tackle save and load system.

    Thank you

  • Yes, you can easily save the array in Local Storage, and yes, it works on mobile. Use Array.AsJSON expression.

    You can do this - on game launch check if the key exists in Local Storage. If it does, load the array data from Local Storage. If it doesn't load it via AJAX from your JSON file.

    I have a demo, it saves a dictionary in Local Storage, but it's very similar to saving an array.

    dropbox.com/s/lu1mzn30kl2sawf/LocalStorageSaveVariables.c3p

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you dop2000

    I'll check your demo.

  • dop2000

    I wanted to tell you it works perfectly.

    Thank you again for your help.

    I might have another question.

    Let's say that the Player has seen all sentences.

    Do I have to do something specific when the array is empty?

    lol I think I found. I just need to check if the Array is not empty I guess?

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