Algorithm Issues (FIX'D)

Started by Recluse, Tue 25/03/2008 00:48:39

Previous topic - Next topic

Recluse

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.

Code: ags
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);
  }
}
All your verbcoin are belong to us.

Gilbert

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.

Recluse

Right, so I could reset the timer in the while loop and it would return true again once it completed.... Right...
All your verbcoin are belong to us.

Gilbert

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.

Radiant

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().

Pumaman

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.

Recluse

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.

Code: ags
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);
    }
  }
}


All your verbcoin are belong to us.

SMF spam blocked by CleanTalk