Optimization when using a 2-dimensional array of objects

Liam McGillivray yoshi.pit.link.mario at gmail.com
Sat Mar 23 04:19:54 UTC 2024


On Friday, 22 March 2024 at 07:34:33 UTC, monkyyy wrote:
>> Is one option more efficient than the other?
>
> You should probaly do the lazyest thing, factor out your 
> "ispassable" logic, like what your walking n of 3, n of 8, n of 
> 15? so long as you dont do something insane it will be fast on 
> a modern computer; allocating several dynamic array that are 
> the size of your game world every frame could easily be not 
> very sane.

Well, none of the stuff you wrote closely resembles the code that 
I have.

There are 3 reasons why I put this kind of effort into 
optimization:
- I'm obsessive.
- For the learning experience.
- Because things may get more demanding when I get further with 
the enemy AI system. One possibility is to have it make multiple 
copies of all the game objects which it will use to look ahead to 
the next 1-2 turns.

> and if you really really wanted to care, you could precompute 
> the "connected compoints" by flood filling across passable 
> tiles with a "color" of 0, then finding an empty cell, flood 
> filling with 1, etc.; and when you draw the overlay for where 
> you can move you can do a heuristic check for a) they are in 
> the same component, and b) the manhattan distances before c) 
> doing a greedy check

I barely understand any of this, though I know what a Manhattan 
distance is. Is this about measuring distances? Manhattan 
distances appear to be how distances are determined in Fire 
Emblem, though I'm using a slightly more intensive `abs(x) + 
abs(y) + max(abs(x) + abs(y))` whenever I need a quick estimate 
of distance that doesn't account for obstructions.

>> Is there a memory allocation technique that would make each 
>> tile's location in grid inferrable based on it's memory 
>> address?
>
> Yes its called an array
> theres some details you need to know and you need to cast 
> pointers; just try some trial and error with code like:

But objects are reference by default. This means that they don't 
really 'live' in the array I put them in, doesn't it? Wouldn't 
the the array entries just be references on the same level as any 
other?

> ```d
> int[10] foo;
> &foo.print;
> &foo[1].print;
> (&foo[7]-&foo[0]).print;
> ```

This appears to be a different programming language. It isn't D.


More information about the Digitalmars-d-learn mailing list