How do I save local storage to JSON using nwjs?

0 favourites
  • 13 posts
From the Asset Store
"Easy Local Notifications" enhances games with customizable local notifications.
  • [SOLVED]

    CURRENT GOAL: To create a save for each game slot to the users pc, using arrays into JSON and NWjs and/or Local Storage.

    CURRENT ISSUES:

    1. I cannot figure out how to save those arrays using NWjs, IF I even need to use NWjs.:::

    The save of anything with SAVESTATEJSON is not saving anything, and in fact the save file has 0kB. Nothing saves.

    2. I cannot figure out how to load Local Storage, as I am wondering if I need NWjs and/or AJAX, etc.:::

    I cannot LOAD Local Storage back into array without knowing where Local Storage saved to.

    What is not working? NWjs saving to local folder. It WILL save the file! BUT...it will not save data to that file.

    Question 1: What is the best (not easiest) way to do this? I.e. Arrays or dictionaries then JSON with NWjs or Local Storage or both, etc?

    Question 2: Am I on the right track? I have it where it saves the arrays. Check. I have the arrays saving to Local Storage. Check.

    NOTE: I am using arrays to make the save sizes smaller and traditionally for large data in the end.

    -If I save the array, it updates Local Storage just fine. Thats about as far as I have got successfully with multiple tests. So, I have my arrays. Good. Now, what way is best to save the way I need and load the way I need? Having all these options confuses me on what works and doesnt and what goes together or not, etc etc.

    photos are included of what Ive done simply with testing.

    P.S.I am a bit slow, so please don't attack my intelligence. I have head injuries. Somehow I have to state that because someone always judges me. Please dont. I just want to see if there is help. Thank you all for helping if you can. I am patient as hell, so Ill communicate until WE can get it to work! Coffee or beer money for whomever gets this resolved with me! LOL

  • Hi, you don't need NWjs to make multiple local storage saveslots, unless you want to create files that the player can manipulate afterward;

    Once you put everything you need in your array, use Local storage plugin to set an item with array.asJson as its content, and its slot name, that way you can save item "save1", item "save2" etc, a method is to use a variable to choose with slot is selected;

    To load it, you need to do the local storage correct action order : Get item "(insert slot name)", on item get, set array from Json with localstorage.ItemValue ~

  • Ok, cool so NWjs isnt necessary at all because I dont want any manipulation outside of what the game stores and loads. Thats good to know! TY

    So, I have my array that has variables that include the game slot, player name, and then all of the in game vars so far that I have :) I have an issue loading the JSON to array though at the moment.

    When I go to load the Local Storage information for say, slot 1, I do the proper get then on get flow but it does not load correctly. I was wondering then if this is because I am using "set" in the array instead of "insert" maybe because in the long run, these arrays will be expandable.

    SO, what I am thinking is, based on what Ive read, -Do I have to have the initial array always match the W, H, D of the saved array in Local Storage for it to load properly? If so, and I want to make it dynamic, I dont know how I could impliment a dynamic array into Local Storage if the load has to match the current blank arrays dimensions. I heard something about having to make them all 3 dimensions regardless. But That is as far as I have got on reading so far :P

    Thank you for the help, I will focus on Local Storage ONLY for now and test with that to see if I can get the LS to load back INTO the default array.

  • When I set array.AsJson I am supposed to put the name of the array in "array" correct? So, if my array is named save1, then it would be save1.Json, correct?

    Also, on item get....do I have to load all the items individually this way or can I load the array fully at once? Thank you again for the help! I will be reading and testing the rest of the night :)

  • I'm not sure of why you want to use multiple arrays, is that to display the datas of each saves on a menu screen? Like health, level, weapons, etc

  • I'm not sure of why you want to use multiple arrays, is that to display the datas of each saves on a menu screen? Like health, level, weapons, etc

    Oh, so I have 3 arrays, one called playerGameSave1, and 2 and 3. So, each array is going to be used for each "gameslot", that way I can have different values for the players stats and info for each load. SO, if you wanted to load game 2, then it reads from playerGameSave2.Json, and if it does not exist it creates the array information with default values so it doesn't mess with gameslot 1 or 3.

    Each array will hold ALL of the game variables, global and local. The variables will be updated (global for example) when the array is loaded and overwritten when requested or upon autosave of that slot.

    The current issue with LocalStorage I have at this moment is loading it. I did the proper loading setup, but it does not load into the array I select for it to load into, according to debug. Ill show my code for loading:

    So, when the game starts, it will have a blank array and if not, it creates one. If you have never played the game, it will use that array to update, save, and then eventually load. So, say I changed things that needed saving. I update the values in the global variables, and local variables for gameplay, and then the array is updated and then saved AsJson in Local Storage. All is well so far. So, then the loading part.

    LOADING:

    Let me just simplify it. Say I have a button that when clicked, should load the Json into the proper array. I say when clicked, >LocalStorage > get item "[key used to save array]". In my case, this key is "gameData1". So after get item, I make another event saying: LocalStorage > On item "gameData1" get, action: with the proper array:Load from JSON string> LocalStorage.ItemValue.

    This does not load the array. I do clear the previous array too in testing to see if it loads properly then wait before loading and still nothing loads. Hope that makes sense :) The info IS, in fact, correctly saved and its format is correct. So, the LocalStorage has this information in that specific key that cannot be loaded even with simple actions.

  • > I'm not sure of why you want to use multiple arrays, is that to display the datas of each saves on a menu screen? Like health, level, weapons, etc

    Exactly, yeah either display, load, or whatever I need to do with that information. Each slot has an array to save to. :)

    What is even more strange is, I can load it from another layout lol. It works when tested the EXACT same way in code from another part of the game. Do you think clearing the array first has a setback on this? I give it time before I try to load it again so I wouldn't think that would be an issue. I just need to make sure I can load it when I need to. Thank you again for your time, I appreciate it very much

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here is a video of what Im doing. I hope it is helpful. Note: In the video I had changed the global save variable to enable and disable for testing. After the video, I changed it back and it still had the same errors or uneventful actions. :P

    youtu.be/AoZKw2tlUH8

    Subscribe to Construct videos now
  • From what I see in the video you sent, no information about the saved datas are displayed on the load save screen, in that case only one array is necessary, but it would take too long to explain again, so I'll prepare an example capx when I'm home, I'll also make an example with a load screen that displays the saved datas in case of (this one needs more than one array)

  • Thank you for the help. I could be overthinking something so Ill keep chugging :) You dont have to make one if you dont want to but if you do, thank you for everything!

  • Alright, I made two example capx :

    First one is basic, it saves and load the array and update the global variables :

    drive.google.com/file/d/1xq6FTed8jhtOrgkKI8yHFUKpXeScQ_u9/view

    Second is more complex as it also displays the content of each saves on screen :

    drive.google.com/file/d/1QbhycuMy43jMu5Pcse-WAi4WaYEzoNsC/view

    Don't hesitate to ask if you need more help

  • WOW, thank you very much I will look into these and learn something from them to fix what I have and mark solved when it works out :) You have been a great help, let me know what I can do to return the favor!

    I want to note for anyone that views this thread, there is something important I learned while working to create this save/load system.

    When you make a game, create global variables to use for updating instance variables because the global variables can be used easily to save to the arrays and should be done in three ways:

    1. Upon change of an instance variable

    2. Upon exiting/saving purposely or if a force close happens

    3. Automatically every X seconds (i.e. like MC does with say 3 minutes etc)

    Saving:

    a. Update all the global variables from the instance variables

    b. save the global variables to the array

    c. save the array to local storage

    Loading:

    a. load from local storage into array(update it from save data)

    b. update global variables from array - this way you can EASILY update instance vars

    c. update instance variables from global variables

    That is a flow of save and load I found EASY for me to follow! Ans when I add stuff, I just set the next array positions of XY to the key and value and save it again!

    Now, I will look into these files you have created and see how I can utilize that flow with them to create a multiple save system and make sure I dont make more arrays than I need :P

    God Bless!

  • Alright, I made two example capx :

    First one is basic, it saves and load the array and update the global variables :

    https://drive.google.com/file/d/1xq6FTed8jhtOrgkKI8yHFUKpXeScQ_u9/view?usp=sharing

    Second is more complex as it also displays the content of each saves on screen :

    https://drive.google.com/file/d/1QbhycuMy43jMu5Pcse-WAi4WaYEzoNsC/view?usp=sharing

    Don't hesitate to ask if you need more help

    I LOVE these! I just need to make an overwrite feature for them just make sure the player is sure they want to do so, and I can implement this idea easily :) Thank you so much, I will actually go ahead and mark this solved because this is EXACTLY like my flow so far, just easier to understand haha! TY and happy coding!

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