How do I avoid pulling my hair when using arrays & localStorage?

0 favourites
  • 13 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Hi everyone, i know this has been explained several times in the forum, but I´m fighting with something for the last two days... The idea is Kids read "customized" stories with their name, moms name, etc. One on each layout so they can have a "minigame in the background" and a layout for entering their data to be shown.

    I´ve seen this amazing tutorial construct.net/en/tutorials/arrays-beginners-170/page-3 but still can´t connect my brain cells with this.

    I have 12 textBoxes where users type their data into, a save button, and a gotolayout button. ok so far

    Users fill the 12 textboxes, on each change, 12 globalvariables get updated. ok so far

    When users click "save", An array gets updated on it´s 12 "x" values.ok so far

    The user clicks wich for they want to see, they click gotolayoutX Ok so far

    They find a text with strings to be replaced, they get replaced using Replace. Ok so far

    Now I need to save this &%$ so next time they use the app, they don´t have to type everything again (unless they need to)

    So, first layout, Localstorage, check if "data" exists, if so, load "data" from Array.Asjson, and place it in the array..... HERE I GOT LOST, REALLY LOST

    if anyone could explain how to work this out as if I am stupid... I´d really appreciate that.

    Was sending it from 12 user imputs to 12 globalVariables to 12 Xs in array a stupid move?

    This is my .capx

    drive.google.com/open

    Thank you very much, I´d really love to understand this %$# and stop asking over and over ;)

  • Also I fixed the other logic, it's one event now. There is no need to use global variables or multiple objects. I use a method of instance variables to track array position, have a look :

    dropbox.com/s/uff9hdnf7trftwo/imput.capx

  • Thanks so much for your answer,

    I added the line you suggested, but when I reload my browser, the data goes back to the original instead of the new one entered...

    any idea on how can I overwrite it each time I enter new data?

    Thank you so much

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Change 'on 1 pressed' to 'on start of layout' so it does the load on start of layout instead.

  • Just saw your file, thank you. In your file I don´t get to replace texts, even when trying to mix it with mine, I can´t find how to specify wich line of the arrey shuld be replaced

    Is it working for you ? Thank you

  • Yeah I got confused there about what you wanted, try this : dropbox.com/s/q0v4pidsjpvqdya/imput2.capx

    To clarify my method there is the instance variable on the text input object is the line in the array. If arraypos=0 it is 0 in the array.

  • Thank you so much for your help! Your example works great, I had to change the way words are replaced in the second layout, and now seems to work ok :)

    Since you took the time, would you mind explaining your logic and how you thought this? Not really the technical part wich I can see it, and eventually learn it through tutorials, but the logical part, your thoughts before typing.... seems I had trouble understanding the order things should be thought before even opening construct.

    My call was to have multiple textboxes, on each change, update a global variable and store it on a localstorage+array... then, on the final text, use those global variables to replace a string inside the text.

    I think that, somehow, I overcomplicated things even before starting. Lack of technical knowledge, of course, but maybe also logical thinking. My example was working, but compared to yours, was long and complicated. As far I as I see, yours is simple and straight to the point.

    If you could just tell me how you did plan it in your head, would be a great way to learn something that I cannot really learn from technical tutorials.

    Thanks so much, and have a great week!

  • It is :

    array contains data

    text box relates to each line of the array with an instance variable, so 0= position 0, 1 = position 1 etc

    array dynamically updating with whatever is in the text box for that position

    then on save : save current array in json format to local storage

    on load : load array in json format from local storage

    Because all data is stored in an array, you don't need to also store data as global variable. I link a text input object to a specific location in the array with an instance variable. My thought process was first to avoid global variables because you have to save and load them individually. Using an array is nice and powerful for this. Next step is to allow you up to update array data and I did this with an instance variable acting as position for the array.

  • Thank you so much for this extra explanation :)

    I can now visualize a little bit better what was done, hopefully understanding it for future implementations.

    Thank you!

  • I have another question, hope you don´t mind... But please, before answering, if it´s complicated, just ignore me, and tell me before I get into something I won´t be able to understand so I don´t waste anyones time. lol This is just curiosity, not a project.

    Is it possible to store data online so everyone uses the same data?, I can save locally, but I´d like to know if its possible to "play" on the same project without really being a multiplayer game or dealing with firebase, etc.

    Let´s say for the sake of this example I want to access the same game in the morning from a pc, in the afternoon from my phone, maybe tomorrow from another couuntry, etc and find this data always stored in the server where I upload it, no matter who touches it... a co-op clicker..

    here´s an example

    badablu.com/desarrollo/records

    badablu.com/desarrollo/records/records.capx

    Thanks! :)

  • I've not really touched that side of things but I would say multiplayer is the way. Kind of shooting yourself in the foot by saying without using multiplayer because it would be an ongoing multiplayer game hosted somewhere so that seems to be the way :)

  • thank you so much for your answer!

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