Grid based movement optimization

Godot Version 4.7

So I haven’t quite started coding it yet, but I’m trying to figure out grid based movement (think Mega Man Battle Network) in 3D. I’ve done this once before in Unity in 2D, but I feel it’s rather unoptimized.

I would store each cell (fixed amount of 18 cells) in a Dictionary, every 3 cells is a column on the grid (so starting top left on the grid and going down, 3x6 grid). To move, I’d take the index of the cell my player is currently on and based on the direction, I’d add or reduce an amount from the index, get from the dictionary the required cell, take it’s global position and then move the player to that position. This did require my player (and my enemies) to always have a reference to the dictionary. Also, I’d need to check every time if the direction I’m moving is valid by checking if the required index isn’t out of range.

This worked for it’s purpose at the time, but I feel it’s a little convoluted. There should be an easier way, no?

Any input is appreciated!

EDIT: It just came to me how some games like Crypt of The Necrodancer or Fire Emblem are able to move up down left right in any setup of their grid. My implementation feels very specific to my setup, since I already know what size my grid will be. How can I achieve the same thing as those other games?

Converting a list to a grid this way does not seem like a big issue to me (depending on the goal).

But the piece of info I’m missing is the Dictionary value: what data structure does your cell actually hold? Is that the thing that seems convoluted to you? Could be, but hard to tell. If it’s only a global position it may be a bit overkill… Then again, maybe not.

I’d suggest, just build your system and share the code for review here. I’m more than convinced you will then get the feedback you need.

The Dictionary would hold a number from 1 to 18, this way I can easily find the desired cell by calculating and then searching for it as a string. The value would be a reference to the cell node. This might be too much though, as I could just use the index in an Array instead, but I like Dictionaries more.

I guess what I find convoluted is the way the movement works. Having to call a Dictionary, then get the specific node and calculate the move based on an index seems unoptimal, at least to me. I was imagining maybe a raycast or something else could be used instead? In the sense that it could check the cell immediately next to it instead of looking through the Dictionary?

I guess I’m mostly worried about optimization, but I’ll work out the code first and update the post when I can.

By what metric?
Watch out not ot succumb to premature optimization.

After reading the linked article, it seems I have fallen for premature optimization haha. I haven’t written any code yet and I’m already trying to optimize something that doesn’t exist.

My previous attempt in Unity was pretty much (working) spaghetti code, so I guess I’m including this when I think about how to go about my current project.

I believe in just jumping in and making mistakes. Learn from it then. Fear of failure will only block you.

  1. Make it
  2. Share the code
  3. Read the reviews
  4. Fix it and feel confident

Of course you can’t do that with everything, but this is small enough for a forum.

Good luck!