• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Legacy GM What happens to lists and maps in these instances?

XirmiX

Member
Wondering what would happen in these cases to lists and maps. Picture that they are in a script, and when a script is called with either of these pieces of code in, I'm interested in knowing what will happen in terms of garbage collection; will the list or map be destroyed or hang around filling up memory the more it is called? Here are the two examples using a ds_list:

Code:
var thrownParameter = argument[0];

var newList = ds_list_create();
var newParameter = thrownParameter++;
ds_list_add(newList , newParameter);

return newList;
This is just the same thing, but the variable holding a reference to it is not declared with a "var" prefix:
Code:
var thrownParameter = argument[0];

newList = ds_list_create()
var newParameter = thrownParameter++;
ds_list_add(newList , newParameter);

return newList;
I am asking because I know that ds_list_destroy() exists in GML and the problem is that there is no way to use this function at any point because the list needs to be returned. If in both of these cases the garbage collector doesn't remove these lists from memory, is there some better way I could handle this while still getting the desired result?
 
Why not run the code and see? You should be able to run debugger and check and see what all data structures exist. You just need to pause with debugger and inspect the bottom panels, I forget the name of it exactly, it's down there with the the output console.

If I had to guess if you don't return the DS it would be destroyed, but maybe I'm wrong. I expect once you return it though you need to destroy it like normal.
 

Ricardo

Member
Data structures are globals in GMS and it doesn't matter if you create them in a script or hold their reference using a local variable. They'll remain in memory unless you destroy then.
In your case, you obviously won't use ds_list_destroy in the scripts, but you must still use it later outside the script to get rid of the list once it is not relevant anymore.
 
Last edited:

samspade

Member
Wondering what would happen in these cases to lists and maps. Picture that they are in a script, and when a script is called with either of these pieces of code in, I'm interested in knowing what will happen in terms of garbage collection; will the list or map be destroyed or hang around filling up memory the more it is called? Here are the two examples using a ds_list:

Code:
var thrownParameter = argument[0];

var newList = ds_list_create();
var newParameter = thrownParameter++;
ds_list_add(newList , newParameter);

return newList;
This is just the same thing, but the variable holding a reference to it is not declared with a "var" prefix:
Code:
var thrownParameter = argument[0];

newList = ds_list_create()
var newParameter = thrownParameter++;
ds_list_add(newList , newParameter);

return newList;
I am asking because I know that ds_list_destroy() exists in GML and the problem is that there is no way to use this function at any point because the list needs to be returned. If in both of these cases the garbage collector doesn't remove these lists from memory, is there some better way I could handle this while still getting the desired result?
There is a difference between the list and the variable which holds the list. The list is 'global' in the sense that any instance can reference it if they have the reference. So when you create the list, that list now exists in whatever way GM is holding lists under the hood. The variable that holds the list, is not the list itself. The variable is treated normally under standard scope rules. So if it is an instance variable, it will be destroyed when the instance is destroyed and if it is a local variable it will be destroyed at the end of the list.

In your case you're returning the reference to the new list so there's no problem. So long as you destroy it later. The following code would create and destroy a single list - the list created and returned with the script is the same list destroyed:

Code:
///script_create_list()
var new_list = ds_list_create();
return new_list

///use
my_list = script_create_list();
ds_list_destroy(my_list);
Just remembers that variables don't hold data structures in the way they hold a number or a string. They hold a reference to a data structure which is somewhere else. So long as you have that reference number you can access, change, and destroy the data structure and you can pass around that reference and store it in as many places as you like.

As a side note, at present the 'reference' to a data structure is literally just an integer. That's why you always have to reference what type of data structure you're using because GM has no way of knowing if a 0, 1, 2, etc. is just a 0, 1, 2, etc. or if is a reference to a list, map, queue, etc.
 

TheouAegis

Member
You could even enumerate lists at the start of the game, never saving their IDs anywhere.

For example:
Code:
enum items {
sword,
axe,
mace,
bow,
gun
}

repeat 5 ds_list_create();
Code:
ds_list_add((instance_place(x,y,obj_ItemDrop).type);
 
Top