I'm building a game using a 3d grid based, procedurally generated world (same world concept as Dwarf Fortress/Towns).
For the Blocks i use a 3D array of Block instances (normal class, not GameObject), like this: Block[,,] blocks = new Block[sizeX,sizeY,sizeZ]. The block object contains all relevant block info like positions, and bools like "isWalkable" and vertice positions for rendering (they are then combined into "Chunks" for rendering) This runs quite well, even with a million Blocks (128x128x64)
My question: Would it be better to store the data in seperate arrays? For example have arrays "BlockWalkable[,,]", "BlockVerts[,,]"...
This way i could get rid of Objects.
I'd like to get rid of as much overhead as possible to make room for implementing pathfinding later... Would there be any advantages for lookups for example? Or memory savings? I woult test it but it would require quite a large rewrite, so i thought i'd ask first.
asked Feb 20, 2012 at 09:46 AM
I do not remember the numbers exactly, but the overhead for having a class in say C# is in the magnitude of a few bytes. If you have a few million objects that makes a few megabytes, it could still be tolerable on the PC. Depends a lot on your memory budget.
The performance may suffer if you use methods (and I guess properties count as methods) to look up values as opposed to just look up a value in an array.
However, my opinion is this: the readability (and maintaining and testing) of your code will likely be much harder if you do these optimisations. So if it was my project, I would definitively do some profiling to identify the bottlenecks before I start optimising.
answered Feb 20, 2012 at 10:05 AM