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

Help with map destroy

H

hiep

Guest
Hello !

I used JSON to save game like that
Code:
var list = ds_list_create();
with (Obj_Undo)
{
    var _map = ds_map_create();
    ds_list_add(list, _map);
    ds_list_mark_as_map(list, ds_list_size(list) - 1);
   
    var obj = object_get_name(object_index);
    // feat is a ds_list i create for every object which is child of Obj_undo
    ds_map_add_list(_map, "fea", feat);
}

var    wrapper = ds_map_create();
ds_map_add_list(wrapper, "root", list);
var json = json_encode(wrapper);
ds_map_destroy(wrapper);
return json;
I dont know why my feat (ds_list) in every object which is child of obj_undo is destroy when use
ds_map_destroy(wrapper);

I want to keep the ds_list (feat). Please help me.

Thanks for advane !
 
H

Homunculus

Guest
Lists added using ds_map_add_list are automatically deleted when you destroy the parent structure. The only way to keep it is by adding a copy to the ds_map instead, look at ds_list_copy in the manual
 
T

Timothy

Guest
Lists added using ds_map_add_list are automatically deleted when you destroy the parent structure. The only way to keep it is by adding a copy to the ds_map instead, look at ds_list_copy in the manual
What if the list is large... maybe using ds_map_replace_list before destroying is a better option?
 
H

Homunculus

Guest
Considering you will ideally be creating the copy and destroying it soon after you save, I can't really see that as a problem, both in terms of memory nor performance. It really depends on what you mean by "large", but if you are really storing that much data to begin with during your regular gameplay, chances are the copy will not cause any issue.

If you are really worried about that, I think that using ds_map_delete should work as well, since as far as I know it's not supposed to delete the contained list.
 

samspade

Member
Hello !

I used JSON to save game like that
Code:
var list = ds_list_create();
with (Obj_Undo)
{
    var _map = ds_map_create();
    ds_list_add(list, _map);
    ds_list_mark_as_map(list, ds_list_size(list) - 1);
  
    var obj = object_get_name(object_index);
    // feat is a ds_list i create for every object which is child of Obj_undo
    ds_map_add_list(_map, "fea", feat);
}

var    wrapper = ds_map_create();
ds_map_add_list(wrapper, "root", list);
var json = json_encode(wrapper);
ds_map_destroy(wrapper);
return json;
I dont know why my feat (ds_list) in every object which is child of obj_undo is destroy when use
ds_map_destroy(wrapper);

I want to keep the ds_list (feat). Please help me.

Thanks for advane !
You don't need to destroy any data structures at this point in the above code and you probably should be putting a duplicate of the feat list in instead of the feat list itself. That last one is harder to answer without knowing how it is used.

You destroy lists to prevent yourself from creating a memory leak. However, in the above case all your list and map references are stored in a primary wrapper list and marked as such. You don't want to destroy them (as you found out) in this case as you are then destroying the very things you're trying to save (sounds over dramatic).

While this is one of the more intermediate things to wrap your head around, remember that all data structures basically exist off on their own somewhere. They don't live in the objects or scripts that created them. What lives in the objects are the references, the addresses, of those lists. You simply need to not lose all the references to a list because if you do, you can't delete it and you have a memory leak. In the above case, assuming I tracked everything correctly, there is a reference to all the lists and maps in the final wrapper. So as long as you destroy that at some point (presumably on game save or game end) you're fine. You haven't lost anything.

The reason feat list is more complicated is that if you don't copy it, you will have two references two it - not bad in and of itself - but if at any point you call destroy that list (say in the clean up event of the object or you destroy the wrapper in game save) then the other object has a reference to a non-existant list which will cause a crash if you try to use it.
 
H

hiep

Guest
Thanks for your help first !.
My idea is save every move in my game by JSON then i push it into a ds_stack. when i undo the game i can create everything like the previous step.

I tried to "putting a duplicate of the feat list in instead of the feat list itself" but event happen exact like i put itself.

Yeah. You right i want to prevent memory leak. I only want to save the ds_list (feat) not the wrapper or list or _map... You mean it save into an adress and when i destroy wrapper it destroy _map, list and everything i put in it ?

So any solution for prevent memory leak and save the ds_list (feat) too ?

Thanks you
 
H

Homunculus

Guest
If you used a duplicate and the original got destroyed, you are doing something wrong. How did you implement that?
 
Top