Game Development Community

Mouse Button Repeat

by Edith Cowan University (#0002) · in Torque Game Engine · 05/03/2009 (12:33 am) · 3 replies

Hey guys I've been ripping my hair out about this for hours and hours now, just wondering if there is any way to make a function repeat itself whilst MouseButton1 has a %val = 1.

Using
while(%val $= 1)
does not work properly as it processes it way to fast and essentially crashes the Torque Engine. I've also tried using
if(%val $= 1)
and that doesn't work as it only passes the function once per click.

The functionality of this code is it checks the distance (only X, Y) in vectors between the player and 4 objects. It detects which one is the closest, then checks if it's under 3.0 units away from the player. If it is it calls the function "repair" which repairs the closest in-range object to the player.

Essentially what I want it to do is perform the code I have made repeatedly with a 2second delay between each performance. Any tips? here's my code that's called on the Right Click:
function blockInteract(%val)
{
   //echo("Right Click");
   %playerX = getWord($Player.getTransform(),0);
   %playerY = getWord($Player.getTransform(),1);
   %playerXY = %playerX SPC %playerY;
   //echo("Player XY: " @ %playerXY);
   %distanceList = "";
   $usingID = 0;
   
   for(%i = 0; %i < $activeTargets; %i++)
   {
      %objectX = getWord($barricadeList.getObject(%i).getTransform(),0);
      %objectY = getWord($barricadeList.getObject(%i).getTransform(),1);
      %objectXY = %objectX SPC %objectY;
      //echo("Object " @ %i @ " XY: " @ %objectXY);
      
      %distance = VectorDist(%playerXY, %objectXY);
      //echo("Player to Object " @ %i @ " Vector: " @ %distance);
      
      if(%i $= 0){
         %distanceList = %distance;}
      else{
         %distanceList = %distanceList SPC %distance;}
      //echo("Distance List: " @ %distanceList);
   }
   
   for(%i = 0; %i < $activeTargets; %i++)
   {
      if(getWord(%distanceList, $usingID) > getWord(%distanceList, %i))
      {
         $usingID = %i;
      }
      //echo("FUCKING USING ID: " @ $usingID);
   }
   
   //echo("Distance: " @ getWord(%distanceList, $usingID));
   
   if(getWord(%distanceList, $usingID) < 3.0)
   {
        repair();
   }
}

function repair()
{
   if($usingID $= 0)
   {
      if($NorthBarricadeExists $= false)
      {
         %pos = "-2.75796 -12.3972 0.00 0";
         $barricadeList.getObject(0).setTransform(%pos);
         $NorthBarricadeExists = true;
      }
      $NorthBarricadeDamage -= 0.05;
      if($NorthBarricadeDamage < 0.0)
         $NorthBarricadeDamage = 0.0;
      echo("NORTH REPAIR: " @ $NorthBarricadeDamage);
   }
   if($usingID $= 1)
   {
      if($SouthBarricadeExists $= false)
      {
         %pos = "0.81628 10.3957 0.00 0";
         $barricadeList.getObject(1).setTransform(%pos);
         $SouthBarricadeExists = true;
      }
      $SouthBarricadeDamage -= 5.0;
      if($SouthBarricadeDamage < 0.0)
         $SouthBarricadeDamage = 0.0;
      echo("SOUTH REPAIR: " @ $SouthBarricadeDamage);
   }
   if($usingID $= 2)
   {
      if($WestBarricadeExists $= false)
      {
         %pos = "-13.5122 -0.52322 0.00 0";
         $barricadeList.getObject(3).setTransform(%pos);
      }
      $WestBarricadeDamage -= 5.0;
      if($WestBarricadeDamage < 0.0)
         $WestBarricadeDamage = 0.0;
      echo("WEST REPAIR: " @ $WestBarricadeDamage);
   }
   if($usingID $= 3)
   {
      if($EastBarricadeExists $= false)
      {
         %pos = "14.162 3.84743 0.00 0";
         $barricadeList.getObject(3).setTransform(%pos);
      }
      $EastBarricadeDamage -= 5.0;
      if($EastBarricadeDamage < 0.0)
         $EastBarricadeDamage = 0.0;
      echo("EAST REPAIR: " @ $EastBarricadeDamage);
   }
}

#1
05/03/2009 (12:41 am)
I think you could find a solution if you examine the weapon scripts. Specifically follow how the onFire function works. Might even find the state system compatible with your intended results.
#2
05/03/2009 (12:45 am)
Thanks, I'll check that out now :)
#3
05/03/2009 (8:04 am)
That would require that you make a weapon image that calls your function when you fire, and make the fire state very short. You'll need to mount the image in slot 1 (not the usual 0) so that the right mouse trigger calls the image's fire state.