Grid Based Map Object-Array vs Arrays only

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.

more ▼

asked Feb 20, 2012 at 09:46 AM

avatar image

344 45 35 64

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

1 answer: sort voted first

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.

more ▼

answered Feb 20, 2012 at 10:05 AM

avatar image

773 5 8 13

Thanks for the information. I think I'll leave it as it is for now (with Objects) and if i REALLY need to optimise something I'll look into it again.

Feb 20, 2012 at 11:36 AM maikkel
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

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



Answers and Comments



asked: Feb 20, 2012 at 09:46 AM

Seen: 1201 times

Last Updated: Feb 20, 2012 at 11:36 AM