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

How to Prompt an action only once?

J

Josh Broderick

Guest
Hi! I am creating a game where I have enemies wandering around until they come within "sight" (variable) of the player, then they will go after the player until the players goes outside their "sight". Everytime they see the player, I want them to flash, so the player knows they enemy has seen them. So far I've done a good job at making the enemy constantly white while they are chasing the player, then go back to normal afterwards, because I can't figure out how to set a variable to a given value only once until the if statement happens again. Please help!

I can post any of the coding needed to better understand what I need help with. I've seen other similar questions online, but none have been able to help me.
Thanks!
 

chamaeleon

Member
Use a boolean variable to indicate whether you have already changed the color or not (or whatever visual change you may use). When the condition occurs that should trigger the change, check the variable to see if it's set as effect having been used or not. If it hasn't, set the variable true, and make the change. After some number of steps/time has passed change back to normal. When the condition that would reset the flag occurs (what would indicate it's ok to show the change effect again), just reset the flag (this is a separate check from the changing the visual effect back to normal).
 
J

Josh Broderick

Guest
Thank you for your reply! Unfortunately, I still can't quite figure out why it's not working. To me, it seems like everything should be working. I'm going to copy and paste my code, hopefully someone can tell me where I went wrong:

This is in the draw event:
if (flash>0)
{
another = 1;
d3d_set_fog(true, c_white, 0,0);
draw_self();
flash = flash - 1;
d3d_set_fog(false, c_white,0,0);
} else
{
draw_self();
flash = 0;
}

This is in an enemy script (everything works but the flashing):
var dist = point_distance(x,y,oplayer.x,oplayer.y)
if(dist<view) ///view is an arbitrary value I set in the create event
{
if (another != 1)
{
flash = 6;
another = 1;
}
state = scr_enemy_chase_state;
targetx = obj_player.x;
image_speed = .15;
targety = obj_player.y;
} else
{
scr_enemy_choose_next_state();
another = 0;
}



To me, this would seem to work that as soon as the enemy got in range, if another isn't 1 (which it's 0 to start with), it will set flash to 6 (for 6 frames) but then set another to 1, so it doesn't keep flash at 6 forever. I'm obviously missing something, though, because they stay white the whole time they are chasing the player.

Thanks for your help!
 
Last edited by a moderator:
P

pushcx

Guest
Surround your code with (CODE)(/CODE) using square brackets instead of parentheses so we can see your indenting.

But it looks like the bug is that you set flash to 6 every time the enemy sees the player, not once. That's the part of the code that needs a boolean. Set it to true when seeing the player, skip the check for seeing the player if it's set, and unset it when the enemy loses sight of the player.
 
S

SSJCoder

Guest
Thank you for your reply! Unfortunately, I still can't quite figure out why it's not working. To me, it seems like everything should be working. I'm going to copy and paste my code, hopefully someone can tell me where I went wrong:

This is in the draw event:
if (flash>0)
{
another = 1;
d3d_set_fog(true, c_white, 0,0);
draw_self();
flash = flash - 1;
d3d_set_fog(false, c_white,0,0);
} else
{
draw_self();
flash = 0;
}

This is in an enemy script (everything works but the flashing):
var dist = point_distance(x,y,oplayer.x,oplayer.y)
if(dist<view) ///view is an arbitrary value I set in the create event
{
if (another != 1)
{
flash = 6;
another = 1;
}
state = scr_enemy_chase_state;
targetx = obj_player.x;
image_speed = .15;
targety = obj_player.y;
} else
{
scr_enemy_choose_next_state();
another = 0;
}



To me, this would seem to work that as soon as the enemy got in range, if another isn't 1 (which it's 0 to start with), it will set flash to 6 (for 6 frames) but then set another to 1, so it doesn't keep flash at 6 forever. I'm obviously missing something, though, because they stay white the whole time they are chasing the player.

Thanks for your help!
I can't easily test it since there's code from your project, but here's my attempt to fix your code:
Enemy:
Code:
var dist = point_distance( x, y, oplayer.x, oplayer.y );

if ( dist < view )
{
    // update flash
    flash -= 1;
    if ( flash < 0 )
        flash = 6;
 
    // update enemy
    state   = scr_enemy_chase_state;
    targetx = obj_player.x;
    targety = obj_player.y;
    image_speed = .15;
}
else
{
    // out of range
    scr_enemy_choose_next_state();
    flash = 0;
}
Draw:
Code:
if ( flash > 0 )
{
    d3d_set_fog( true, c_white, 0, 0 );
    draw_self();
    d3d_set_fog( false, c_white, 0, 0 );
}
else
{
    draw_self();
}
No idea why, but it seem you use both "oplayer.x/y" and "obj_player.x/y". In any case you should be able to fix that, if it becomes a problem.

Oh and one more thing, if you want them to flash only once, every time they get into the view, here's the (new) code:

Enemy:
Code:
var dist = point_distance( x, y, oplayer.x, oplayer.y );

if ( dist < view )
{
    // update flash
    flash -= 1;
    if ( !has_flashed )
    {
        flash       = 6;
        has_flashed = true;
    }
  
    // update enemy
    state   = scr_enemy_chase_state;
    targetx = obj_player.x;
    targety = obj_player.y;
    image_speed = .15;
}
else
{
    // out of range
    scr_enemy_choose_next_state();
    has_flashed = false;
}
Draw:
Code:
if ( flash > 0 )
{
    d3d_set_fog( true, c_white, 0, 0 );
    draw_self();
    d3d_set_fog( false, c_white, 0, 0 );
}
else
{
    draw_self();
}
 
Last edited:
Top