Is it ok having a matrix of objects...?

… or it is not? or do exist better options?

in my case, i’m trying to make a little game, kind of a rogue like tile and turn based game. For now the map is only 15x15, though i’m not sure if it will change.
So, i’ve always used a list of objects but in this particular case i was wondering if there’s a better option, one that makes the programmation easier.

For example, i can easily check if there’s something in the upper tile just with:
if(objects[player_X][player_Y - 1] != null);

if i create por example an arrow to the right:
objects[player_X + 1][player_Y] = new Arrow();

or if a monster moves to the right:
objects[monster_X + 1][player_Y] = objects[monster_X][player_Y];
objects[monster_X][player_Y] = null;


I actually was thinking in a matrix of pointers, but i don’t really know if that’s what i have or i’m just programming some kind of abomination jajaja

anyway, any insight or recommendation? thanks!

If your world is a 2D grid of tiles then using a 2D array (matrix) is a perfect match.

As markus mentions, a 2D array would work well for you. In my current game I have a Grid class which contains a 2D array of GridCell objects.

One thing I can suggest is taking a look at the Point class. It’s a simple class that stores and X and a Y value and has several helpful functions and operator overloads (such as Equals) which should make your life easier. I use this class for players moving along the Grid in my game. Each character has a Point called GridPos which is used to identify where they are on the game grid.

Thank you two!
I’ve been programming using the 2d array and it really makes it easier and more intuitive, and yeah, the point class is great too, it really works great as in id!
My only “problem” is that i didn’t really decide if i wanted to allow more than one object in a position, por example, loot + an enemy, but i guess i can use a secondary array for loot, etc The game is still so green jajaja

Wanted to see your game gambill but seems you guys still didn’t show it, good luck with it anyway!

It’s sort of misleading to use the word “matrix” in that context; technically, a matrix is a table of numbers for linear transformations.
If you want multiple items per space, you could use a 1D list of objects, each of which stores their own location. A little more cumbersome when it comes to checking adjacency, but effective.

My suggested solution is to use a
Dictionary<Point, YourBaseClassHere>()
(maybe something other than a Point it’s up to you)
A Dictionary is to C# what a HashMap is to Java.

It uses Key Value pairs. You supply a key, it returns an object at that location.
You can then just do something like: var objectYouWant = yourDictionary[new Point(x,y)];
It’s efficient and flexible, and you can key by any type, and have values of any type, so your not limited to unsigned integers for lookup address.

thanks, using a dictionary seems a neat idea!

also, yeah ed022! I meant a 2d array. at the end, since with the exception of pickable objects (like coins, etc) i’m gona stay with the one element per position, i made a 2d array por those elements and another one for the pickable ones.

If i can ask something else here, what about recursivity? is it “acceptable” in videogame programming or should i look for an alternative?
for example, when i want to move an enemy but its movement depends of the next enemy, whose movement may depend of another one, etc

That kind of recursion is fine. I have not used a single programming langue that recursion is not fine, unless you have to deal with serialization.
C# will serialize recursion without issue though.