Jump to content


Photo

Trying to stop path


  • This topic is locked This topic is locked
8 replies to this topic

#1 Masterjohn74

Masterjohn74

    GMC Member

  • New Member
  • 358 posts

Posted 17 February 2012 - 03:10 AM

I am trying to stop the original path it is going on and then use another path.
What this code does is supposed to make the unit go to an enemy but if the player wants to it can back out. i am mostly having problems with exiting the path that moves towards the enemy and starting a new one.
if instance_exists(P_enemy)=true
{

can_attack=1
}
if can_attack=1
{
if mouse_check_button_released(mb_right) && collision_point(mouse_x,mouse_y,P_enemy,true,true)=true
{
target=instance_nearest(mouse_x,mouse_y,P_enemy)

        mp_grid_path(myGrid,myPath,x,y,target.x,target.y,1);

        path_start(myPath,unitSpeed,0,false)
}
if mouse_check_button_released(mb_right) && collision_point(mouse_x,mouse_y,P_enemy,true,true)!
=true
{
goalx = floor(mouse_x/32)*32+16;
goaly = floor(mouse_y/32)*32+16;
scr_formCircle(goalx,goaly)//just to move towards a point on the grid
}
if distance_to_object(instance_nearest(x,y,P_enemy))<
500
{
target=instance_nearest(x,y,P_enemy)
mp_grid_path(myGrid,myPath,x,y,target.x,target.y,1);


path_start(myPath,unitSpeed,0,false)
}

}
I am using a script to set where the object is going by using path_start

Edited by Masterjohn74, 24 February 2012 - 02:42 AM.

  • 0

#2 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 17025 posts
  • Version:GM:Studio

Posted 17 February 2012 - 07:57 AM

Okay, well there are a number of problems I see... The first is that I wonder how likely it is that a player will get the mouse to have the EXACT same x/y coords as the enemy target? Not very... AND your way of getting the enemy target id using the same code is all wrong. So, instead of checking the mouse x/y, use point_distance() to check how FAR the mouse is from the P_enemy and if it is less than a certain amount (5, 10 whatever) then do something. As for the traget part, just have target=instance_nearest to wherever the mouse is.

PS: You have a lot of redundant code... why check the mouse button twice? Just do it ONCE then run the code if true!
  • 0

#3 Masterjohn74

Masterjohn74

    GMC Member

  • New Member
  • 358 posts

Posted 17 February 2012 - 10:57 PM

Okay, well there are a number of problems I see... The first is that I wonder how likely it is that a player will get the mouse to have the EXACT same x/y coords as the enemy target? Not very... AND your way of getting the enemy target id using the same code is all wrong. So, instead of checking the mouse x/y, use point_distance() to check how FAR the mouse is from the P_enemy and if it is less than a certain amount (5, 10 whatever) then do something. As for the traget part, just have target=instance_nearest to wherever the mouse is.

PS: You have a lot of redundant code... why check the mouse button twice? Just do it ONCE then run the code if true!

But will that stop the path?
  • 0

#4 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 17025 posts
  • Version:GM:Studio

Posted 17 February 2012 - 11:03 PM

Eh? When you call mp_grid_path, it stops the path and replaces it with a new one. Other than that just call path_end()...
  • 0

#5 Masterjohn74

Masterjohn74

    GMC Member

  • New Member
  • 358 posts

Posted 18 February 2012 - 12:15 AM

ok thanks
  • 0

#6 Masterjohn74

Masterjohn74

    GMC Member

  • New Member
  • 358 posts

Posted 18 February 2012 - 02:27 AM

Eh? When you call mp_grid_path, it stops the path and replaces it with a new one. Other than that just call path_end()...

I tried changing the code around but it still does not work
if instance_exists(P_enemy)=true
{

can_attack=1
}
if can_attack=1
{
if mouse_check_button_released(mb_right) && collision_point(mouse_x,mouse_y,P_enemy,true,true)=true
{
target=instance_nearest(mouse_x,mouse_y,P_enemy)

        mp_grid_path(myGrid,myPath,x,y,target.x,target.y,1);
        path_start(myPath,unitSpeed,0,false)
}
if mouse_check_button_released(mb_right) && collision_point(mouse_x,mouse_y,P_enemy,true,true)!=true
{
goalx = floor(mouse_x/32)*32+16;
goaly = floor(mouse_y/32)*32+16;
scr_formCircle(goalx,goaly)
}
if distance_to_object(instance_nearest(x,y,P_enemy))<500
{
target=instance_nearest(x,y,P_enemy)
mp_grid_path(myGrid,myPath,x,y,target.x,target.y,1);

path_start(myPath,unitSpeed,0,false)
}

}


  • 0

#7 Masterjohn74

Masterjohn74

    GMC Member

  • New Member
  • 358 posts

Posted 23 February 2012 - 06:00 PM

Still Need help
  • 0

#8 torigara

torigara

    GMC Member

  • GMC Member
  • 6485 posts

Posted 24 February 2012 - 12:01 AM

&& collision_point(mouse_x,mouse_y,P_enemy,true,true)=true

The first part is never executed because collision_point never returns true. It is advised to look up the function again and read the general description located at the top of the page. This sentence is right above the description of collision_point:

All these functions return either the id of one of the instances that collide, or they return a negative value when there is no collision.

You could (and should) just use the returned id instead of taking instance_nearest.

Other potential problem lies in the structure of your logic. It basically reads:
if (you release the mouse button on an enemy) {
    start a path to it
}
if (you release the mouse button elsewhere) {
    execute a script
}
if (there is an enemy near the object) {
    start a path to it
}
Even if you manage the first if block to work, the third one will cancel it and make you go toward the nearest enemy always.

Edited by torigara, 24 February 2012 - 12:16 AM.

  • 0

#9 Masterjohn74

Masterjohn74

    GMC Member

  • New Member
  • 358 posts

Posted 24 February 2012 - 02:40 AM


&& collision_point(mouse_x,mouse_y,P_enemy,true,true)=true

The first part is never executed because collision_point never returns true. It is advised to look up the function again and read the general description located at the top of the page. This sentence is right above the description of collision_point:

All these functions return either the id of one of the instances that collide, or they return a negative value when there is no collision.

You could (and should) just use the returned id instead of taking instance_nearest.

Other potential problem lies in the structure of your logic. It basically reads:
if (you release the mouse button on an enemy) {
    start a path to it
}
if (you release the mouse button elsewhere) {
    execute a script
}
if (there is an enemy near the object) {
    start a path to it
}
Even if you manage the first if block to work, the third one will cancel it and make you go toward the nearest enemy always.

How do I make it check if where I click is on the enemy?
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users