• 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 Sentry Enemy Coding Help

L

LuckyGunn

Guest
I'm working on a stealth game and revising some mechanics. I have a sentry enemy that used to have a constant, swiveling line of sight. I'm trying to change it at the moment so it instead lies "asleep" for a few seconds, "blinks awake" for a few seconds during which it can detect the player, then falls back "asleep" and repeats. I got this system up and running and it works pretty well when only one enemy exists in the room at a time. However, when more than one of this enemy object exists in a room, after waking up, the enemies will sporadically flicker on and off every few frames. It seems as if they're caught between their two states.

I've tried a few different fixes and nothing seems to work. I tried making each enemy into an individual object instead of instances of the same object (reasonable since there's only about 10 in each room), all running on the same code. With this, only one object in the whole level worked (it did function perfectly, though), and the rest never turned on.

My final attempted solution was to remove all code/events from all but one of the sentry objects and parent them all to one, but that resulted in the first issue again (flickering on and off).

I think the error is coming from the multiple objects checking a collision line for the player object, but I'm not experienced enough to know if this is the issue or how exactly to go about fixing it.

I'll include all of the object's info/code below (it's a total mess and filled with variables that are now irrelevant after the changes, apologies). Any help in fixing this issue would be greatly appreciated!

Code:
Information about object: obj_sentry
Sprite: spr_sentry_sleep
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:

Children
obj_sentry02
obj_sentry03
obj_sentry04
obj_sentry05
obj_sentry06
obj_sentry07
obj_sentry08
obj_sentry09

Mask:

No Physics Object
Create Event:

execute code:

///Variables

//Calculate Camera
length = 300;
rotation = 270;
xx = x + lengthdir_x(length, rotation);

yy = y + lengthdir_y(length, rotation);

/*
//Camera Angle Limits
maxAngle = -15;
minAngle = -120;
up = choose(true, false);
*/

//Timer
room_speed = 60;
seconds = 5;
actSeconds = 3;
delaySeconds = .5;
timer = room_speed * seconds;
count = seconds;
actCount = actSeconds;
delayCount = delaySeconds;
alarm[0] = 60;
alarm[1] = 0;

//states
sleep = 0;
search = 0;
warn = 0;

Alarm Event for alarm 0:

execute code:

///Timer

sleep = 1;
search = 0;

if count = 5
{
audio_play_sound(snd_sleep, 1, false);
actCount = 3;
}
if count > 1
{
count --;
alarm[0] = 60;
}

else if count = 1
{
audio_play_sound(snd_warn,1,false);
warn = 1;
count --;
alarm[0] = 60;
}
else if count = 0
{
warn = 0;
alarm[1] = 60;
alarm[0] = 0;
}

Alarm Event for alarm 1:

for all obj_sentry: execute code:

///Activate Sentry

sleep = 0;
search = 1;
actCount --;
audio_play_sound(snd_search, 1, false);

if actCount = 0
{
count = 5;
alarm[0] = 60;
}

else if collision_line(x, y, xx, yy, obj_player, false, false) && obj_player.sprite_index != spr_player_hide
{
instance_create(obj_player.x,obj_player.y,obj_dead);
}

else
{
alarm[1] = 60;
}



Step Event:

execute code:

///Sentry Motion & Collision

//Collision
xx = x + lengthdir_x(length, rotation);
yy = y + lengthdir_y(length, rotation);

//animation
/*
if sleep = 1
{
    if count = 5
    {
    sprite_index = spr_sentry_close;
    }
    else
    {
    sprite_index = spr_sentry_sleep;
    }
}

else if search = 1
{
if actCount = 3
    {
    sprite_index = spr_sentry_open;
    }
    else
    {
    sprite_index = spr_sentry_search;
    }
}
/*
if collision_line(x, y, xx, yy, obj_player, false, false) && obj_player.sprite_index != spr_player_hide
{
    instance_create(obj_player.x,obj_player.y,obj_dead);
}
*/

/*
//Rotation
if !(up) && rotation < minAngle
{
up = !up;
}
else if (up) && rotation > maxAngle
{
up = !up;
}

if (up)
{
rotation += .75;
}
else if !(up)
{
rotation -= .75;
}

Draw Event:

execute code:

///animations

if sleep = 1
{
draw_sprite(spr_sentry_sleep,0,x,y);
}

else if warn = 1
{
draw_line(x, y, xx, yy);
}

else if search = 1
{
draw_sprite(spr_sentry_search, 0, x, y);
draw_triangle_colour(x,y, xx-30, yy, xx+30, yy, c_yellow, c_red, c_red, false);
}
 
E

Eggzum1998

Guest
Let me ask my friend, she's working on a game like this
 

Kalegar

Member
I think the problem is the 'alarm[0] = 0' line at then end of the alarm0 event. That is causing alarm0 to trigger every step after count reaches 0. Try removing that line or changing it to alarm[0] = -1;
 
Top