Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am drawing this simple grid on my NodeJS server:

var grid = [];

for(var x = 0; x < 20; x++){
    grid[x] = [];
    for(var y = 0; y < 20; y++){
    grid[x][y] = 0;

    }
}

console.log(grid);

The outcome looks like this:

enter image description here

I know, pretty right?!

0 is supposed to indicate FREE, thus if a player requests to move to a field with 0, he will!

The problems come when I want to add more then just Free/Occupied, for instance I would like to give each Array Element an ID number for the Client Updates, or certain features on the field to be stored.

I tried to assign { something: N, something2: N}

But thought it looked rather performance expensive in the long run. (On a big Grid)

I read about using a single Object for several elements, but I cannot find this any more..

Should I perhaps use an Array of Objects, or an Additional Array inside each X,Y element?

Any performance / convenience / anything goes tips are welcome :D

Edit: Thank you for the ideas so far, I was now thinking to perhaps using Strings. Storing Variable_A + "," + Variable_B and then later using the .split to use the information. Any thoughts on this?

share|improve this question
    
Depends on what you store. If you just have a few flags, you might use a number and basic bit operations (see this if you don't know the concept). If you need to store more, then an object is fine. –  dystroy Jan 18 '13 at 10:36
add comment

2 Answers

up vote 2 down vote accepted

You seem to match this frequent use case :

  • a big map with each cell having a few characteristics
  • some objects being somewhere in the map

I suppose the map is big because you ask about performances. If the map is small, then both the question and my answer seem less relevant.

I usually use

  • a grid containing an int for each cell, this int being able to carry up to 32 flags (using bitwise operation)
  • a list of objects (may be an array or database records), each one having an x and an y

Not referencing the objects from the grid avoid coherency/race problems and is lighter.

A frequent practice is also to use a monodimensional array and to address the cells as a[x+W*y];. It makes many operation (like the cloning of the whole map or any range operation independent of the position) easier and faster. I'm not really familiar with node.js but I suppose the Buffer class should be used.

As I suppose you'll want to use a browser as final interface, here's a trick I use : I encode my maps as PNG, with each color of the palette simply being one of the possible flag combinations. This makes

  • storage and transmission very efficient (PNG is compressed)
  • map operations easy with image based operations
  • fast rendering at low resolution easy in the browser using image rendering
share|improve this answer
    
I fail to see how this is different then: Array[Array[Array]]]. –  Oliver Schöning Jan 18 '13 at 21:06
    
I am unfamiliar with Bitwise. I will read up on it, does it mean I could store more then one value in each Array Element? (32) –  Oliver Schöning Jan 18 '13 at 21:07
    
Bitwise operators allow you to store up to 32 booleans in one javascript number. Read this as an example (it's in java but it doesn't matter, it works the same). –  dystroy Jan 18 '13 at 21:08
    
It sounds to me like I am supposed to use Numbers; (example: 5) And then let the server figure out what 5 is supposed to do. I suppose that would be faster then an additional Array, perhaps I should open a new question about that? –  Oliver Schöning Jan 18 '13 at 21:09
add comment

You should absolutely use this approach:

{ something: N, something2: N}

Deciding to not use it, is a perfect example of premature optimization.

Just do the simplest thing that will work, and if it is slower or takes too much resources (this will more likely be the problem, not speed) you optimize from there.

People nowadays underestimate what a powerful beast they have in front of them. And I am not even talking about your dev PC, but of modern smartphones/tablets.

share|improve this answer
    
Just not sure if this would slow it down to much, I mean THIS approach could potentially create thousands of Objects just for a grid. –  Oliver Schöning Jan 18 '13 at 20:58
    
I see your Point though, And agreed, best way to learn is not over-think it all the time. –  Oliver Schöning Jan 19 '13 at 6:48
1  
Thousands of objects is not a problem at all. A compiled statically typed language like C# on a modern PC can cope with creating tens of millions of objects per second. My guess would be you will not have problems with hundred thousands of similar objects. –  Boris Jan 20 '13 at 0:29
    
Cheers. I'll try and see what happens I guess! (Tens of Millions of Objects.. Jesus Christ) –  Oliver Schöning Jan 21 '13 at 8:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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