x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.20
Tags:
more ▼

asked Jul 29 '18 at 09:59 AM in C++ Programming

avatar image

gamnuke
3 1 2

avatar image Shadowriver Jul 30 '18 at 12:34 AM

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

avatar image gamnuke Jul 30 '18 at 05:34 AM

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.

avatar image gamnuke Jul 30 '18 at 05:40 AM

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

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 30 '18 at 08:19 AM

avatar image

dZh0
1.2k 6 5 8

avatar image gamnuke Jul 30 '18 at 10:18 AM

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 :) 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:

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question