SOLVED Enemy AI, patrol left-right.

neutro

Member
Hi guys,
So, now I am working on a simple enemy AI system.
I have the enemy oEvilFrosty1.
I have the object oPatrolTrigger, in the Instances layer, 2 instances of it that my oEvilFrosty1 is gonna patrol between.

oEvilFrosty1 Create Event:
GML:
hsp = 0.2;
vsp = 0;
grv = 0.14;
oEvilFrosty1 Step Event:
GML:
// gravity
vsp += grv;

// x collide
if (place_meeting(x+hsp,y,oWall)) {
    while (!place_meeting(x+sign(hsp),y,oWall)) {
        x += sign(hsp);
    }
    hsp *= -1;
}

// y collide
if (place_meeting(x,y+vsp,oWall)) {
    while (!place_meeting(x,y+sign(vsp),oWall)) {
        y += sign(vsp);
    }
    vsp = 0;
}

// turn around
if (place_meeting(x,y+1*sign(hsp),oPatrolTrigger)) {
    hsp = -0.2;  
}

// move
image_xscale = sign(hsp);
x += -hsp;
y += vsp;
Frosty starts, patrols towards the left, collides with the oPatrolTrigger, sprite image mirrors correctly, patrols right, then there is this second instance of oPatrolTrigger and I can't really solve how to get Frosty to do the collide and turn around while patroling towards the right. I tried a bunch of different ifs, if else and nested if-statements all through the day. Anyone got any suggestions how to solve this?
Kind regards,
Fred
 

neutro

Member
If it's moving horizontally, shouldn't this be x + sign(hsp), y?



This is always sending it to the left. You probably want something like the hsp *= -1 you used when it hit the wall.
Code:
GML:
// turn around
if (place_meeting(x,y+1*sign(hsp),oPatrolTrigger)) {
    hsp *= -1;
}

// move
image_xscale = sign(hsp);
x += -hsp;
y += vsp;

Result:
 

Nidoking

Member
Ah, the oPatrolTrigger are those little red boxes in the ground? In that case, you just want x, y + 1. The problem is that when sign(hsp) is negative, you're checking for collision above the snowman, not below. You want to check for collision below both ways. That, or put the oPatrolTrigger above the ground (I assume they'll be invisible when you're finished making them work) and use x, y.
 

neutro

Member
Ah, the oPatrolTrigger are those little red boxes in the ground? In that case, you just want x, y + 1. The problem is that when sign(hsp) is negative, you're checking for collision above the snowman, not below. You want to check for collision below both ways. That, or put the oPatrolTrigger above the ground (I assume they'll be invisible when you're finished making them work) and use x, y.
So how do I solve it? I tried several ifs and else ifs with negative values, positive etc. This is so complicated.
 

neutro

Member
if (place_meeting(x,y+1,oPatrolTrigger))

Like I said, that's it.
GML:
// turn around
if (place_meeting(x,y+1,oPatrolTrigger)) {
    hsp *= -1;
}

// move
image_xscale = sign(hsp);
x += -hsp;
y += vsp;
Now, the snowman collides, stop moving left OR right and flickers between left or right image_xscale. Same problem I had earlier today.
 

Nidoking

Member
Okay, got it. Let's try fixing all of the problems at once.

if (place_meeting(x + sign(hsp),y+1,oPatrolTrigger))

Also, why are you ending with x += -hsp? I think that should be x += hsp.
 

neutro

Member
Okay, got it. Let's try fixing all of the problems at once.

if (place_meeting(x + sign(hsp),y+1,oPatrolTrigger))

Also, why are you ending with x += -hsp? I think that should be x += hsp.
EDIT: Getting tired here, LEFT, not right as I wrote

Ok, the minus was for the snowman to be moving LEFT. I want him to move LEFT from start. I thought that was the correct logic.
So now, the snowman collides and change direction correctly.
BUT, he's doing a moonwalk :D walking backwards and turns, walking backwards and turns.
 
Last edited:

Let's Clone

Member
EDIT: Getting tired here, LEFT, not right as I wrote

Ok, the minus was for the snowman to be moving LEFT. I want him to move LEFT from start. I thought that was the correct logic.
So now, the snowman collides and change direction correctly.
BUT, he's doing a moonwalk :D walking backwards and turns, walking backwards and turns.
Is your default sprite facing to the right or the left?
 

sylvain_l

Member
GML:
// turn around
if (place_meeting(x,y+1,oPatrolTrigger)) {
hsp *= -1;
}

// move
image_xscale = sign(hsp);
x += -hsp;
y += vsp;
Now, the snowman collides, stop moving left OR right and flickers between left or right image_xscale. Same problem I had earlier today.
that's because once he is colliding with oPatrolTrigger the collision event is running again every frame if it doesn't move far enough to be no more colliding.

2 solutions:
  1. You just need a flag to pause the execution of your direction swapping for a few frames then use an alarm to reset the flag so when it collide with the other oPatrolTrigger the opposite side it will run again.
  2. or at the same time you swap direction you also move the snowman to ensure it's no more colliding with oPatrolTrigger
 

neutro

Member
So, It works if I mirror the sprite. But he still starts off moving right. I want him moving left from start.
 

neutro

Member
that's because once he is colliding with oPatrolTrigger the collision event is running again every frame if it doesn't move far enough to be no more colliding.

2 solutions:
  1. You just need a flag to pause the execution of your direction swapping for a few frames then use an alarm to reset the flag so when it collide with the other oPatrolTrigger the opposite side it will run again.
  2. or at the same time you swap direction you also move the snowman to ensure it's no more colliding with oPatrolTrigger
Tell me more about flags. How do I learn more about implementing them? Is flags the same as booleans? Do you have an easy example? I'm realizing flags play an important role in game logic, because I read about them here and there in forum threads.
 

Let's Clone

Member
It is facing left. And as the player starts at bottom left, he faces right.
If your player is walking to the right, then your sign(hsp) is positive 1. Left would be negative one.
If your setting your image_xscale to be equal to your sign(hsp) then walking right (+1) would not flip your sprite, makit it left-facing.
 

neutro

Member
If your player is walking to the right, then your sign(hsp) is positive 1. Left would be negative one.
If your setting your image_xscale to be equal to your sign(hsp) then walking right (+1) would not flip your sprite, makit it left-facing.
Thanks! I hope, in the Future, I will understand this like I understand my native tongue 🙂
Kind regards,
A bit drunk-noob named Fred
 

Let's Clone

Member
Thanks! I hope, in the Future, I will understand this like I understand my native tongue 🙂
Kind regards,
A bit drunk-noob named Fred
No worries, the language will become more intuitive as you use it.

If you don't mind a self-plug, I put up tutorial on youtube for GMS2. I have bored of the typical tutorials that teach how to make a skeleton of a game (don't get me wrong, those are great and I learned a LOT from them). But I just wanted something different. So my approach is that I clone classic games from my childhood and teach how to make those. If this sounds interesting to you then I'd love for you to check out the content!

www.youtube.com/c/letsclone
 

neutro

Member
No worries, the language will become more intuitive as you use it.

If you don't mind a self-plug, I put up tutorial on youtube for GMS2. I have bored of the typical tutorials that teach how to make a skeleton of a game (don't get me wrong, those are great and I learned a LOT from them). But I just wanted something different. So my approach is that I clone classic games from my childhood and teach how to make those. If this sounds interesting to you then I'd love for you to check out the content!

www.youtube.com/c/letsclone
Nice! Will check out. :)
 
Top