Get Nearby Actors from Binary?

Hi, I’ve been through the process of creating a game similar to Minecraft and I’ve managed to create a system which saves multiple blocks to binary… So far for the purpose of optimization, I created a system which uses a For Loop to loop the binary array of blocks, checking whether each block is within range of the player and spawning that block to reduce lag. The problem is that this for loop is very CPU costly, and I’m wondering if there is a way to loop through blocks in binary that are in the range of the player instead of having to loop through the entire binary array.

Wehre do oyu hold this block information and how do you index it?

When the game starts, the player runs a For Loop, which saves a bunch of transforms for each block into a binary array which is stored as a file. These blocks essentially make up the terrain at which the player can walk on. However, for optimization, I cannot have the blocks spawn and be rendered all at once, so I made a block of code which loops through the generated array of blocks on the hard drive, checking the distance between each block and the player. If the block is within range, the block can be spawned. I’ve got everything working so far. However, the issue is that that For Loop which is used to check the distance is very CPU costly, I want to find a way to loop through the blocks only around the player instead of the whole map.

The blocks are indexed by depth, meaning it spawns blocks downwards before moving to the next column, then to the next row.

There is a mathematical concept for storing 2D/3D array linearly where the distances remain relatively close. See Wikipedia article on Hilbert curve. However these curves have some edge cases and seams which would need to be handled separately.

On top of this it would be largely beneficial if you divide up your data in big chunks (quadrants) and check only the 4/9 quadrants that are closest to your player to avoid checking blocks that are sure to be too far away.

Thirdly: Spawning is a very CPU and memory expensive process. This is also the case with reading from the HDD. Try to avoid doing these things by:

  1. Instead of spawning - move your blocks that have exited the view range and place them in place of the “newly visible” blocks by just modifying them;
  2. Ignore blocks that are fully covered by other blocks;
  3. Make the loop run asynchronously if possible and make it feed the player whenever it is ready. You will have a delay but you would take the blocks in larger area and then show only a small potion of it;
  4. Load large chunks from your binary file into your game to avoid constant read from the HDD.

I would really suggest you divide your “for” loop in “Reading file”, “Checking distances” and “Placing new blocks” and check which takes up the most time.

I can really bet that the “Checking distances” is the lightest of these processes.

I managed to solve my problem with some other method in the most painful way possible, but thanks so much for telling me about Hilbert’s curve, I’ll definitely look more into it :slight_smile: I will try your suggestions to optimize it even more.

If you’re wondering how I solved it, I added another binary array storing the number of each block as “Block IDs”. I then put a trigger box on top of each block. When a player walks into the trigger, it gets the IDs of the blocks around the block that was stepped on, using some math and spawns those.

Thanks anyway for the potentially helpful answer C: