GML image_angle checks randomly stop working mid-game

Hi, I am currently working on my top down GTA 1 style game, and I have run into an issue. When the player is wanted by police, every 16 - 30 seconds, I want the dispatcher to announce the player's direction.
Code:
///Direction Update
    ///Wanted by police and driving a car
if(wanted == true and state == 1){
    directionUpdateTimer -= 1;

    if(directionUpdateTimer <= 0){
        directionUpdate = true;
    }else{
        directionUpdate = false;
    }
   
    if(directionUpdate == true and directionUpdateTimer <= 0){
        directionUpdate = false;
        directionUpdateTimer = choose(irandom_range(500,800));
       
        if(Direction == "north"){
            sound_play(snd_suspectNorth);
        }
       
        if(Direction == "south"){
            sound_play(snd_suspectSouth);
        }
       
        if(Direction == "east"){
            sound_play(snd_suspectEast);
        }
       
        if(Direction == "west"){
            sound_play(snd_suspectWest);
        }
       
    }

}

if(state == 1){
    ///Set direction variable for police dispatcher update announcment
    if(driving.image_angle >= 315 ){
        Direction = "east";
    }

    if(driving.image_angle <= 45 ){
        Direction = "east";
    }

    if(driving.image_angle >= 46 and driving.image_angle <= 135){
        Direction = "north";
    }

    if(driving.image_angle >= 136 and driving.image_angle <= 225){
        Direction = "west";
    }

    if(driving.image_angle >= 226 and driving.image_angle <= 314){
        Direction = "south";
    }

}
I made it so I can see variable values of every object in the game, and the Direction variable just stops updating when the player turns after a minute or so of playing. I have kept testing it and I haven't found any patterns to when it stops... any ideas? (This version of the game I'm working on is in GM 8.1, I have been having the issue in GMS 1 as well)(direction is capitalized to not confuse with the built in direction variable)

Thanks!
 
T

Timothy

Guest
Maybe image_angle is outside your check range. Try adding this inside you state == 1 condition body.
Code:
image_angle = (image_angle + 360) mod 360;
 
Maybe image_angle is outside your check range. Try adding this inside you state == 1 condition body.
Code:
image_angle = (image_angle + 360) mod 360;
Thanks for replying, but I just found out that image angle continuously increases. I thought image angle was in degrees, and it went back to zero after hitting 360/ went to 360 after hitting 0. Do you know of a way of checking the direction based of the sprite based on where it's facing rather than image angle?
 
T

Timothy

Guest
Code i posted will keep image_angle between 0(inclusive) to 360(exclusive) as long as you don't decrement by huge amounts. You can just put the code in step or call it as soon as you change image angle.
 
Code i posted will keep image_angle between 0(inclusive) to 360(exclusive) as long as you don't decrement by huge amounts. You can just put the code in step or call it as soon as you change image angle.
Thanks a lot, but how much is a huge amount? Hundreds, thousands, hundreds of thousands, I'd rather the game not crash or the system stop working in the middle of playing it...
 

johnwo

Member
Where is Direction set?
Additionally, you can, instead of using a lot of if's just use something like:
Code:
switch (round(image_angle/90)) {
    case 0:
    // facing east
    ...
}
If you're worried about image_angle being above 360 or below 0, just do:
Code:
while (image_angle < 0) image_angle = 360+image_angle;
image_angle = image_angle mod 360;
Albeit it's redundant as all hell if you apply addition and subtraction in a reasonable way...
 
Last edited:
Top