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

GameMaker Need help with some GML

G

giancoulas

Guest
i am trying to get this enemy obj to charge at the players last known location then stop there if the player is less then 200 away while the obj does do this it just keeps going till it collides or the player becomes in range of another charge i am pretty sure what i am doing wrong is very obvious but i am just not seeing it lol any help would be great

oh an this is all in the step event
 

Attachments

Cpaz

Member
So i'll break down what the snippet is doing piece by piece just to be sure were on the same page, then i'll try and explain what's going wrong.

So this was the snippet you gave:
Code:
if (distance_to_point(obj_player.x,obj_player.y) > 200)
{
   move_towards_point(obj_player.xprevious,obj_player.yprevious, 5);
}
if (distance_to_point(obj_player.xprevious, obj_player.yprevious) < 1)
{
   spd=0;
}
So this first part:
Code:
if (distance_to_point(obj_player.x,obj_player.y) > 200)
{
   move_towards_point(obj_player.xprevious,obj_player.yprevious, 5);
}
Is checking if distance between the object and the point (in this case, the position of the player) is greater than 200. If so, move towards the previous position at a constant speed of 5.

This second part:
Code:
if (distance_to_point(obj_player.xprevious, obj_player.yprevious) < 1)
{
   spd=0;
}
Is checking if the distance is less than 1, there it assigns the variable "spd" to 0.

Now, It's important to say, the way it's currently setup, the object will be constantly moving towards the player at a speed of 5. Something i'd recommend, is using a variable for speed. We can use the one that your already using since it's being assigned, but not used.

So my first change will be with the move towards function. For the sake of simplicity, well have that be constantly on and remove the condition, while also changing the value 5 with "spd".
Code:
//always try to move towards player
move_towards_point(obj_player.xprevious,obj_player.yprevious, spd);
Simple enough right? We'll be instead using the condition to change the speed of the object. So basically, it's always trying to move towards the player, but it won't actually move unless its a certain distance away.
Like this:
Code:
//always try to move towards player
move_towards_point(obj_player.xprevious,obj_player.yprevious, spd);

//set the speed of object
if (distance_to_point(obj_player.x,obj_player.y) > 200) // if it's farther than 200 pixels away from player...
{
   spd=5;
}
else // if it's closer than 200 pixels...
{
   spd=0;
}
The else statement is basically if the opposite of the if statement is happening, then do whatever in the block (or set of brackets).

I should also say that xprevious and yprevious are just the values from the previous step. Kinda hard to explain beyond that.

As you learn more about game maker and GML, you'll learn why this isn't the best method for handling this, but it should still work.

Best of luck! If I need to explain something better please let me know.
 
G

giancoulas

Guest
thank you actually makes a lot of sense now that i see it lol i am going to test it now. i figured this wasnt the best way i am just trying to get this working then i plan to make a script for it because this is a boss fight i am doing and plan to make him do more lol
 
Top