Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Recluse on Tue 25/03/2008 00:48:39

Title: Algorithm Issues (FIX'D)
Post by: Recluse on Tue 25/03/2008 00:48:39
I'm having algorithm issues.... I think... It's the only reason I didn't stick this in the beginner's board.

I'm trying to make a ticker on the dashboard to show the players "speed". It's entirely behind the scenes, but the number should gradually get higher as the player continues to walk... Here's what I have currently, it outputs to a GUI.

int WalkSpeed = 0;

function repeatedly_execute()
{
  if(player.Moving == true){
    while(IsTimerExpired(1) && WalkSpeed < 8)
    {
      WalkSpeed++;
      lblCurrSpeed.Text = String.Format("Current Speed: %d", WalkSpeed);
    }
    if(IsTimerExpired(1))
      SetTimer(1, 10);
  }
  else{
    while(IsTimerExpired(1) && WalkSpeed > 0)
    {
      WalkSpeed--;
      lblCurrSpeed.Text = String.Format("Current Speed: %d", WalkSpeed);
    }
    if(IsTimerExpired(1))
      SetTimer(1, 10);
  }
}
Title: Re: Algorithm Issues
Post by: Gilbert on Tue 25/03/2008 01:08:10
Can you tell what's not working in your codes? We can't give suggestings just by guessing.

Also, what is the use of timer 1? Didn't read the whole section of codes, but as far as I know, after a timer is set, it will return true only once, so even if you use a while loop to check it its content will only be executed at most once.
Title: Re: Algorithm Issues
Post by: Recluse on Tue 25/03/2008 01:13:21
Right, so I could reset the timer in the while loop and it would return true again once it completed.... Right...
Title: Re: Algorithm Issues
Post by: Gilbert on Tue 25/03/2008 01:15:44
But you're checking:

while (IsTimerExpired(1)...)

not while (!IsTimerExpired(1)...)

that means, if the timer has not expired the while loop will be skipped immediately.
So, I suspect the while loops won't even execute most of the time.
Title: Re: Algorithm Issues
Post by: Radiant on Tue 25/03/2008 09:18:55
Based on your code, you may want to consider using a custom variable "int my_timer" that you decrease every single loop, rather than doing IsTimerExpired().
Title: Re: Algorithm Issues
Post by: Pumaman on Tue 25/03/2008 15:21:34
Yes, IsTimerExpired will only return 1 once. So, you might want to restructure it like this:

    if (IsTimerExpired(1) && WalkSpeed < 8)
    {
      WalkSpeed++;
      lblCurrSpeed.Text = String.Format("Current Speed: %d", WalkSpeed);
      SetTimer(1, 10);
    }

You'd also need to make sure that the timer was started to begin with, otherwise this would never get called.
Title: Re: Algorithm Issues
Post by: Recluse on Tue 25/03/2008 18:04:11
Hooray CJ! Thanks for pointing out that I should be using an if statement. No point in sticking a while loop in there when repeatedly_execute is already looping for me, right?

For those who are interested, here's the piece of working code:

Be sure to call SetTimer(1, 10) from game_start() or elsewhere in your code.

int WalkSpeed = 0;

function repeatedly_execute()
{
  if(player.Moving == true){
    if (!IsTimerExpired(1) && WalkSpeed < 8)
    {
      WalkSpeed++;
      lblCurrSpeed.Text = String.Format("Current Speed: %d", WalkSpeed);
      SetTimer(1, 10);
    }
  }
  else{
    if (!IsTimerExpired(1) && WalkSpeed > 0)
    {
      WalkSpeed--;
      lblCurrSpeed.Text = String.Format("Current Speed: %d", WalkSpeed);
      SetTimer(1, 10);
    }
  }
}