Game Development Community

Help w/ AutoRun Toggle

by Kaya Dogan · in Torque Game Engine Advanced · 08/13/2006 (2:36 am) · 3 replies

We are trying to implement some kind of AutoRun toggle, so the player doesn't need to keep 'W' pressed over long distances. Here's the code we currently have:

$mvAutoRun = false;

// We don't want the player to keep 'W' pressed over a long distance, cause
// it might become annoying over time. That's what we have 'AutoRun' for ;)
function toggleAutoRun(%val)
{
   // TODO: Stop AutoRun once numlock is pressed again...
   //       At this time, you need to move your character via WASD
   //       to stop AutoRun...
   
   if($mvAutoRun)
   {
      $mvAutoRun = false;
   }
   else
   {
      $mvAutoRun = true;
      AutoRun();
   }
}

function AutoRun()
{
   if($mvFreeLook)
      $mvFreeLook = false;
   
   // Move it!
   $mvForwardAction = 1;
}

moveMap.bind(keyboard, numlock, toggleAutoRun);

I know that $mvAutoRun is ALWAYS true atm, so there must be another condition we have to check, but I can't figure out which condition and how I can make it work the way it should. When AutoRun is active and the player presses NumLock again, AutoRun should stop. That doesn't work atm as toggleAutoRun is not correct. Maybe you can help us out and point me into the right direction or even correct the code and explain the changes a bit. Thanks a lot! :)

#1
08/13/2006 (10:15 am)
A really cheesy way to add autorun is to just make an input function exactly like moveForward except it only sets the state if %val is true. This doesn't turn off when you sidestep or move back though so if you want that you need to do a workaroud.

Actually...

if you made something like

AutoRun(%val)
{
$mvForward = 1;
$autorun = true;
}

then you go into moveleft moveright moveforward moveback functions and add

if($autoRun)
{
$mvForward = 0; (except in moveforward, where you'd just turn off autorun)
$autoRun = false;
}

then I think you'd have a pretty comprehensive autorun implementation.
#2
08/17/2006 (9:39 am)
You're function doesn't work because it is fired twice.

$mvAutoRun = false;

// We don't want the player to keep 'W' pressed over a long distance, cause
// it might become annoying over time. That's what we have 'AutoRun' for ;)
function toggleAutoRun(%val)
{
   // TODO: Stop AutoRun once numlock is pressed again...
   //       At this time, you need to move your character via WASD
   //       to stop AutoRun...
   
   [b]if(%val)
   {[/b]
      if($mvAutoRun)
      {
         $mvAutoRun = false;
      }
      else
      {
         $mvAutoRun = true;
         AutoRun();
      }
   [b]}[/b]
}

function AutoRun()
{
   if($mvFreeLook)
      $mvFreeLook = false;
   
   // Move it!
   $mvForwardAction = 1;
}

moveMap.bind(keyboard, numlock, toggleAutoRun);

Its is called once with %val = 1. (key down) and then with %val = 0 (key up). So it would turn autorun on then off!

Hope this helps. ;)

-- Ricky
#3
08/17/2006 (12:28 pm)
Yay! Thanks, m8s. I knew I must have been missing something. One step closer to a nice set of game controls for our project.