I have a class TileChunk which at it's most basic contains a rectangle that surrounds the chunk, and four bools isLeftLoaded, isTopLoaded, etc. Using the rectangle I can determine which chunk the player is in.

What I want to do is create some kind of method which checks the current chunk the player is in, and if nothing is loaded then create new chunks that surround the sides that haven't been loaded. The problem I'm coming across is that using the four bool variables doesn't seem to work correctly. At initialization, I create 9 chunks to make a 3x3 starting area but some of the bool variables aren't being set correctly.

My question is how should I write a method which determines what chunks have loaded chunks adjacent to them? Every chunk is stored in a List, but other than using the bool variables I'm not sure what is the best way to do this.


The general design is problematic, you might be able to get it to work, but it will be unnecessarily tricky.

Instead give chunks coordinates that correspond to their position in the game world in chunks. If they are all the same size or multiples of a common size, then figuring out which world position corresponds to which chunk will be very easy.

Now you could have a list of chunks that exist (preferably a hash table with the coordinates as keys). And depending on the player position just check if the chunks he should be seeing exist. If all of them exist you're good, if one or more of them don't exist create them and add them to your list. That way you also won't have to initialize a 3x3 starting area.

A problem with your system would also be that if you somehow teleport the player somewhere. Suddenly there might be no existing neighbour nearby, so how could you deal with that?

  • \$\begingroup\$ They do have a variable Position which is the top left corner that I can use. \$\endgroup\$ – curry pajeet Aug 5 '16 at 4:50
  • 1
    \$\begingroup\$ But I think I get what you're saying. If I can get the key of the hashtable by using my player position, I can loop through the table and if a chunk doesn't exist at on offset to the left or right of the chunk I'm in I create that chunk \$\endgroup\$ – curry pajeet Aug 5 '16 at 5:28
  • \$\begingroup\$ The point is that you can make it depend on the world position and not on what neighbours what. So instead you can generate everything that is visible to the current camera or whatever you prefer. \$\endgroup\$ – Nils Ole Timm Aug 5 '16 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.