1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

[SOLVED] Saving and regenerating instances that the player has spawned

Discussion in 'Programming' started by Fredrik, Oct 20, 2019.

  1. Fredrik

    Fredrik Member

    Joined:
    Jun 21, 2016
    Posts:
    182
    Hey!
    A few days ago I asked for help on how to make a system to save 'states' for various instances that have been changed by the player, like in a dungeon crawler if the player break a crate or open a chest, to then go down to a new floor, and then back up to the previous one again, and how to make the crate stay broken and the chest stay open. I got excellent help from the forum, and now I need help with something similar.
    Now, in the dungeon crawler I'm making it's possible to throw items on the ground, but if I throw a sword on the ground, then go down to a new floor and then again goes back up to the previous floor, that sword will be gone as it has been removed :/ does anyone have any good suggestions on how to save and then regenerate the same object at the same position??

    Suggestion:
    Just before going to a new floor a system will loop through all instances under parent_item and give them an id, similar to what I've done with the previous problem, which was fixed with PixelatedPope's tutorial:


    Basically a string combined with what floor you're on + the object index + x position + y position, which will almost in all cases, even in a randomly generated dungeon, give a fully unique id.
    Then save all the id's of all the item instances as well as count how many items that are saved, in a file. Upon going back to a floor with items that should be regenerated, or when restarting the game, there gotta be a system that will loop through a instance_create loop that will go through all the saved id's and generate instances based on what's saved in that id. I bet this could be done if it's possible to refer to certain positions in a string (?) like from postion 1 to position 2 ?
    A id created using this system can look something like 2obj_itmdrp_ironsword400300 where 2 is the floor it was generated on, obj_itmdrp_ironsword is the object index, 400 is the x and 300 is the y. With this it should be possible to fill in the needed information to a instance_create function? but I'm not sure how to do it. If there's a Game Maker guru out there that might have suggestions to this I'd be happy :)

    Thanks!
     
  2. Catan

    Catan Member

    Joined:
    Jun 20, 2016
    Posts:
    747
    Instead of generating a single string per instance (requiring you to parse it at room start) you could use JSON. Basically, you loop over the instances and save their values in a ds_map, put that map in a ds_list, and store everything to a json file representing the floor items.

    You don’t need unique ids with this setup since all you have to do on load is go through the list and create the instances.
     
    Last edited: Oct 20, 2019
    Fredrik likes this.
  3. Fredrik

    Fredrik Member

    Joined:
    Jun 21, 2016
    Posts:
    182
    @Catan
    Thanks alot for that suggestion. I watched Shaun Spalding's tutorial on saving and loading with JSON, and it works perfectly!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice