Game Development Community

dev|Pro Game Development Curriculum

How to add drivable car into Torque FPS!

by Maximillian Brewer · 01/31/2009 (6:19 pm) · 14 comments

Before we start, I wan't to make it clear that the following code is not mine, but has been given to me to create a resource.

Step 1: Copy car.cs from starter.racing/server/scripts and starter.racing/data/shapes/buggy folder to the corresponding folders in your fps.

Step 2: Add the following line in bold to WheeledVehicleData::onAdd() in car.cs
// Only power the two rear wheels...
   %obj.setWheelPowered(2,true);
   %obj.setWheelPowered(3,true);
   
   [b]%obj.mountable = true;[/b]

Step 3: Open up player.cs and add the following bold snippets:

In Armor::onCollision():
// Mount vehicles
%this = %col.getDataBlock();
if ((%this.className $= WheeledVehicleData) && %obj.mountVehicle && %obj.getState() $= "Move" && %col.mountable) 
{
    // Only mount drivers for now.
    %node = 0;
    %col.mountObject(%obj,%node);
    %obj.mVehicle = %col;
    [b]%obj.client.setcontrolobject(%col);[/b]
}
In Armor::doDismount():
if (%forced && %success == -1)
      %pos = %oldPos;

   [b]%obj.unmount();[/b]
   %obj.mountVehicle = false;
   %obj.schedule(4000, "mountVehicles", true);

Step 4: Now add this code section at the very end of player.cs
function serverCmdsetPlayerControl(%client)
{
     %client.setControlObject(%client.player);
}

function JumpOutOfCar()
{
    commandToServer('setPlayerControl');
    schedule(500, 0, "jump");
    schedule(600, 0, "jump");
}

moveMap.bindCmd(keyboard, "ctrl x","JumpOutOfCar();","");

Step 5: Now if you go back to car.cs, in datablock WheeledVehicleData(DefaultCar) add the bold line:
{
   category = "Vehicles";
   shapeFile = "~/data/shapes/buggy/buggy.dts";
   emap = true;
   [b]mountPose[0] = sitting;[/b]

Step 6: Now just go into server/scripts/game.cs and add the bold line:
exec("./crossbow.cs");
   [b]exec("./car.cs");[/b]


Well thats it! All that's left to do, is load up your game, drop in the default buggy, and then run into it!

------------------------------------------------------------------------
For any help please leave a comment :)

#1
01/31/2009 (11:56 pm)
Excellent, thanks for that! I'd played with getting the buggyinto starter.fps before, but I really didn't want to jump into the scripting to get Kork mounted on it. I just settled for console commands ;)
#2
02/01/2009 (10:21 am)
Thanks Max for the resource!
#3
02/04/2009 (12:45 am)
Glad you both like it! :)
#4
02/19/2009 (7:38 am)
What version of the engine are you putting this in? I put it into the AFX 1.8.0 beta version last night, and although it's not causing any errors, I'm not picking the vehicle up on collision. One thing that I did note was that the "mountpose[0]" step was already in my car.cs file, at the right line position, so my code may be a little "different", but I thought it would do no harm to ask what engine you were working in.
#5
02/20/2009 (2:19 am)
I am using TGE 1.5.2 as this is what i own, and it hasn't been tested with any other versions (TGEA)

From Max
#6
02/24/2009 (7:18 am)
that's work, thx^^/

I try to use this script on the "tutorial.base"'s "blue guy",but it's do not work...
#7
02/24/2009 (8:34 am)
@Netwyrm: I haven't tried in AFX, but it works in TGEa 1.8.1.
@Ryo: this will not work in tutorial.base without modification of tutorial.base & changes to the resource code. The example here was based on and relies on code present in starter.fps
#8
03/02/2009 (6:35 am)
Michael did you modify this code for ir to work on the tgea 1.8.1 or you just copied it. Thanks.
#9
03/07/2009 (12:08 am)
@Nicolas: I wrote the instructions for Maximillian to allow mountable vehicles in the TGE starter.fps, this resource isn't needed at all for TGEa 1.8.1.
#10
03/13/2009 (8:20 pm)
I'm new to TGE, so i still can't figure out how to spawn the car. The Buggy is in my shapes folder, I did all the scripting, now I just don't know what to do.
Jon
#11
03/13/2009 (8:25 pm)
Nevermind, I found that I didn't take the </b> out of the onAdd section.
Thanks anyways
#12
04/04/2009 (2:18 am)
Thank you,this tutorial helps me a lot!
#13
06/19/2009 (9:50 am)
CAN I tell you how much I love Torque? Implemented this in AFX 1.5.2 for TGE and not only does it work beautifully, but some serendipitous stuff....

I have a pathfinding resource in my engine version, and an orc who was on his way somewhere else ran into my car while my human player was driving it. Since the Armor:onCollision handler allows ANY player to take control of the vehicle, the Orc essentially hijacked my car from me.

I think he was dragging me for a while underneath the car because I could not move. Finally I jumped and I was released from this awful form of abuse.

This engine is so fun to work with. I will have to modify my code to make sure that NPCs cannot hijack vehicles.


#14
06/20/2009 (5:54 am)
@ nmuta: Hey! did you work out how to not let your NPCs hi-jack the car yet?

EDIT: I have an idea on how to make NPCs not get in the car also, and then make it so that if you do drive into them, they die. I plan on getting it working like that today, and if so, i will post here and update the resource too with a section called "Advenced Car in FPS"