Weak References - Garbage Collection in GameMaker

preview_player
Показать описание
Let's talk about struct cleanup using weak references! Don't worry, this one won't be obsoleted the day after I post it like the last one was.

Example on Github:

GameMaker Studio 2.3 playlist:
GameMaker 3D playlist:

0:00 Introduction
1:11 Making myself look bad
1:30 Creating a weak reference
2:55 weak_ref_alive()
5:22 What to do with weak references
9:54 GameMaker things
11:38 Using these in the real world
13:15 The end

Thumbnail:
Trash by Xinh Studio from the Noun Project

#GameMaker
#GameMakerStructs
#GameMakerGarbageCollector
- - - - -
I've now made that Patreon:
That promised social media link spam.
Transition / idle music is by my old friend Kamex:

Rate, comment, subscribe, watch more, enjoy!
Рекомендации по теме
Комментарии
Автор

Sentence of the day - "Weak references are nothing like your seventh grade english teacher" ....

mzzy
Автор

Hello! I have been encountering the weirdest bug ever (at least i think it is) with weak references, i hope maybe you know whats going on? This might be a longer comment, I'm just stuck on this problem and could really use some help.

Context: i am making a vampire survivors clone (just for practice) and use struct as light weight objects for a lot of things, like the enemies and the projectiles, for performance reasons. One of the weapons i have shoots bullets at the closest enemy. When the bullet is created, a target (enemy struct) is given in the form of a weak reference. The bullet checks every frame if the weak reference still exists, if not, it just keeps on moving in its current direction, nothing special. I keep track of the enemies in a list, and a grid (3d array), and when an enemy is destroyed, i make sure to remove ALL references to it in the grid and list. All of this works, but for some unknown reason, the bullets "weak reference alive" check still goes off.

Here is the thing, I've tracked the source of this problem to the WEIRDEST thing I've ever come across, and i can even recreate the problem in an empty project. Here it is. For some baffling reason, whenever i make a 3d grid using arrays, if i make it TOO BIG, the weak ref alive check stop functioning. Here is how i recreated it in a blank project:

OBJECT:

CREATE EVENT:
struct =
{
a : 234,
b : 231
}

reference = weak_ref_create(struct);

counter = 0;

var _counter = 0;
for(var i = 0; i < 30; i++;)
{
for(var j = 0; j < 30; j++;)
{
grid[i][j][0] = _counter;
_counter++;
}
}

KEY PRESS SPACE EVENT
struct = undefined;

STEP EVENT
if(weak_ref_alive(reference))
{
show_debug_message(counter);
counter++;
}

If i put this in an empty room, i see the counter going up in the console, and when i press space, nothing happens. BUT if i change the width and height of the grid from 30 to 10, it DOES work.
Do you have any idea how this could happen? Thanks for the awesome tutorials!

jurreosinga
Автор

This doesn't make any sense other than for maybe debugging purposes....the whole point of a destructor isn't to make sure they were cleared with garbage collection, it is specifically TO clear themselves for garbage collection. For example, I use double-linked list structs on occasion that use head/tail pointers, along with various link/unlink capabilities....if I were to weak_ref every single one to ensure they were cleaned, then I'm just building the list all over again, that's tedious, not to mention what am I supposed to do if a pointer doesn't get clean? Yell at the "customer" that the program didn't do its job?

There simply isn't good pointer gc in gms right now....I would love to have a straightforward function like "pointer_nullify" or something, where the memory of that location is cleaned regardless of how many references are pointing to it. (Any errors in debugging would make it far easier to tell where your problems lie in trying to reference a pointer after it has been destroyed).

I also wonder if gms keeps a count within the struct block every time something references it to denote gc requirements....or if it's just a bit flag and as soon as ANY reference drops it, it is put in gc regardless of how many actually point to it. I may have to test that out one day.

sabriath
visit shbcf.ru