I am making a 2d tile based game in C# and XNA 4.0. I am having trouble loading and unloading "chunks" of tiles(blocks). The whole world is randomly generated and is infinate on both axis. How would I go about loading and unloading chunks of tile data in the camera's view?

A pastebin to the pastebin links(I still have the 2 link cap):

http://pastebin.com/9PYr8cvC

• World class: pastebin.com/K9Qfzh2z Chunk class: pastebin.com/sCknxzig Camera class: pastebin.com/JMueU0HN Player class: pastebin.com/tm4kUQUb – ClassicThunder Mar 5 '12 at 21:21
• There's probably not enough info you gave there. Unless XNA actually has that pre-built, people don't know how you make chunks and so on. – jcora Mar 5 '12 at 21:31
• @Bane In World.cs there is a method called GetChunk(ChunkLocation location), And a *Location is a Vector2 with custom helper methods. Generator.cs: pastebin.com/PkrnZteN – ceriosNerd Mar 5 '12 at 21:41
• I get the picture, but know that I never worked with XNA. :) – jcora Mar 5 '12 at 22:14
• Kinda of already had this question here. – ClassicThunder Mar 6 '12 at 0:55

Basically you want to have a range around your camera. When chunks come into this range, you load/generate them. When chunks leave this range, you save/unload. Keep in mind you'll want to keep the loaded range larger than the visible range, so your chunk loading isn't seen.

At the moment it looks like you're storing your chunks in a dictionary. That's kind of a strange choice, and it may be a little more work for you to maintain your chunks with that data structure.

It looks like you've got a lot of the functions you need already, nice work. You'll want a list of chunk positions that should be loaded, your loaded list. Your update loop is going to maintain that list. As the camera moves, you update the list to include all the chunks in the range of the camera.

It would look something like this:

Starting with this scenario, where the red dot is the camera, moving in the X plus direction. All the grid squares (chunks) touching the green area are currently loaded.

Then the camera moves far enough to get some new chunks:

All the blue chunks will be loaded/generated. All the red chunks will be unloaded/saved.

As for maintaining the list, you can update whenever the camera moves past a chunk boundary. Like if it moves past the X plus boundary of the chunk it's currently over, like in the example above, the chunks to be added or removed from the list could be found like this:

for(int y = Camera.Position.Y + LoadRange.Y; y >= Camera.Position.Y - LoadRange.Y; y--){
}


Where GetChunkRootPositionAt converts a world position into the root position of the chunk that contains that location. And the LoadedList functions take a Vector2 to add/remove from the loaded list.

• That's close to how the dictionary is currently being used. But the key is ChunkLocation, which I'm sure at least has the x,y(z). The reason for the linked list (or similar structure) is it's easier to keep track of where things are. For example, it's harder to iterate through all the chunks on the X-minus side of the world with a dictionary, since you'd first have to look through the entire dictionary to find all the chunks there. I think it'll save headaches in the future, but that's just my opinion. – MichaelHouse Mar 5 '12 at 23:36
if(tile is inside of the window)