• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Saving instance_place

I'm saving all my game values in a ds_map. But I came across a snag. How would I go about saving something like the following:

global.current_bat = instance_place(x, y+100, SentryBatObject);

Before I was just saving the objects, but since this variable is a reference, I don't know how to serialize it.
 

FrostyCat

Redemption Seeker
When you save, save the instance ID alongside each instance you save, and write variables containing instances IDs as-is. As an example, this is what your save data may look like in JSON form:
Code:
{
  "instances": [
    {
      "id": 100aaa,
      ...
    },
    {
      "id": 100bbb,
      ...
    },
    ...
  ],
  "current_bat": 100aaa,
  ...
}
As you load and re-create the instances, create a map pairing the old instance ID (i.e. the one read from the saved data) and the re-created new instance ID (i.e. the corresponding one returned by instance_create_*()). Here's what the pairing map may look like in JSON form:
Code:
{
  "100aaa": 100xxx,
  "100bbb": 100yyy,
  ...
}
To finish, simply use the pairing map to translate the loaded instance ID into a re-created instance ID.
Code:
global.current_bat = pairing_map[? string(loaded_data[? "current_bat"])];
If you're going to be using maps and lists to store your data, I strongly recommend that you familiarize yourself with data modeling in MongoDB. Most of the principles work the exact same way with plain JSON, and it really isn't all that hard.
 
Top