Search code examples
c#godotgodot4

Creating chunking system in Godot with tilemaps. Interesting problem with loops


Godot: v4.1.1 Built using Forward+ mode

Some background: I have a chunk manager class that has a Dictionary<>, ActiveChunks (which I should probably change to a List<>), and a Queue, InactiveChunks, so that I can hibernate old unused chunks so that they could be recycled later. As of now, I have the entire in-game map set to be active in the manager subscriptions as I haven't implemented world anchors yet (I want to get a good handful of other things done first, such as inventory which I will probably use to implement that).

The problem: In the manager _Process method, I have a while loop that is supposed to count down to 0 from the number of chunk subscriptions minus the number of active chunks as it adds on TileMap UIs from the InactiveChunks queue, but it instead counts down by one and then the count gets stuck in some sort of infinite loop where it no longer decrements, but there are no nested loops inside and it doesn't seem to leave the loop. I am sort of at a loss for words.

I managed to replicate this issue in a test project, which can be found at the link below. Hopefully this will be easier read and work with than trying to paste all the code here.

https://github.com/stevetheburger/ChunkTest.git

If one has issues trying to obtain the code, please let me know.

Thanks.

P.S. Updated dictionary to be list. Different flavor of problem. May self resolve tomorrow. See comments for details.


Solution

  • Turns out that a local variable was being assigned to the data and C# was doing it by reference without me knowing it. Had to step away for a while and come back to realize it. Still no idea why the dictionary doesn't work, but I don't need it anyway.