• 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!

Searching for a tutorial on object movement: How to move objects by selecting a destination?

M

MMGames

Guest
Hi guys,

I would like to program some point-and-click-like game. I am having a hard time to find out how to move units there.
So basically, I select the worker and then I want to click on the tree square to move it there. I am having a hard time with the logic behind this. Tried to find tutorials online, but couldn't find any.

moving units.jpg

Any resources you can share that might help me?

Thank you.
 

NightFrost

Member
If the idea is to create grid-aligned movement, you can use the mp_grid functions to set up the movement grid and calculate paths across it. Then it is a matter of having the worker follow the resulting path.
 

Felbar

Member
if you just want linear movement you could use lerp

if you want to move on the grid with obstacle avoidance your going to have to look at pathfinding tutorials

GML:
//in Create event
dest_x = x;
dest_y = y;

//in Step event
//change last value for speed this is 0.10 or 10 percent per frame
x = lerp(x , dest_x, 0.1);
y = lerp(y , dest_y, 0.1);

//in Global Left Pressed event <-- note must be global left pressed
dest_x = mouse_x;
dest_y = mouse_y;
 
M

MMGames

Guest
I actually am using the DnD version since my development skills are not so good... but thanks, I will check all the info you posted here and lets hope that helps me at least get in the correct track.
I was having issues with the logic itself. How can I give the worker a destination if the moment I click on another element, the focus goes away from the worker?
 

Felbar

Member
Thats why I used 'global left pressed' it lets you know when the mouse button is pressed regardless of where
or on what object, there is most certainly a DnD equivalent for that event.

You could do something like left click for selection, right click for movement , or ctrl-left click etc.
 
M

MMGames

Guest
You could do something like left click for selection, right click for movement , or ctrl-left click etc.
I think that would be the key, using the different buttons for different purposes. I just need to be carefull with the menu buttons.
Is the global left also activated if you click on other objects that have a left_click event? I'm learning yet.
Maybe left_click can save the object_type and Id (Each object could generate automatically and ID) and then with right-click, use the "if objects exists" and then check for the ID, so if the ID matches, move the objects to the new coordinate saved when right_click.

Well, at least now I have an idea where to start from.
Thank you all!
 
Top