[Solved] Trouble with SetTimer and IsTimerExpired in my Game

Started by DBoyWheeler, Tue 21/04/2015 01:18:13

Previous topic - Next topic

DBoyWheeler

Okay... I'm having a bit of trouble with some coding in my game.

See, there's a scene in my game where the heroine, Lydia, encounters a bear.

She has three seconds to either leave the room or use an item (the Hot Sauce) to drive away the bear, or she is attacked by the bear and triggers the death message (yep, I even prepared a GUI for death messages).

But after a couple seconds, the game will end up doing nothing.

Here's my code for the bear encounter (Lydia crosses the right edge to trigger it).
Code: ags

function room_LeaveRight()
{
  //If the bear is still active (i.e. not driven away yet), this will trigger his appearance.
  if (chapter < 8)
  {if ((cLydia.HasInventory(iHotSauce))&& (cLydia.HasInventory(iRope)))
  {
    int countdown = (GetGameSpeed() * 4);
    cLydia.StopMoving();
    cBear.Transparency = 0;
    cBear.Move(cLydia.x, cLydia.y-5, eBlock, eWalkableAreas);
    cBear.ChangeView(53);
    cBear.x = cLydia.x;
    cBear.y = (cLydia.y-5);
    DeathThreat = true;
    SetTimer(1, countdown);
    }
  }
}


Here is a code where if she leaves the room to the previous room, the "Death Threat" turns off, but if she tries to force her way across, the bear will kill her (as in the timer automatically expires).
Code: ags

function hOutHoleRoom_AnyClick()
{
  if((chapter < 8) && (cBear.Transparency < 100) && (DeathThreat == true) && (Mouse.Mode == eModeWalkto))
  {if (mouse.x < 205)
  { DeathThreat = false;
    SetTimer(1, 0);
    cLydia.Walk(4, 331, eBlock, eWalkableAreas);}
  else if (mouse.x > 205)
  { SetTimer(1, 0);}
  }
}


And here's for what is when the timer expires.
Code: ags

function room_RepExec()
{
if (DeathThreat == true)
   {if (IsTimerExpired(1) == true)
     {
      cLydia.x = cBear.x;
      cLydia.y = (cBear.y + 5);
      cBear.LockView(54);
      cLydia.LockView(27);
      cBear.Animate(0, 5, eOnce, eBlock, eForwards);
      Wait(5);
      cLydia.Animate(0, 5, eOnce, eBlock, eForwards);
      IsLydiaAlive = false;
      lDeathMessage.Text = "That was VERY 'un-bear-able'.  Lydia should've planned this more carefully.";
      aLoseHorn.Play(eAudioPriorityHigh, eOnce);
      gDeathMessageGu.Visible = true;
      gDeathMessageGu.SetPosition(0, 250);}
   }
}


First off, the "chapter" variable is a Global Variable I've prepped to help me keep track of where the story is, so that certain dialogues and what not are at the right spots.  And secondly, yes, the LoseHorn music is indeed the Losing Horn music from the Price is Right.

But I digress.  I can't seem to get the Timer stuff to work properly.  What could I be doing wrong?

Gilbert

I haven't read much of the code, but from the manual:
QuotePass TIMEOUT as 0 to disable a currently running timer.

So, setting a timer to 0 doesn't make it expire, but instead disables it.
One quick change is to try to set the timer to expire in 1 game loop instead.

DBoyWheeler

#2
Thanks.  Also, I'm considering trying out Khris' trick in this thread.  Maybe that might be useful in the future.

[Edit]

Okay, I'm still having trouble, and I have no flippin' clue what I did wrong.  I even tried Khris' idea and it was a no-go.

Here are the snippets of code compiled for the bear encounter in the room.
Code: ags

function room_LeaveRight()
{
  //If the bear is still active (i.e. not driven away yet), this will trigger his appearance.
  if (chapter < 8)
  {if ((cLydia.HasInventory(iHotSauce))&& (cLydia.HasInventory(iRope)))
  {
    DeathThreat = true;
    cLydia.StopMoving();
    mouse.DisableMode(eModeWalkto);
    cBear.Transparency = 0;
    cBear.Move(cLydia.x, cLydia.y-5, eBlock, eWalkableAreas);
    cBear.ChangeView(53);
    cBear.x = cLydia.x;
    cBear.y = (cLydia.y-5);
    SetTimer(1, 160);
    }
  }
}

function room_RepExec()
{
 if(IsTimerExpired(1))
 {
  if(DeathThreat == true)
      cBear.LockView(54);
      cLydia.LockView(27);
      cBear.Animate(0, 5, eOnce, eBlock, eForwards);
      Wait(5);
      cLydia.Animate(0, 5, eOnce, eBlock, eForwards);
      IsLydiaAlive = false;
      lDeathMessage.Text = "That was VERY 'un-bear-able'.  Lydia should've planned this more carefully.";
      aLoseHorn.Play(eAudioPriorityHigh, eOnce);
      gDeathMessageGu.Visible = true;
      gDeathMessageGu.SetPosition(0, 250);}}
}


I decided to take out the walk away code, so it's either use the Hot Sauce to drive the bear away or die (the walk code wouldn't work properly).

The code that drives the bear away is in the global script, since it's using an item on a character.
Code: ags

function cBear_UseInv()
{
  if(cBear.Transparency < 100)
  {if(cLydia.ActiveInventory == iHotSauce)
  {
    DeathThreat = false;
    cLydia.LockView(38);
    cLydia.Animate(0, 5, eOnce, eBlock, eForwards);
    cLydia.UnlockView();
    cLydia.LoseInventory(iHotSauce);
    cBear.LockViewFrame(55, 0, 0);
    Wait(35);
    cBear.UnlockView();
    cBear.LockView(56);
    cBear.Animate(0, 5, eRepeat, eNoBlock, eForwards);
    cBear.Move(cLydia.x, 269, eBlock, eWalkableAreas);
    cBear.UnlockView();
    cBear.Transparency = 100;
    cLydia.Say("Wow!  That's some strong stuff.");
    chapter = 8;
    Display("Lydia hears a familiar voice call out: 'Lydia?!  Is that you?!'");
    cLydia.Say("*gasp* That's Denise!  Her voice is coming from the hole!");
    mouse.EnableMode(eModeWalkto);
    }
  else if(cLydia.ActiveInventory == iPocketknife)
  {Display("Lydia may not have enough force to use the pocketknife as an effective self-defense weapon.");}
  else {Display("That's probably not a good item to use to get the bear to go away.");}}


So what in the world could be screwing up the timer?

[Edit] Okay, I saw what I did wrong.  If I had it on an edge, it would kinda get stuck.  I have to use a region property.  Here are the changes I made.

Code: ags

function region1_WalksOnto()
{
    //If the bear is still active (i.e. not driven away yet), this will trigger his appearance.
  if (chapter < 8)
  {if ((cLydia.HasInventory(iHotSauce))&& (cLydia.HasInventory(iRope)))
  {
    cLydia.StopMoving();
    region[1].Enabled = false; //To prevent possible loops.
    cLydia.FaceLocation(cLydia.x, 0, eNoBlock);
    mouse.DisableMode(eModeWalkto);
    cBear.Transparency = 0;
    cBear.Move(cLydia.x, cLydia.y-5, eBlock, eWalkableAreas);
    cBear.ChangeView(53);
    cBear.x = cLydia.x;
    cBear.y = (cLydia.y-5);
    cLydia.Say("Eek!");
    SetTimer(1, 240);
    }
  }
}
[/ags]

And I removed the right edge thing.

SMF spam blocked by CleanTalk