If you look at http://127.0.0.1:51291/index.htm#t=GameMaker_Language%2FGML_Overview%2FStructs.htm it says:
When a struct is no longer required it can be removed from memory using the delete operator, which flags the struct as being able to be garbage collected. This is not strictly required as the garbage collector may do this automatically in the following game steps if the struct is no longer referenced in your code, but it is good practice to do so and we recommend it (for example, call delete in the Clean Up event of an instance to explicitly tell the garbage collector that an instance scope struct is to be deleted).
However, if you read the entry for Delete, it says:
What this means is that you call the delete operator along with the variable that holds a struct and it will remove the specific reference to the struct stored in the given variable (a reference is simply a value that points to the memory area that is storing the struct and its contents). If this reference was the last reference to the struct in your game, then the garbage collector will remove the struct from memory in a following iteration, typically at the very start of the next step.
I spent hours trying to figure out why this struct wasn't disappearing (partly because the debugger, when it got to the line with "delete" it jumped up 40 lines of code and said the line that was executing was something completely irrelevant so I went down that rabbit hole). Turns out, delete probably only destroys that reference to the struct? What is the point of that? Couldn't I just as easily use `someStruct = undefined;` instead of `delete someStruct`?
I have a system that has a couple of different references to some structs in a hierarchy, but each of those only has one actual parent. That parent should be the only one that can actually delete it, but other structs might have references to the child struct.
So, for example, StructA has two structs that it creates and adds to a list of children: StructB and StructC. StructX is it's own struct on the same level as A and asks A for a reference to StructB. Now, StructX has a direct line to StructB. When I want to destroy StructB, my plan was for StructB to tell StructA it was ready to be destroyed and StructA would delete StructB and remove it from its list of children. Then, I figured I'd just check `is_undefined` before StructX tried to talk to StructB again, but since StructX even knows that StructB exists, StructB can't be deleted without me hunting down every reference that it has.
Is there a way to use something like the first description of delete?
Please sign in to leave a comment.