• 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!

Is it possible to get all instance variables as ds_map?

breakmt

Member
Hello! Can I get ALL variables from my instance as ds_map? I want to save my instance (as json), but don't want to point which variables I want to save, just want to save them all. Any thoughts? Thanks!
 

chamaeleon

Member
Hello! Can I get ALL variables from my instance as ds_map? I want to save my instance (as json), but don't want to point which variables I want to save, just want to save them all. Any thoughts? Thanks!
Thought: You cannot distinguish a number being used as a number from a number being an id for a data structure. Hence you cannot codify whether to call some kind of data serialization function or not just based on the value, you'll need to keep track of that information elsewhere (in some other way, data structure or whatever, or explicitly handle those variables on a case-by-case basis).
 

breakmt

Member
Thought: You cannot distinguish a number being used as a number from a number being an id for a data structure.
Ouch... English is not my first language and this sentence is really hard for me. I'm not sure that I understand you correctly.
With "variable_instance_get_names()" I will get an array with all variable names. With "variable_instance_get()" I will get that variables values.
In theory everything is OK :)
 

chamaeleon

Member
Ouch... English is not my first language and this sentence is really hard for me. I'm not sure that I understand you correctly.
With "variable_instance_get_names()" I will get an array with all variable names. With "variable_instance_get()" I will get that variables values.
In theory everything is OK :)
Not ok if you understand that IDs returns by ds_list_create(), etc., are numbers and you have other variables that contains non-data structure number values. You are only ok if all your variables only contain numbers used for position, speed, gold coin count, etc. Again, if a variable contains a data structure id, you will have no means to determine that you need to handle it specially (json_encode(), ds_list_write(), or whatever, in contrast to string()). Knowing what names of instance variables you have does not tell you at all what the contain as far as code is concerned, unless you handle it explicitly.

Some initialization code
Code:
a = 100;
b = ds_list_create();
ds_list_add(b, 1, 2, 3);
Some save code
Code:
var names = variable_instance_get_names(id);
for (var i = 0; i < array_length_1d(names); i++) {
    var v = variable_instance_get(id, names);
    if (names[i] == "b")
        show_debug_message("saving list " + names[i] + " = " + ds_list_write(v));
    else
        show_debug_message("saving number "+ names[i] + " = " + string(v));
}
I needed to write code to handle one instance variable specifically in this case. Otherwise, it would only "save" (just using show_debug_message() in the example) a number like 0, which of course is not the content of the list.
 

chamaeleon

Member
Code:
a = 0;
b = ds_list_create();
c = ds_map_create();

show_debug_message("a is real " + string(is_real(a)));
show_debug_message("a is list " + string(ds_exists(a, ds_type_list)));
show_debug_message("a is map  " + string(ds_exists(a, ds_type_map)));
show_debug_message("b is real " + string(is_real(b)));
show_debug_message("b is list " + string(ds_exists(b, ds_type_list)));
show_debug_message("b is map  " + string(ds_exists(b, ds_type_map)));
show_debug_message("c is real " + string(is_real(c)));
show_debug_message("c is list " + string(ds_exists(c, ds_type_list)));
show_debug_message("c is map  " + string(ds_exists(c, ds_type_map)));
Code:
a is real 1
a is list 1
a is map  1
b is real 1
b is list 1
b is map  1
c is real 1
c is list 1
c is map  1
The result of this is not surprising if you inspect a, b, and c, and see that the number 0 is stored in all of them. The only thing that makes 0 have meaning as a list or a map is because you call a list or a map function with it as id argument. But in order to know whether to do that, you need to know beforehand that you want to do that based on some other information that isn't just that value.
 

breakmt

Member
Oh, now I see. Yes, I did not consider that variables could be not only simple values. Thanks for that information!
 
Top