0
\$\begingroup\$

I have a 2D grid based movement. To move, I stop key inputs until the character has reached a tile. Because of this, if you make a new directional input midway from one tile to another tile, nothing will happen as the unit has not reached the tile yet.

This seems like a fundamental challenge for grid based movement and I am wondering how people generally handle it, if they handle it at all.

One solution I had was to make the tile sizes smaller (From 32x32 to 16x16) so that the distance between each tile was smaller, but I don't really want to use 16x16.

Thanks

\$\endgroup\$
  • \$\begingroup\$ I don't understand the issue you are having. You want grid-based movement (OK). You ignore midway inputs from one tile to another (OK, this is how grid-based game work, not a fundamental challenge). Then the solution you mention is as if you didn't want grid-based movement in the first place. \$\endgroup\$ – Ferreira da Selva Aug 11 '18 at 22:08
1
\$\begingroup\$

One way to solve this is to use an action queue.

You have a queue of actions, that you execute every time the unit is idle.

When the user presses button, you enqueue the action "Move Left" and then in your units update logic you dequeue the action and perform it.

This way the player can keep adding actions and the unit will keep executing them.

For basic player control logic, you would probably not allow a player to constantly add new actions, and rather only let him add an action if there are no other actions queued up currently.

This system works both for player control, but also for AI control. It's generally very flexible and can be expanded on greatly. If you understand how stacks work, they might offer some other interesting advantages. For instance, you could enqueue a pathfinder action that will pop itself off and replace itself with the necessary moves to reach it's destination.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. I think the queue option with only 1 element in the queue is equivalent to what im currently doing. There would still be an issue of waiting until the unit is done moving to the next tile before allowing more user input. \$\endgroup\$ – shell Aug 10 '18 at 17:43
0
\$\begingroup\$

To solve this you simply need to remember the last input that was given during the transition. Once you reach the end of the move, then you perform the action that is remembered. However if the user simply holds the button down as he moves it should requeue the action until the end of the movement, in case the user was not intending to more further than the next tile.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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