If variable help

Started by Glenjamin, Fri 22/04/2016 18:16:22

Previous topic - Next topic

Glenjamin

Hello everyone!

I'm making a game where the player needs to hide in a closet to avoid a monster.
This event is based on three variables:
Turning on the lights which triggers the event,"Lights"
The amount of time it takes for the monster to arrive, "Timer(1)"
And whether or not the player is hiding, "Hiding"

Here is my code so far.

Code: ags

function room_RepExec()
{
if (lights == true && IsTimerExpired(1) && hiding == true)
  {
     cdoormonster.Walk(181, 138, eBlock, eAnywhere);
     SetTimer(3,500);
     //timer(3) is for when the monster leaves the room after a certain amount of time
  }
else if (lights == true && IsTimerExpired(1) && hiding == false){
  cdoormonster.Walk(181, 138, eBlock, eAnywhere);
  cdoormonster.FollowCharacter(cEgo, 1, 10);
  }
}


The issue I'm having is, the monster only appears when the player is hiding, and if the player were to leave the closet whilst the monster is in the room, it would not attack.

Thank you for your help!




Gord10

Is
Code: ags
cdoormonster.Walk(181, 138, eBlock, eAnywhere);

the part where monster appears?
Games are art!
My horror game, Self

Slasher

#2
first off you should check that all booleans are turned on/off correctly and that the timers are set correctly.

Like when coming out of hiding and setting hiding back to false.

Glenjamin

Quote from: Gord10 on Fri 22/04/2016 18:22:19
Is
Code: ags
cdoormonster.Walk(181, 138, eBlock, eAnywhere);

the part where monster appears?

The monster starts out outside of a walkable area offscreen. That event is to move him into the walkable area so he can attack.

I will also check my variable triggers and ensure everything works fine of that end.

Thank you!


Cassiebsg

Do you remember to reset your timer? Probably not that in the first case, but if you hiding and leave hiding when the monster is in the room, you have really no event set for that. Timer only expires once, you'll need to restart timer for it to work.
There are those who believe that life here began out there...

Kumpel

#5
There is some code missing. I guess your problem would be solved if you include an if statement that checks if the player did everything right, and then sets a bool var f.e. "HideSuccess" to true (But don't let it check in rep_exec or r_e_a, just copy the if-statment or make it a function and call it after every part of the hide puzzle is done)

Then do this if-statement to check if that is happening, what gives you trouble:

Code: ags

if ((HideSuccess == true) && (!IsTimerExpired(3)) && (hiding == false))  //maybe include && (lights == true)... 
  {
    HideSuccess = false;
    cdoormonster.Walk(181, 138, eBlock, eAnywhere);
    cdoormonster.FollowCharacter(cEgo, 1, 10);
  }


Of course you need to set the new var HideSuccess also to false in r_e or r_e_a if the light is turned out right after being turned on or the player is not hiding anymore right after he hid (and maybe after the monster left... I am not sure about that...).

Hope that helps :)

Khris

Some general stuff about if() and IsTimerExpired():

- comparing a boolean to true is redundant. When AGS gets to if (lights == true), what it does is complete ignore the if() at first and focus on the expression, lights == true. Since true == true evaluates to true, and false == true evaluates to false, you can simply use just lights.

- if you check multiple values and one of them is IsTimerExpired(), always put that first or the logic won't work properly. IsTimerExpired() will only return true exactly once, then false again, so if lights is true and the timer is expired but hiding is false, the test will fail, and every subsequent time it will fail at the timer check.

TL;DR:
Code: ags
 if (IsTimerExpired(1) && lights && hiding) { ...


However, you need to deal with multiple situations here, so you should probably nest your tests.

Code: ags
  if (IsTimerExpired(1)) {
    cdoormonster.Walk(181, 138, eBlock, eAnywhere);
    
    if (lights) {
      if (hiding) {
        SetTimer(3, GetGameSpeed() * 12); // 12 seconds
      }
      else {
        FollowPlayer(...);
      }
    }
    else {
      // no lights
    }
  }

SMF spam blocked by CleanTalk