Game Development Community

dev|Pro Game Development Curriculum

Torque Vehicle Resource

by Mike Stoddart · 03/01/2003 (4:57 pm) · 174 comments

Download Code File

The following changes can probably be applied to any version of Torque post 1.1.2, including the current CVS head. Follow the instructions carefully. Post any problems or comments as a response to the resource. I extracted these code changes from a more complete system so I apologise profusely if I have left any code snippets out or made any mistakes. Also, this is my first resource, so forgive any obvious/stupid/blatant mistakes.

1) Add the vehicle.cs file into "fps/server/scripts". This file contains generic logic for vehicle management, including mounting and dismounting. There's also a simple function for creating a vehicle when the user presses the appropriate key.

2) Copy the car.cs file from racing/server/scripts/ to fps/server/scripts. Modify any references in the script from:

"~/data/shapes/buggy/";

to:

"~/data/shapes/car/";

3) Add the following to the end of the datablock in car.cs. Note that you must add extra code for each extra seat in your vehicle. If you have extra seats, you must setup the poses and mount point transform for them. The pose just specifies a pose that the player will take when they mount that seat. For example, the driver sits but other passengers may stand. The mount point transform specifies which direction the player faces in that seat.
maxMountSpeed = 0.1;
   mountDelay = 2;
   dismountDelay = 1;

   stationaryThreshold = 0.5;
   maxDismountSpeed = 0.1;
   numMountPoints = 1;
   mountable = true;

   mountPose[0] = "Sitting";
   mountPointTransform[0] = "0 0 0 0 0 1 0";
   isProtectedMountPoint[0] = false;
4) Copy the whole "car" directory from racing/data/shapes/ into fps/data/shapes.

5) Add these lines into the function onServerCreated() in fps/server/scripts/game.cs:
exec("./vehicle.cs");
exec("./car.cs");
6) Add the following to the end of fps/client/scripts/default.bind.cs:
moveMap.bindCmd(keyboard, "e", "commandToServer('MountVehicle');", "");
moveMap.bindCmd(keyboard, "v", "commandToServer('AddCar');", "");
7) Modify player.cs replacing the existing functions with the following. This basically prevents the player from mounting a vehicle by walking into it. It also moves the logic for vehicle management from the player file to vehicle.cs that was added earlier.
function Armor::onMount(%this,%obj,%vehicle,%node)
{
   onPlayerMount(%this,%obj,%vehicle,%node);
}

function Armor::onUnmount( %this, %obj, %vehicle, %node )
{
   onPlayerUnmount(%this, %obj, %vehicle, %node);
}

function Armor::doDismount(%this, %obj, %forced)
{
   doPlayerDismount(%this, %obj, %forced);
}

function Armor::onCollision(%this,%obj,%col,%vec,%speed)
{
   if (%obj.getState() $= "Dead")
      return;

   // Try and pickup all items
   if (%col.getClassName() $= "Item")
      %obj.pickup(%col);
}
8) Add the files vehicleDriverMap.cs vehiclePassengerMap.cs to fps/client/scripts.

9) Add the following two lines to the function initClient() in fps/client/init.cs:
exec("./scripts/vehiclePassengerMap.cs");
   exec("./scripts/vehicleDriverMap.cs");
10) Add the following code to the end of fps/server/scripts/commands.cs. This code is executed when the player presses the key to dismount or change seats in the vehicle.
function serverCmdMountVehicle(%client)
{
   //Determine how far should the picking ray extend into the world?
   %selectRange = 3;

   // Only search for vehicles
   %searchMasks = $TypeMasks::vehicleObjectType;

   %pos = %client.player.getEyePoint();

   // Start with the shape's eye vector...
   %eye = %client.player.getEyeVector();
   %eye = vectorNormalize(%eye);
   %vec = vectorScale(%eye, %selectRange);

   %end = vectorAdd(%vec, %pos);

   %scanTarg = ContainerRayCast (%pos, %end, %searchMasks);

   // a target in range was found so select it
   if (%scanTarg)
   {
      %targetObject = firstWord(%scanTarg);
      echo("Found a vehicle: " @ %targetObject);
      onMountVehicle(%targetObject.getDataBlock(),
                     %client.player,
                     %targetObject);
   }
   else
   {
      echo("No object found");
   }
}

function serverCmdDismountVehicle(%client)
{
   doPlayerDismount(%client, %client.player, %true);
}


function serverCmdFindNextFreeSeat(%client)
{
   echo("serverCmdFindNextFreeSeat " @ %client.nameBase);

   // Is the vehicle moving? If so, prevent the player from switching seats
   if (isVehicleMoving(%client.player.mvehicle) == true)
      return;

   %newSeat = findNextFreeSeat(%client,
                               %client.player.mvehicle,
                               %client.player.mvehicle.getDataBlock());

   if (%newSeat != -1)
   {
      echo("Found new seat " @ %newSeat);

      setActiveSeat(%client.player,
                    %client.player.mvehicle,
                    %client.player.mvehicle.getDataBlock(),
                    %newSeat);
   }
   else
   {
      echo("No next free seat");
   }
}
11) Add the following to the end of fps/client/scripts/client.cs:
function clientCmdPopActionMap(%map)
{
   echo("Popping action map " @ %map);
   %map.pop();
}


