Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: xenogia on Fri 03/03/2006 02:21:48

Title: Random background sounds after a specific timer duration (issue)
Post by: xenogia on Fri 03/03/2006 02:21:48
I have been trying to get certain sound effects to play after a specific amount of time.  There are three sound effects it randomly chooses from, the issue I am having is that no sound effect is playing but the code I put in seems logical.  This is what I have written:

(Before Fades In)

SetTimer (1, 400);


(Repeatedly Execute)

int ran=Random(2);
if ((IsTimerExpired(1) == 1) && (ran == 0)) {
  PlaySound (26);
  SetTimer (1, 400);
}

if ((IsTimerExpired(1) == 1) && (ran == 1)) {
  PlaySound (27);
  SetTimer (1, 400);
}

if ((IsTimerExpired(1) == 1) && (ran == 2)) {
  PlaySound (26);
  SetTimer (1, 400);
}


I have tried putting SetTimer (1, 0) before the SetTimer (1, 400) to see if that works, but that doesn't seem to solve my problem.

Title: Re: Random background sounds after a specific timer duration (issue)
Post by: Gilbert on Fri 03/03/2006 02:40:10
The reason is, a timer expires only once and then it will get reset. That is, if you checked if it expired with IsTimerExpires(), the second time you check it with IsTimerExpires() again it wouldn't be reported as "expired" anymore.

So, only the first if part will be checked in your codes.
Recommended change:

(Before Fades In)

SetTimer (1, 400);


(Repeatedly Execute)
if (IsTimerExpired(1) == 1) {
Ã,  int ran=Random(2);
Ã,  if (ran == 0)Ã,  PlaySound (26);
Ã,  else if (ran == 1)Ã,  PlaySound (27);
Ã,  else if (ran == 2) PlaySound (26);
Ã,  SetTimer (1, 400);
}
Title: Re: Random background sounds after a specific timer duration (issue)
Post by: xenogia on Fri 03/03/2006 02:42:11
Thanks gilbot