T3D/Tutorials/SimpleFPSTutorial/Part6

From TDN

SIMPLE FPS TUTORIAL for Torque 3D



Back to Part Five: Custom Weapon Server Scripts

Part SIX




Custom Player:

We could edit the "game/art/datablocks/player.cs" file and add the new inventory items - but we can also make a new custom player, derived from the original player, and override only the functions and variables that we want.

Create a new file "game/art/datablocks/TutorialPlayer.cs". Player.cs contains almost everything that we need already, so let's link it to that.

datablock PlayerData(TutorialPlayer : DefaultPlayerData)
{

};


And that's it! "TutorialPlayer.cs" now has the same parameters as "Player.cs", because "TutorialPlayer is derived from the DefaultPlayerData". Next add our new allowable inventory items. The format is "maxInv[itemName/AmmoName] = MaxNumber". Add our new weapon items and their ammunition.

   maxInv[semiauto] = 1;
   maxInv[semiautoAmmo] = 20;
   maxInv[fullauto] = 1;
   maxInv[fullautoAmmo] = 30;


So now the TutorialPlayer can pick up 1 of each of our new, custom weapons (semiauto and fullauto) and the carry the maximum of 20 and 30 bullets of each (respectively).

There's one last thing to add to this new player, and that's to keep him inside the little route which we cut into the ground in Tutorial Part ONE.

runSurfaceAngle  = 50;

Now he can't climb up the walls. The whole thing should now look like:

datablock PlayerData(TutorialPlayer : DefaultPlayerData)
{
   runSurfaceAngle  = 50;//yorks edit - was 70

   //yorks additions
   maxInv[semiauto] = 1;
   maxInv[semiautoAmmo] = 20;
   maxInv[fullauto] = 1;
   maxInv[fullautoAmmo] = 30;
};

Now exec that file in "game/art/datablocks/datablockExec.cs" and make sure it's NOT above "exec("./player.cs");" from which it derives all of it's functions and other variables.

//custom datablocks
exec("./weapons/weaponEffects.cs");// <---- this needs to be loaded BEFORE the weapons that use it
exec("./weapons/semiauto.cs");
exec("./weapons/fullauto.cs");
exec("./TutorialPlayer.cs");//  <---- this needs to be loaded AFTER player.cs


Now that we've got our weapons and custom player scripted, let's give the player the ability to select them by pressing a key on the keyboard. Open up "game/scripts/client/default.bind.cs". In the Full Project Template from which we have derived "Simple_FPS_Tutorial", the stock rocketlauncher weapon has a keybind set to key "1".

moveMap.bindCmd(keyboard, "1", "commandToServer('use',\"RocketLauncher\");", "");

The "server command" tells the server (game host) to update the client/s (Player/s) with the weapon change. We're going to create similar keybindings for our new weapons, with "semiauto" set to "2" and "fullauto" set to "3". At the bottom of the file add:

//custom weapon keybinds
moveMap.bindCmd(keyboard, "2", "commandToServer('use',\"semiauto\");", "");
moveMap.bindCmd(keyboard, "3", "commandToServer('use',\"fullauto\");", "");


Now you have to add these new keybinds to "game/scripts/client/config.cs", but rather than doing it manually, we'll get the engine to do it for us. In the directory structure go up to the "Simple_FPS_Tutorial" folder and double click the "DeletePrefs.bat" file. This will clean/delete the preferences and keybinds config file and force the engine to create new and updated ones when you restart the project, and your new keybindings will be available.

And let's have a break from scripting, and (re)start Torque3D.



Part Seven: custom gameType