In the [game I am currently making](https://github.com/LiamM32/Open_Emblem/blob/master/oe-raylib/source/app.d), I have a `Map` class (actually a combination of an interface & class template, but I'll call it a "class" for simplicity), in which there will probably be only one instance running at a time. In that map class is a 2-dimensional dynamic array of `Tile` objects called `grid`. What these `Tile` objects represent are like squares on a chessboard. Their placement in `grid` represents their location on the map.

Whenever a function in the game has to pass a location on the map, I have a dilemma between passing a reference to the tile object, and passing it's grid coordinates through `struct Vector2i {int x; int y;}`. The functions that receive this information sometimes must access the Tile's variables to know it's properties, but may also need to know it's location so that it can find neighbouring tiles. The `Unit` class also has a 2-dimensional dynamic array of entities holding information on the unit's distance from that tile, and whether it can reach it during this turn.

Is one option more efficient than the other? I already have the location of each tile cached as a pair of variables within each tile. To get a location from a Tile object, a call would be made to the member function `Vector2i location()`. To get a tile object from it's coordinates, a member function of `Map` `Tile getTile(Vector2i location)` would be called. Should I stick to one over the other?

Is there a memory allocation technique that would make each tile's location in `grid` inferrable based on it's memory address? It would be nice if the two were interchangeable, so that I wouldn't have this dilemma.

Not knowing which option is best has lead to a little bit of messy syntax. Right now I have multiple versions of the functions `getReachable` & `getAttackable` in the [`Unit`](https://github.com/LiamM32/Open_Emblem/blob/master/source/unit.d) class which return a different type, because I'm not sure which is better.

Reply via email to