Thursday, March 31, 2011

Threading and a new video.


I've fixed the threading issue I had.  The solution came in the form of compartmentalization.

Before my change, I had a giant allCubes Dictionary that stored every single cube in the game.  There was another Dictionary called _terrain that stored each chunk by it's Vector3 location.  The chunk class had a list of cubes that cross-referenced the allCubes Dictionary.  Sounds more complicated than it is.

Anyway, whenever I added/removed a cube or chunk I had to update that huge dictionary.  When you have 2 million cubes in the dictionary, you're going to be referencing it constantly.  This is why adding/deleting from it was causing so many issues.

I could have written a thread-safe dictionary or I could have used ConcurrentDictionary, but I felt that having the giant allCubes dictionary was a bad approach to begin with.  By moving those cubes into the chunk class and referencing them from there, I've compartmentalized the areas that I'm constantly accessing.  I can add/remove cubes from each individual chunk without bothering another one.  That, my friends, is good business.

I didn't do this from the beginning mainly because I was short sighted.  Simple as that.

I also fixed my texture issue.  Switching my sampler from a linear wrap to a point clamp stopped the shader from reading too far out.

Now that chunk loading/unloading has been completed, I'm going to work on adding/removing cubes.  I've been thinking about it a lot and I think I'm going to add a Dictionary called changes inside of each chunk class.  I'll use the Vector3 position of the change for the key and create a change class that will store the type of change.  That way I only have to save that changes dictionary and reference it when I rebuild the chunk each time.  There's no need to store the entire chunk when I know it's going to be consistent.

I'm also going to keep working on the character controller.  I don't know why but I hate programming a character controller.  Everytime I go to start it I find something else I can do.  Eventually I'm going to want to go past Version .01a though and I can't do that without a character controller.

So here's the latest terrain video.  No audio but at least it's pretty.


Sunday, March 27, 2011

Threading issues..

I'm having an issue with unloading chunks and it's related to threading.

When I unload a chunk, I need to remove all the cubes from the global cube pool.  I'm currently storing those cubes inside a public static Dictionary<Vector3, cube> named, appropriately, allCubes.  allCubes is inside of a static class named gameData so I can access it from wherever I need.  The Vector3 key in allCubes is the location of the cube itself.  So to make a long story short, the cube at 0,0,0 has the key of Vector3(0,0,0) and I reference it using allCubes[Vector3(0,0,0)].whateverINeed.

Most of the time, all I'm doing is reading this Dictionary and adding to it when I create a new chunk.  Now that I'm working on unloading chunks, I need to remove the cubes from that Dictionary.  If there was no threading, it would be a simple matter of removing it and not thinking twice.  Unfortunately with threading, life is not that easy.  If I pull a cube from the dictionary while I'm iterating through it, I'm going to have an exception thrown.  I've been trying to use the lock() method but that doesn't seem to help. 

Anyway, I thought I knew enough about threading to get by, but clearly that's not the case.  I'll be focusing on that for the week and probably won't go forward until I figure this out.

So until then..

Quick update with textures..


Thought I'd post a quick update.  My textures aren't the greatest but it's better than all 1 grass texture.