1. Hey! Guest! The 32nd GMC Jam will take place between Feb 22nd, 12:00 UTC (Friday noon) and Feb 25th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Saving a game with object instances

Discussion in 'Programming' started by Ted Gress, Feb 2, 2019.

  1. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    466
    Hi


    I'm trying to store my variables in a ds_map but I don't know how to go about saving an object inside an object.. For example, how to save the global alch_hourglass object in the code below:

    Code:
    global.alch_hourglass = noone;
    
            ds_map_add(instanceMap, "global-alchemist_timer", global.alchemist_timer);
            ds_map_add(instanceMap, "global-alchemist_lock", global.alchemist_lock);
            ds_map_add(instanceMap, "global-poison_all_splash", global.poison_all_splash);
            ds_map_add(instanceMap, "global-poison_all_splash", global.poison_all_splash);
    
     
  2. CameronScottCreations

    CameronScottCreations Member

    Joined:
    Dec 1, 2018
    Posts:
    68
    What do you mean you need to save the object, do you need to save the state of the object, such as specific variables in that object like how much progress a timer has made and restore it to be exactly the way it was when the game ended?
     
  3. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    466
    I need to save the state of the object. But I don't know if the object is instantiated, For example:

    Code:
    [/COLOR][/FONT][/LEFT]
    [FONT=arial][COLOR=rgb(222, 222, 222)]
    [LEFT]
    ///////save file names
    global.save_file_name = noone;
    global.load_file_name = noone;
    
    ////Alchemist Timer
    
            ds_map_add(instanceMap, "global-loading_flag", global.loading_flag);
            ds_map_add(instanceMap, "global-saving_flag", global.saving_flag);
    


    Bascially my question really is how to save a variable with a no one value
     
  4. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    671
    fyi the assignment noone gives the variable a value of -4, so doesn't effect how you set up your save system.
     
  5. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    739
    @Ted Gress I am not sure what you are exactly trying to save and load but if it involves saving instance ids and later loading them and hoping they make sense, I would urge you to come to with something else and store data associated with the instances instead. If you think that storing the instance ids in a map makes it possible for the map serialization functions to do something with the instance variables that is not correct either (if that is not what you're doing my apologies, but there is very little code context to understand what you expect to happen).
     
  6. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    466
    @chamaeleon Yes. I need to save an object in a ds map and i'm not sure how to go about doin git. Do I break it down into its constituent variables? What then when I go to load the file, if I just create the object I broke down, it will be a new instance and I don't necessarily want that - it would reset all the objects.
     
  7. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    739
    As far as your ds_map is concerned, the instance you're storing is just a number (the id as returned by the create method), and that is what will be saved. It has no idea that the number happens to be instance rather than position floating point number, or a life counter, or .., etc. No introspection of the instance happens. If you need to save an instance's variables, you need to have code that goes through and saves those that are relevant for a save operation. Instance id numbers, data structure ids, etc., are just numbers with no meaning attached to them in their variables.
     
  8. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    913
    Ted, you can't save an instance and respawn it with all its variables being reconstituted using the instance ID. Once you destroy that instance, the instance ID for it means absolutely nothing. What you have to do is save the object ID AND all the variables you want saved. Then use that object ID to create a new instance of that object type and then fill that instance with the saved variables when you load it. I already showed you how to do it in the other thread.
     
  9. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    739
    Just a note to say that storing an object id could invalidate a save file if an updated program is used that rearranges, adds, or deletes objects in the resource tree. Better to let the storage structure either implicitly represent the type of object a piece of data is representing, or store an identifier (string, number, whatever) that can be mapped to an object, or the name of an object and use asset_get_index() to get the object id that is valid for the executable running.
     
    RefresherTowel likes this.
  10. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    913
    Good point, but the overall structure remains the same. Save the type of object you want (Ted: Use asset_get_index(object_get_name(object_index)) to get the object type, instead of simply the object_index) then create an instance of that object when you load and add in/modify the variables you have saved in the map to the newly created instance.
     
  11. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    739
    Another note... asset_get_index(object_get_name(object_index)) is the same as object_index... What one should do is save object_get_name(oid) to the file, and use asset_get_index(str) to get the object id upon load for instance creation.
     
    RefresherTowel likes this.
  12. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    913
    Hahaha, yeah, that's right. *shakes head*
     
  13. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    466
    RefresherTowel: So when I am saving these references I save the object id and all the enclosed variables? How do I do that with a ds_map?

    Would it be something like:

    ds_map_add(instanceMap, "global-object_index", object_get_name(object_index));
     
  14. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    466
    So basically I’m saving the object type and the variables that make it up?
     
  15. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    229
    You can save and load variables for different instances by referencing that instance in the ini file you have created... use ini_open(name of save file) to create a new save file.

    If your variable is using numbers, then use ini_write_real.
    Code:
    ini_write_real(Object name for instance, Variable being recorded, value of variable);
    If it's using a string, then use ini_write_string.

    Code:
    ini_write_string(Object name for instance, Variable being recorded, value of variable);
    So your done saving the variable. Now you wanna load it in. So, determine when the game should load in (at startup, when you press a button, etc.).

    Then, you want to open that save file with ini_open(name of save file).
    Now you can reference the variables in that file.

    Let's say your object is obj_car. And the variable is color. The value you saved is red.

    Code:
    variable = ini_read_string(object name being referenced, variable being referenced, default value for the variable);
    
    so...
    
    obj_car.color = ini_read_string(Car, color, black); //the car's color, default is black (if the variable you are looking for isn't in the save file, then it will default to this).
    
    If you're referencing the color from the obj_car itself, then you don't need to put obj_car.color but instead just color.
    Likewise, if it's a global variable then just use global.color instead.
     
  16. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    466
    So something like this:

    Ds_map(instancemap(object_index-variable,instancevariable)
     

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