Timer within an infinite loop

Started by arj0n, Thu 21/04/2011 10:47:19

Previous topic - Next topic

arj0n

Something is wrong with this part of code. Not sure if the first row (which isn't right) is needed anyway.
Can someone please help me out with this?
The timer needs to run and get to the IsTimerExpired part.
Then the loop should restart the timer. The loop should be infinite.
And it's, of course, for the rep_exec.

Code: ags

Bool SetTimer = True
While 1
{ if(settimer ==true)
{(settime ==true)
{settimer(1,120);
Settimer = false;  
}
If (istimerexpired(1))
{Display(“txt”);
Settimer=true
}
}

Khris

Code: ags
  if (IsTimerExpired(1)) {
    ...
    ...
    SetTimer(1, 120);
  }


You need to start it once in game_start or a room's fadein.

arj0n

#2
@Khris
So the code I posted is not needed, only this little part you posted?

In the end it should change the background repeatedly, eg: gNoise.backgroundgraphic=6, gNoise.backgroundgraphic=7, gNoise.backgroundgraphic=8.

But this example with Display txt doesn't show any text at all:
Code: ags

function room_RepExec()
{
if (IsTimerExpired(1)) {
Display ("1");
Display ("2");
Display ("3");
    SetTimer(1, 120);
  }
}

arj0n

#3
Tried this, seems to work and seems to be infinite cause "cEgo.Say ("endloop");" is never shown.
But not sure if it is really OK:

Code: ags

function room_AfterFadeIn()
{
int i=0;
while(i<=1){
cEgo.Say ("2");
Wait (40);
cEgo.Say ("3");
Wait (40);
if (IsTimerExpired(1)) {
cEgo.Say ("endloop");
Wait (40);
SetTimer(1, 120);
i++;}
}
}

Khris

First of all, IsTimerExpired() will only work inside one of the repeatedly_execute functions.

Now, like I said, my code will only work if the timer is started manually once, e.g. inside one of the room's fadein functions. Otherwise, IsTimerExpired() in my code will never be true because the timer never got started.

What you did there won't work at all; Wait() halts the game until the number of loops has passed, it doesn't run in the background.
And:
Code: ags
int i=0;
while(i>=1){

The while block is skipped entirely, because obviously i isn't >= 1 if you just set it to 0.

arj0n

Yes, the code I posted wasn't correct at all.

Code: ags

function room_RepExec()
{
cEgo.Say ("1");
cEgo.Say ("2");
if (IsTimerExpired(1)) {
cEgo.Say ("endloop");
Wait (40);
SetTimer(1, 120);
}
}

This seems to run though.... Is it correct this way?

Ultra Magnus

Try this.
Code: ags

int timer;
function room_RepExec()
{
  if (timer<40) {gNoise.backgroundgraphic=6; timer++;}
  else if (timer<80) {gNoise.backgroundgraphic=7; timer++;}
  else if (timer<120) {gNoise.backgroundgraphic=8; timer++;}
  else timer=1;
}
I don't mean to sound bitter, cold, or cruel, but I am, so that's how it comes out.

I'm tired of pretending I'm not bitchin', a total frickin' rock star from Mars.

SMF spam blocked by CleanTalk