Hey all, Rioku here with a scripting question.
I currently have a triple for loop that I’m using to check if chunks exist in my voxel game. So, it loops through x,y,z and generates chunks accordingly. That’s all fine, I can maintain a steady framerate without any issues using this method.
However, the problem is that the loop isn’t fast enough to keep up with player movement. The player can very easily walk out of the world, because the chunks don’t generate around the player correctly. So what I’d like to know is how to generate chunks outward from the one the player is in rather than in a square. Or just simply a way to prioritize chunks so that the closest are generated first.
The way my generation and detection system works currently is that I have a seperate thread that loops through the x,y,z coordinates in chunk space around the player. It checks the world data, and if the world currently has the chunk requested already loaded, it’ll skip it. Otherwise, it’ll add a new set of coordinates to a list. Then, in a coroutine, we grab the first set of coordinates in that list, and generate a chunk there. Then it removes those coordinates, and the world manager adds the new chunk to it’s internal data by default.
Another small problem is that my thread will sometimes stop randomly. I’m pretty sure the reason is that it’s checking the world data at the same time that a new chunk is being created and added to the world, so it’s throwing an error and stopping the thread. It doesn’t actually log the error, but I’ve had problems with modifying lists at runtime before. I was thinking of making the coroutine wait every second or two, and letting the thread run, then going back to the coroutine. That way only one of them is running at a time. Thoughts?
Any help would be greatly appreciated! Once I get past this, I can move on to the more interactive parts of the game, like modifying the world and such, and fixing up terrain generation to look more pretty.