Problem with SetTimer in while-loop (SOLVED)

Started by , Mon 20/02/2006 20:28:06

Previous topic - Next topic

Tomac

Hi!

When I am using this function:

Code: ags

function processBars(int b)
{	
  SetTimer(1, 5);
  bar_info[b].amount-=(bar_info[b].max/2+Random(8));
  barpaint(b);
  while(bar_info[b].amount < bar_info[b].max)
  {
    if (IsTimerExpired(1)==1)
    {
      SetTimer(1, 0);
      bar_info[b].amount+=1;
      barpaint(b);
      SetTimer(1, 5);
    }
  }
}


i get the following Error-Message:

"Error: Script appears to be hung (150001 while loop iterations without an update) in Line ... "

I don't understand what the problem is because the value of bar_info.amount is counting up each time so the while loop should stop after a few iterations.

Any suggestions what the problem could be?

Thanks,
Tomac

Pumaman

There is no Wait(1) in your loop, therefore the timer will not expire, because the game is not actually running in the background.

Perhaps an easier way to achieve what you want would be this:

Code: ags

function processBars(int b)
{ 
  bar_info[b].amount-=(bar_info[b].max/2+Random(8));
  barpaint(b);
  while(bar_info[b].amount < bar_info[b].max)
  {
    Wait(5);
    bar_info[b].amount+=1;
    barpaint(b);
  }
}


Tomac

Thanks for your answer, but now I have the problem that with the Wait command my game is blocked what should be avoided. I had a similar solution first, but then tried with SetTimer instead of Wait to make this function Non-Blocking what made the program crash (see error above).

So would there be any possible solution with a Timer?

Ashen

#3
Try:
Code: ags

function processBars(int b)
{
  int temp;
  bar_info[b].amount-=(bar_info[b].max/2+Random(8));
  barpaint(b);
  while(bar_info[b].amount < bar_info[b].max)
  {
    while (temp < 5) temp ++;
    if (temp == 5) {
      bar_info[b].amount+=1;
      barpaint(b);
      temp = 0;
    }
  }
}


You could use the Timer, but you'd need to run the IsTimerExpired() check in repeatedly_execute, rather than the function itself..
I know what you're thinking ... Don't think that.

Tomac

Thanks a lot, putting the IsTimerExpired check in repeatedly_execute solved my problem!

SMF spam blocked by CleanTalk