GML Visual [Solved] 'Click to move' pathfinding ?

P

percev4l

Guest
Hello !

I am a graphic artist, trying to learn GMS2 for making some little prototypes.
Since I doesn't have any notion in coding, I am starting working with
drag & drop.

I am trying to make my character moving like in a basic point n click, finding the path on any mouse click, avoiding solids and other characters.

But I don't know which actions to use.
For example, I tried to connect the coordinates of my mouse click with the "Star Following Path" action,
but I don't know how to do that, and I have the impression that it is not the good process.

Could someone give me some advices ?

Thanks !
 

Kyon

Member
CREATE
Code:
xx=x; yy=y;
STEP
Code:
move_towards_point(xx,yy,5); //moves to xx and yy with a speed of 5
if mouse_check_button_pressed(mb_left) //if you're clicking with your left mouse button
{
xx=mouse_x;
yy=mouse_y; //update coordinates
}
start with something like this I guess.

I would recommend tutorials on youtube too, they are great.
 
F

Frozen Stick

Guest
I believe there was an action for this kind of behavior...

Well first make an event Mouse - Global Mouse - global left pressed.
in that event drop the action in the move tab named step towards point:

I hope this helps!
 

rIKmAN

Member
I know you said you are using DnD, but if you wanted to try dipping your toe into actual coding then there is a great tutorial blog with an example project here: https://www.yoyogames.com/blog/459/dynamic-mp-grids

It may seem daunting at first, but you are having to spend time learning DnD which eventually you will want to move away from, so might as well spend that time following this and seeing how you get on.

Feel free to post any question you have if you get stuck while following it, loads more people here will be able to help you with code problems as not many users here seem to use DnD - I personally find it more confusing than coding funnily enough,
 

CMAllen

Member
Using D&D to build a click-to-move? I accept your challenge.

First, you'l want a global mouse_left_down event. In that event you'd want to assign the position of mouse_x and mouse_y to two variables, which we'll call clickx and clicky respectively. These two variable tells the player object where it needs to go.

In addition, you're going to want to set the value of another variable to the distance between the player and clickx and clicky. We'll call this variable move_dist and we'll give it a simple function for its value
Code:
point_distance(x, y, clickx, clicky)
This is so the player object knows how far it needs to move.

In the step event, you'd drop in a set point direction from the movement tab, giving it the clickx and clicky values. Now that we've told the player object the direction to where it's going, we need to tell it how fast it should get there. For that, drop in a set speed from the movement tab as well and give it another simple function for its value
Code:
min(move_dist, 5)
This clamps the speed to the lowest value of either the distance to the final destination or 5 pixels per step. The value of 5 can be whatever you want, and higher values result in faster movement.

That should do it. As long as the left mouse button is down, the player will move towards it. Once the left mouse button is released, the player will continue moving towards the last place the mouse button was still being held. If you want the player to immediately stop moving when the left mouse button is released, add in a global mouse_left_released event and drop in one assign variable for move_dist again, and this time we're just going to set it to 0. And that's it. Since the distance the player needs to move is set back to zero when the left mouse button is released, when the speed value is calculated in the step event, it will pick the value of 0 which means no speed and no movement.
 
P

percev4l

Guest
Since most of you recommend to invest time into coding instead of D&D,
I started learning GML with some tutorials and docs, and indeed that way seems to be better !

It is always complicated for a total beginner to know where to look first when you learn from zero,
but it's really fun and I understand things progressively :)

Thanks a lot for all of your advises and answers, that helped me a lot !
 
Top