• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Visual Grid based movement with drag and drop editor

D

d3signmonk3y

Guest
My son and I are trying to create our first game in Gamemaker Studio 2 and for some reason I can't seem to figure out how to do something that should be relatively simple—my programming chops are pretty rusty. We want the movement to be grid based, so that when you press an arrow, the character walks to the next grid space in that direction then stops. If I put the code on an arrow press event on the object he just jumps to the ending spot.
 

TheouAegis

Member
There are a couple ways.

The simplest to understand is to set an alarm (or use a custom timer, if more familiar with other languages) when a direction is picked IF THE ALARM IS NOT ALREADY COUNTING DOWN. Then in the alarm event (or when your timer reaches 0), set your speeds to 0 and realign the instance to the grid.

Another method that doesn't use alarms requires pixel-perfect movement, meaning no overshooting the destination. That means if your grid is 16 by 16, you can easily move at speeds of 1, 2, 4, or 8. You can possibly move at speeds of .5, .25, or even 4/3, all of which should round off cleanly to 16. You then check if floor(x-h) and floor(y-v) are aligned to the grid, where h is the horizontal offset of the origin and v is the vertical offset of the origin relative to the grid. So if your origin is grid-aligned, h and v are 0; if your origin is in the middle of a cell, h and v would be 8.
 
I've also used state machines for this in the past, combined with some version of tweening. State waiting lets you input the direction, figures out the coords for where you are heading to then changes to state moving, where it tweens to the target coords and then resets to state waiting once the target has been reached.
 

Slyddar

Member
Here's another method. You could create a path between your current position, and the target location, then assign your character object to the path and change his sprite to the walking animation. Create a path end event that changes the sprite back to the idle sprite. When you use path_start() to start the path, you can set the speed and end action, which should be stop.
 
D

d3signmonk3y

Guest
Thank you all for the responses. I'll try to get it working tonight!
 

Sabnock

Member
If you have done coding before I would highly recomend dropping d&d and start getting your boy into learn GML. I started with the d&d but took the leap and am very happy that I did. You'll get far better support on the forums as well.
 
Top