Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: on Mon 20/02/2006 20:28:06

Title: Problem with SetTimer in while-loop (SOLVED)
Post by: on Mon 20/02/2006 20:28:06
Hi!

When I am using this function:


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
Title: Re: Problem with SetTimer in while-loop
Post by: Pumaman on Mon 20/02/2006 20:46:55
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:


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);
  }
}

Title: Re: Problem with SetTimer in while-loop
Post by: on Mon 20/02/2006 20:52:32
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?
Title: Re: Problem with SetTimer in while-loop
Post by: Ashen on Mon 20/02/2006 20:59:56
Try:

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..
Title: Re: Problem with SetTimer in while-loop
Post by: on Tue 21/02/2006 09:23:54
Thanks a lot, putting the IsTimerExpired check in repeatedly_execute solved my problem!