In the first line you tell the game that you want to allocate a new chunk of memory on the Heap for a GameState with GameState.new(). At that point result_state is a reference to a fresh new GameState object. It’s looking at a specific memory address on the heap and reading/writing to the GameState there.
var result_state:GameState = GameState.new()
In the second line you modify a property of that GameState.
result_state.name = "ResultState1"
In the third line, I’m not entirely sure what test_state.perform_move does, but I’m assuming it returns a reference to the owner of the perform_move function. Now result_state is a reference to a completely different block of memory on the Heap or the Stack.
At this point your GameState you declared on line 1 is Orphaned. Nothing has a reference to it , and you’ve caused a memory leak! You’ve allocated memory for the GameState but never tell your computer that you’re done using it using queue_free.
And later, when you call result_state.queue_free(), the node/object that you free is a completely different GameState. My hunch is that it’s the GameState you declared on your very first line.
var test_state:GameState = GameState.new()
AKA, you’re calling queue_free on the same object, twice!
test_state.queue_free()
result_state.queue_free()
Hope that helps! Memory management is a difficult topic in CS and Godot does somewhat obfuscate it. Here’s a link to what the docs have to say as well.
Hmmmm, that’s very insteresting! Thanks for the explanation.
The method perform_move returns a GameState with the new positions and state of the game (this chunk of the code is being used to simulate plays). Maybe it would be better to just assign the GameState that returns from the method?
var result_game:GameState = test_state.perform_move(result_moves[move])
That would work! Just keep in mind that right now you’re probably modifying and then returning the GameState that owns the perform_move method, in this case whatever is referenced by test_state.
If you want a different instance of a GameState you need to allocate one with GameState.new(). That way you could have one GameState the represents the board before the move, and another that represents the board afterwards.