Easy way to manage nested lists

kupo15

Member
Recently I've been convinced that nested lists are more flexible and better than using grids so I've been transitioning my grids to nested lists. I'm really enjoying transitioning my ds_grids into nested lists, however, I have to triple check that when I delete a ds_list that I'm also deleting any lists nested inside of it as well to avoid a memory leak which is a bit of a pain.

For example:
upload_2020-1-14_15-30-47.png

If I destroy List A, I have to make sure I destroy List B-D, and I also have to remove List A pointer from the Master List. When nesting lists for JSON saving, its easier to not worry about all this because you simply mark each list and map as you go and destroy the wrapper at the end, but for persistent data structures that aren't temporary, this seems a bit more involved

Can we and is it practical to use ds_list_mark_as_list for memory management outside of JSON saving/loading as the help file says otherwise?

I'm thinking there has got to be a way that I can create a script called ds_list_destroy_nested(pointer) where I input the list I want to destroy, and the script loops through the rest of the nested data structure, marking each nested list as a list, in which I can then simply destroy the list I started with. Then I can remove the pointer in the list above it


 

kupo15

Member
Yes, use it as needed. Or perhaps even better, look into using something like @FrostyCat's extension for working with JSON data in a much more convenient way.
Cool thanks, I'll check it out when I get home even though I don't need the help when it comes to encoding or decoding json data. Unless you mean dealing with json type structures in general as I'm asking about managing nested lists outside of JSON format. From my understand, marking lists during normal apps when you are referencing them doesn't work because the _mark_as functions remove the pointers and turn everything into strings? Which makes building a system by pointer reference not possible. This is why thoughts are only to retroactively endlessly loop through the data structure to mark_as_ when I want to destroy part of it
 

chamaeleon

Member
Cool thanks, I'll check it out when I get home even though I don't need the help when it comes to encoding or decoding json data. Unless you mean dealing with json type structures in general as I'm asking about managing nested lists outside of JSON format. From my understand, marking lists during normal apps when you are referencing them doesn't work because the _mark_as functions remove the pointers and turn everything into strings? Which makes building a system by pointer reference not possible. This is why thoughts are only to retroactively endlessly loop through the data structure to mark_as_ when I want to destroy part of it
The extensions deals with managing the data in the JSON structure (including saving and loading, but not limited to it). Its big selling point is providing functions for reaching deep into the structure to get data out, or update data, and create fully fleshed out data structures with one script call.

The mark functions doesn't remove pointers and turn it into a string. It marks the containing structure as the owner of the map or list being added, but other than that the child map or list remains exactly the same and you can continue to use the appropriate functions or accessors as desired.
 

Neptune

Member
Youre saying this is what nesting is?
Code:
var list1 = ds_list_create();
var list2 = ds_list_create();
ds_list_add(list1,list2);
I've been reading/writing all DS into/out of eachother lol
Unless the above code is just adding the pointer of nest2 to nest1...
 

kupo15

Member
The extensions deals with managing the data in the JSON structure (including saving and loading, but not limited to it). Its big selling point is providing functions for reaching deep into the structure to get data out, or update data, and create fully fleshed out data structures with one script call.
Cool, I'll have to check it out then

The mark functions doesn't remove pointers and turn it into a string. It marks the containing structure as the owner of the map or list being added, but other than that the child map or list remains exactly the same and you can continue to use the appropriate functions or accessors as desired.
Oh wow, really? That's awesome to hear! That means that I can easily create my own ds_list_create_nest() to nest AND assign ownership...do all the stuff I normally do then simply delete the tail end I don't want and never worry about memory leaks. Awesome! Why on earth does the manual strongly recommend against using this functionality outside of json_encoding/decoding scripts? Are they afraid that us users are too dumb to understand what happens when you mark this and are afraid they will get inundated with questions wondering why all their data structures are getting destroyed? Here is another part for maps saying these functions are "useless" unless you are encoding json strings

upload_2020-1-14_18-38-16.png
Youre saying this is what nesting is?
Code:
var list1 = ds_list_create();
var list2 = ds_list_create();
ds_list_add(list1,list2);
I've been reading/writing all DS into/out of eachother lol
Unless the above code is just adding the pointer of nest2 to nest1...
Yes, that is what nesting is. In your example though, you are nesting pointers within pointers
 

chamaeleon

Member
Youre saying this is what nesting is?
Code:
var list1 = ds_list_create();
var list2 = ds_list_create();
ds_list_add(list1,list2);
I've been reading/writing all DS into/out of eachother lol
Unless the above code is just adding the pointer of nest2 to nest1...
Yes, that is what I mean. The above code just stores the numerical data structure id of list2 in list1. Using the mark function instead for the same operation does the same thing with the additional effect of telling list1 that the item added is in fact a list (so that gms json serialization knows this and does the list serialization instead of just writing out the number). By knowing that an added map or list is in fact a map or list, the destroy function can then do the cleanup of, in this case, list2 when you destroy list1.
 

kupo15

Member
Thanks a lot @chamaeleon! It works like a charm and saved me a bunch of code I had planned to create this system. I bet saving the json file is going to be much easier now that what I had to do converting from a grid as well

Still unsure why the manual is so against using this in this way, unless I'm mistaken and JSON refers to more than the file format and also refers to nested structures in general
 
Last edited:

kupo15

Member
Two follow ups regarding this, @chamaeleon .

I came across an odd behavior while converting to nested lists and marking_lists_as_lists. I understand how destroying a list that has pointers to other lists marked as lists will destroy all of them as well. That's what marking it is for. However, what about clearing a list that contains marked lists? I have a master list that holds mariked list pointers in them. I cleared the master list and it destroyed those marked lists. I tested using ds_exists and those lists existed before clearing the master list but not after. Is this a bug? I wouldn't expect clearing a list would touch anything to nest lists marked as such, it should just simply make the master list empty.

Lastly,
Now that we are essentially creating a json structure in-game (instead of creating one during saving as in juju's tutorial) is there any use in doing that all again, creating maps with labels to make things readable for the encoding/saving process? I did this but after the fact feel like it was a waste of time and kinda pointless given that all my data is nothing but lists. I figure, what's the point looping through the structure, recreating ANOTHER json structure using maps to label "username = nestedlist[| 0]"? As long as I keep the order of the lists the same for saving and loading it should be fine I think. Having a readable json file seems kinda pointless and extra work.

keep the "listname" "owner" etc as lists instead of converting them into maps


In this scenario, the best way to to encode my nested lists that have already been made into a json structure is to Simpy...encode the root data structure I already have...right?

Lists only
 
Last edited:
Top