So something like this:
Ds_map(instancemap(object_index-variable,instancevariable)
Am I right with this?
Or would it be:for
dsmap "myobject" object_index
dsmap "myvar1" instance.var1
dsmap "myvar2" instance.var2
?
I know I'm going to catch flack for this but we are behind in development and its because I don't understand
how to do the following situation:
I need to save a global variable that contains a reference to an instance. This is what I have now:
Code:
ds_map_add(instanceMap, "global-current_collision_instance", global.current_collision_instance);
I need to save the object_index and the variables in the instance, I think. I don't know how to do that with a dsmap being that its a key-value pair.Can somebody jump in and give me a hand?
Duplicate topic merged. ~Tsuk
That would only work if you have 1 instance per object you're saving.
We have too little information about your project to be sure what the best advice is.
For example, I'm wondering why you need to save entire instances in the first place.
And if so, what instances need to be saved?
Do you want to save every instance in the room?
Does this include, for example, wall instances that never change from the initial room configuration?
Or do you only want to save the state of a controller object?
This makes a huge difference in how you best save things.
The replies you've gotten before don't suggest you encode all the instance vaiables of every instance in the same "instancemap" variable.
They all suggested you just simply save the variables of the instances you want to save (somewhere in some files), but not necessarily encoded in a single datastructure.
Even though a ds_map can encode a list of intances with their variables, it is not the most logical choice for it.
It is far more logical to encode a list as a list.
You could construct a list where each element represents a single instance you desire to save.
You could represent an instance as a ds_map that maps variable names (as strings) to the respective instance variable value.
Along with the variables you want to save, you should also save the values of "object_get_name(object_index)" and "id".
This is then a datastructure you can easily save and contains sufficient information for later reconstruction.
When loading this datastructure, you'll need to iterate over the elements of the list and create an instance for every element in the list.
The object type of the instance to create depends on the value of "object_get_name(object_index)" you've saved and is "asset_get_index(...)" by default.
You then assign to every variable (that does not refer to an instance) of the newly created instance the saved value for that variable.
For every instance that you create while looping over this list, you add the id of this instance to a new list "newInstances" and a ds_map "idMap" that maps the saved value of "id" to the id of the newly created instance.
Then, when you're done iterating over the saved list (so you've recreated every instance), you start iterating over the elements of "newInstances".
Now you assign for each instance you iterate over a value to every variable that does hold a reference to an instance.
To figure out the value you need to assign, you look up the saved value for the variable of the corresponding instance in "idMap".
If this returns undefined, you just assign the saved value to the variable (because the reference probably points to an unsaved instance or is a keyword).
Otherwise, you assign the value returned by looking it up in "idMap".
There's a saving mechanism for you.
Of course, depending on your project this might still not suffice.
For example, you might have variables that hold references to object indices, which would require special attention as well when updating your game.
You might have instances you want to save without receating them when loading (controller objects for example).
Create events might be screwing you up when trying to load an instance (which could be solved by using instance_change).
You may want to destroy instances when loading a saved state and then destoy events may be a problem.
And there are tons of other possible issues I can think of right away.
However, we need more information about your project if we would want to tackle those issues.
PS: Just so you're aware, there is a game_save and a game_load function that circumvents this issue,
but if you were to use those, they'd come back to bite you in the ass eventually.
One of the reasons being that dynamic resources don't get saved (and it looks like you ar using those)
and another reason being that updating your game corrupts savefiles ceated this way.