function clientCmdPushActionMap(%map)
{
   echo("Pushing action map " @ %map);
   %map.push();
}

12) Comment out the following line in fps/client/scripts/default.bind.cs, otherwise you won't be able to use the 'e' key to enter/exit vehicles.

moveMap.bind(keyboard, e, toggleZoom);
#141
10/10/2006 (6:37 pm)
Thanks Tom.

I actually went ahead and put in a new WheeledVehicle() right into the bottom of the mission file. This worked perfectly and now I am fully able to move the car around the map and save its new position right in the editor.

One question though. Since I put this new WheeledVehicle() {...} in the mission file, do I need to add a call to Missioncleanup.add(...) somewhere? Or does it automatically delete when the mission exits?
#142
10/23/2006 (7:52 am)
I followed this tutorial and was able to add the vehicle with the mission editor bt not with the v key. I commented out the e key and everything but am still not able to mount the vehicle. i have no error in my console log or in game console. i need some help.
#143
11/04/2006 (9:43 pm)
Howdy all
For anyone attempting to use this resource and not able to make the "e" and "v" work as intended, you must make sure you add the binds to the config.cs files by hand. The system apparently does not update Cmd flavors of the binds automatically to the config.cs files. There is 2 config.cs files, i didnt know which one did it so i changed them both. The are identical. The directions only state the default.bind.cs.
Also, delete the DSO's to make the scripts recompile.

moveMap.bindCmd(keyboard, "e", "commandToServer('MountVehicle');", "");
moveMap.bindCmd(keyboard, "v", "commandToServer('AddCar');", "");

After adding this to the config.cs, everything works like a charm :)

Good luck !!
#144
11/27/2006 (10:18 am)
Hi, i would love to get this resource to work, i have noticed that some of the coding has been changed with the new 1.5, i have added the resource and i have gotten 2 errors. It would appear as im searching that some of this code already exists in the 1.5 coding. As a new coder i have followed the txt in the file. The errors are as follows

{
if (isObject(%client.player))
%client.player.playDeathAnimation();
}

// vehicle addon -----------------------------------
function serverCmdFindNextFreeSeat(%client);
##f##unction serverCmdDismountVehicle(%client);
{
^ doPlayerDismount(%client, %client.player, %true);
}
cho("serverCmdFindNextFreeSeat " @ %client.nameBase);

I have tried several permatations of the function command but its not clearing the error.
The second error ...

}
maxMountSpeed = 0.1;
##^##mountdelay = 1;
dismountDelay = 1;
stationaryThreshold = 0.5;
maxDismountSpeed = 0.1;
numMountPoints = 1;
mountable = true;
mountPose[0] = "Sitting";
mountPointTransform[0] = "0 0 0 0 0 1 0";
isProtectedMountPoint[0] = false;
>>> Error report complete.

I dont know what is causing the errors, everyone else has appeared to be able to apply it to the stock FPS, this is the second patch that doesnt seem to work "out of the box". any suggestions
#145
11/27/2006 (3:47 pm)
is that what your code looks like because thats not what the resource looks like to me.
else
   {
      echo("No object found");
   }
}

function serverCmdDismountVehicle(%client)
{
   doPlayerDismount(%client, %client.player, %true);
}

there is no

function serverCmdFindNextFreeSeat(%client);

which is a line of code that would cause an error


on the second error

is there a ^ before the mountdelay in the actual code because that would cause an error.
#146
11/27/2006 (7:40 pm)
there is no ^ in the actual code, its ok, i just restored the orginal code and add my vehicles later, its funny all i want is like 3-4 vehicles hidden around the game. its mostly going to be a easter egg, thanks anyway master treb. I really need to put a ad in here for a coder to be on my team.
#147
11/27/2006 (8:35 pm)
vehicles are going to be a tough easter egg. They are quite difficult to work. I prefer weapons.
#148
12/16/2006 (5:51 pm)
Hi Mike, I'm trying to do this work... In the Starter.FPS I load the game normally, I Add a "default vehicle", after this, when I collide with the vehicle and press the key to the player go to the vehicle, nothing happens, What is wrong? (I'm using the TGE 1.5, and, I Followed all steps of your tutorial).

Regards...
#149
12/22/2006 (3:09 am)
Hi, i tried this resource , i got it running without crashing tge (good thing) i pressed v to add a car (good thing)

but i tried to enter the car with E but it didnt enter... Yes i commented out the line for zoom...

Then i thought it might have bin cause i didnt change the thing in car.cs to make the path data/shapes/car/ not buggy then i tried that to every bit of code in car.cs then when i launched the mission it has an error message:

you do not have the same art bla bla bla Data/shapes/buggy/particlesomething.png

