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

Legacy GM Problem with enemy path and enemy follow not working together

A

Agletsio

Guest
Hi there,

My goal is to have my NPC follow a path until the player reaches within a certain distance of it, where then the NPC would start following the player. If the player manages to exit that certain distance again, I would want the NPC to return to it's path.

At present, I can only make these two options work separately and can't get them to function together.

This is my current code set-up (in a Step Event). With this code the NPC does nothing until the player enters within it's 128 radius, where it's start walking the path instead of chasing the player:


if distance_to_object(Player) <= 128
{
move_towards_point(Player.x, Player.y, 2)
}

else
{
if distance_to_object(Player) > 128
{
path_start (path1, 2, path_action_restart, true)
}
}

Any help or guidance will be much appreciated! Been trying to find answer for hours now without any luck!
 
K

KerwinSneijders

Guest
[removed comment]
Never mind, I misunderstood the question
 
Last edited by a moderator:

jo-thijs

Member
First of all, I think it is best to keep the distance when the enemy starts chasing the player
and the distance when the enemy retreats different.
If they're both 128, it could cause indecisive behaviour in the enemy.
Take the first distance 128 and the second 144 for example.

You can manipulate the position of the enemy on his path through the variable path_position.
When the enemy starts chasing the player, you can store the enemy's x and y coordinates and his path_position.
You can then end his path.

Then, when he stops the chase, you can let him return to the previously stored x and y coordinates (through move_towards_point).
When he gets close enough to those coordinates, you can restart the path and restore the path_position.

Does that do the trick?
 
A

Agletsio

Guest
First of all, I think it is best to keep the distance when the enemy starts chasing the player
and the distance when the enemy retreats different.
If they're both 128, it could cause indecisive behaviour in the enemy.
Take the first distance 128 and the second 144 for example.

You can manipulate the position of the enemy on his path through the variable path_position.
When the enemy starts chasing the player, you can store the enemy's x and y coordinates and his path_position.
You can then end his path.

Then, when he stops the chase, you can let him return to the previously stored x and y coordinates (through move_towards_point).
When he gets close enough to those coordinates, you can restart the path and restore the path_position.

Does that do the trick?
Ahh ok so got the NPC to walk the path until the player enters radius and starts following. Thanks for mentioning path_end, reailsed I never told NPC to end the path when player enters radius. And thanks for suggesting the different distances, makes total sense and will make a habit of it from now on.

Problem now is I can't get NPC to go back to the path after player exits specified radius. This is my current code set-up. Also not sure how to "store the enemy's x and y coordinates and his path_position". Sorry, coding knowledge very minimal.


In Create Event I have this code:

path_start(path1,2,path_action_restart,true)



In Step Event I now have this code:

if distance_to_object(Player) < 128
{
path_end();
move_towards_point(Player.x,Player.y,2);
}

//Up until here, everything works. Everything I've tried after this screws around with the behavior. After implementing the following code, NPC doesn't walk path anymore. Will start to chase me and when player enters detection radius and will return to that path's start when player exits, but not walking path.

else
{
if distance_to_object(Player) > 144
{
move_towards_point(path1.x,path1.y,2);
path_start(path1,2,path_action_restart,true);
}
}
 

jo-thijs

Member
You can't get the x and y of a path.
When the player switches from following the path to following the player, you need to store path_position.
Like this:
Code:
var d = distance_to_object(Player);
if d < 128
{
    if path_index != -1
    {
         pathpos = path_position;
         path_end();
    }
    move_towards_point(Player.x, Player.y, 2);
}
else
{
    if d > 144 && path_index == -1
    {
        var targx = path_get_x(path1, pathpos);
        var targy = path_get_y(path1, pathpos);
        if point_distance(x, y, targx, targy) > 2
            move_towards_point(targx, targy, 2);
        else
        {
            path_start(path1, 2, path_action_restart, true);
            path_position = pathpos;
        }
    }
}
 
A

Agletsio

Guest
Sick, it works!! Thanks so much!!

Just copy and pasted so don't understand the reasoning behind that yet, though. Would you mind explaining this to me if you have time (or pointing me towards a resource that explains all these expression, can't find thorough one):


1.) I take it the "var" expression is how you set variable in code? Was actually trying to figure that out too.

2.) Does "if path_index != -1" mean that "if there is a set path" and/or "if the path index is equal to something(not nothing)"? If I understand correctly, "-1" means "everything"?

3.) What does "==" and how does it differ from "="?

I still have a plethora of questions regarding this specific code but I think understanding these for now would help loads.

Sorry, I know it must be a pain dealing with a beginner. It's just by understanding it I can better implement it in the future and tweak it, when needed, when I understand the process.

Many, many thanks for the help and solution!! Been stuck on this for ages now!
 

jo-thijs

Member
Sick, it works!! Thanks so much!!
You're welcome!

Would you mind explaining this to me if you have time
Sure, no problem.

(or pointing me towards a resource that explains all these expression, can't find thorough one)
The GameMaker manual is a great place for that.

1.) I take it the "var" expression is how you set variable in code? Was actually trying to figure that out too.
Not really, the "var" expression creates a temporary variable, which will be deleted at the end of the code.

2.) Does "if path_index != -1" mean that "if there is a set path" and/or "if the path index is equal to something(not nothing)"? If I understand correctly, "-1" means "everything"?
It is exactly the opposite.
If the current instance is following a path, path_index will be the identifier of that path.
If path_index is -1, it indicates no path is being followed.

3.) What does "==" and how does it differ from "="?
== is for comparing values, = is for assigning values.
GameMaker treats = at unusual places like comparison,
but it is good practice to use == for readability and for when you go to other programming languages.

Sorry, I know it must be a pain dealing with a beginner. It's just by understanding it I can better implement it in the future and tweak it, when needed, when I understand the process.
It isn't whether someone is a beginner that makes them a pain, but it is their attitude,
whether they are eager to learn, willing to share information, willing to test things themselves, ...
You're just fine.
 
Top