how come it sais "/BUGGY" didnt i change the lines to shapes/car? or is there another file i need to edit too then car.cs, im using TGE 1.5 HOPE THIS RESOURCE IS STILL ALIVE NOT DEAD ( the creator i mean )

Apreciate any help thanks.
#150
01/14/2007 (11:25 am)
there has to be a file somewhere wanting that particle you could fix it the cheap way by creating that file path, or if you have torsion you could search all the files for "/buggy/" and find where it is that way.
#151
02/13/2007 (6:45 am)
Wow it actually works once you delete config.cs. I've got this to work on TGEA RC1, TGE 1.42, and TGE 1.5 thanks alot.
#152
02/13/2007 (8:23 am)
TGE doesnt used default.bind it uses config.cs, if config.cs is missing it falls back on default.bind, I think. It creates a new config.cs at the end of every game so if you change the keybinds in the game it sticks. This resource only puts the keybinds into default.bind. if you put the following code into config.cs it should work without deleting default.bind.

moveMap.bindCmd(keyboard, "e", "commandToServer(\' MountVehicle \');", ""); 
moveMap.bindCmd(keyboard, "v", "commandToServer(\' AddCar \');", "");

The syntax in config.cs is a bit different (I dont know or see why it is different Im not sure if it has to be) I havent tested this so if it doesnt work reply.

Oh and you will have to comment out the command that maps
#153
02/28/2007 (8:01 pm)
I'm not having any luck changing the seat translation via:
mountPointTransform[0]   = "0 10 0 0 0 1 0";
I can rotate no problem, but no translation (x,y,z).
(Using TGE 1.5)
#154
02/28/2007 (8:14 pm)
I think I remember having problems with that in older versions of Torque. You would probably be best off changing the mount point on the model. If you really wanted to offset you could check inside the source code and see if there is any expandability for mount point offsets, I dont remember whether or not there is.
#155
03/05/2007 (7:23 pm)
Has anyone had a problem were you can't respawn if you die inside the vehicle?
I can respawn as normal if I die outside of the vehicle by clicking the left mouse button.

Edit:
The vehicle was moving when I died so it looks like the ActionMap is not getting called...
#156
03/06/2007 (8:41 am)
I'm thinking I could do a check to see if the player is mounted in a vehicle on death. Then if its true make a call to pop the vehicle actionmap and push the player actionmap. I'll post the code if I can figure out a correct way to do this.

I forgot to mention that the player will not take damage if this variable is set to true in the vehicle datablock.
isProtectedMountPoint[1] = false;
#157
03/06/2007 (5:59 pm)
Ok, I got the actionmap to reset to the player by adding the code in bold in player.cs

function Armor::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
{
   if (%obj.getState() $= "Dead")
      return;
   %obj.applyDamage(%damage);
   %location = "Body";

   // Deal with client callbacks here because we don't have this
   // information in the onDamage or onDisable methods
   %client = %obj.client;
   %sourceClient = %sourceObject ? %sourceObject.client : 0;

   if (%obj.getState() $= "Dead") [b]
   {
       // check if player died in a vehicle
       if(%obj.isMounted())
       {
           CommandToClient(%obj.client, 'PopActionMap', $Vehicle::moveMaps[%obj.mSeat]);
           CommandToClient(%obj.client, 'PushActionMap', moveMap);
       }
[/b]
       %client.onDeath(%sourceObject, %sourceClient, %damageType, %location); [b]
   }
[/b]
      
}

I think you only need to do this if you want the player to take damage while he is in the vehicle.

Edit: fixed a typo...
#158
04/06/2007 (4:48 pm)
I can mount and drive, but I am having trouble dismounting. I mapped the "k" key to dismount, but when I hit it, all I get on the concsole is that it can't find two objects in the vehicle.cs file. Here are the two errors.

CitySimFPS/server/scripts/vehicle.cs (213): Unable to find object: '' attempting to call function 'getVelocity'
CitySimFPS/server/scripts/vehicle.cs (217): Unable to find object: '' attempting to call function 'isMounted'
#159
04/07/2007 (12:27 pm)
function Armor::doDismount(%this, %obj, %forced)
{
   doPlayerDismount(%this, %obj, %forced);
}

That line in your player.cs is what calls doPlayerDismount, it is sending a whole lot of nothing to the second field %obj. Make sure your code looks exactly like the above... if it does the change it to.

function Armor::doDismount(%this, %obj, %forced)
{
   echo("my Armor is >"@%obj@"<");
   doPlayerDismount(%this, %obj, %forced);
}


That should return "my Armor is ><" in the console if the armor is undefined (Your armor has been deleted) or "my Armor is >1234<" (1234 being any numbers) if there is an armor in which case something weird is going on. If you can tell me which one it is I'll try to help further.
#160
07/04/2007 (2:12 pm)
Anyone got this resource working with the vehicle packs on GG, other than the car pack? I can mount the vehicle, but can only rotate 360 degrees, but not move the vehicle.

Edit: Its been one of those days today! Managed to get it working, it was a problem with my player.cs file - think I will need to get Torrison :)

Great resource by the way!