Game Development Community

dev|Pro Game Development Curriculum

Basic Health Demo

by James Yong · 04/04/2004 (8:02 am) · 2 comments

1. In default.bind.cs, add this line:

//------------------------------------------------------------------------------
// Item manipulation
//------------------------------------------------------------------------------

moveMap.bindCmd(keyboard, "h", "commandToServer('use',\"HealthKit\");", "");
moveMap.bindCmd(keyboard, "2", "commandToServer('use',\"Crossbow\");", "");

2. In demo/client/config.cs, append this line:
moveMap.bindCmd(keyboard, "h", "commandToServer('use',\"HealthKit\");", "");

3. Copy from example folder of your torque sdk and add health.cs to demo/server/scripts.
health.cs should contains

Quote:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

// Inventory items. These objects rely on the item & inventory support
// system defined in item.cs and inventory.cs

//-----------------------------------------------------------------------------
// Health kits can be added to your inventory and used to heal up.
//-----------------------------------------------------------------------------

datablock ItemData(HealthKit)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.
category = "Health";

// Basic Item properties
shapeFile = "~/data/shapes/items/healthKit.dts";
mass = 1;
friction = 1;
elasticity = 0.3;

// Dynamic properties defined by the scripts
pickupName = "a health kit";
repairAmount = 50;
};

//this function will activate when the player pressed 'h' to use his healthkit.
function HealthKit::onUse(%this,%user)
{
// Apply some health to whoever uses it, the health kit is only
// used if the user is currently damaged.
if (%user.getDamageLevel() != 0) {
%user.decInventory(%this,1);
%user.applyRepair(%this.repairAmount);
if (%user.client)
messageClient(%user.client, 'MsgHealthKitUsed', '\c2Health Kit Applied');
}
}


//-----------------------------------------------------------------------------
// Health Patchs cannot be picked up and are not meant to be added to
// inventory. Health is applied automatically when an objects collides
// with a patch.
//-----------------------------------------------------------------------------

datablock ItemData(HealthPatch)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.
category = "Health";

// Basic Item properties
shapeFile = "~/data/shapes/items/healthPatch.dts";
mass = 1;
friction = 1;
elasticity = 0.3;

// Dynamic properties defined by the scripts
repairAmount = 20;
maxInventory = 0; // No pickup or throw
};

function HealthPatch::onCollision(%this,%obj,%col)
{
// Apply health to colliding object if it needs it.
// Works for all shapebase objects.
if (%col.getDamageLevel() != 0 && %col.getState() !$= "Dead" ) {
%col.applyRepair(%this.repairAmount);
%obj.respawn();
if (%col.client)
messageClient(%col.client, 'MsgHealthPatchUsed', '\c2Health Patch Applied');
}
}


4. Create a new items folder under data/shapes, and copy from example folder of your torque sdk and add healthKit.dts, healthKit.jpg, and healthPatch.dts.

5. Check demo/server/demo/player.cs has the following line:
// Allowable Inventory Items
maxInv[BulletAmmo] = 20;
maxInv[HealthKit] = 1;


6. Add the following line to fps/server/scripts/game.cs
// Load up basic datablocks, objects etc.
exec("./audioProfiles.cs");
exec("./camera.cs");
exec("./markers.cs");
exec("./triggers.cs");
exec("./inventory.cs");
exec("./shapeBase.cs");
exec("./health.cs");
exec("./staticShape.cs");

7. Go to your fps game, press F11 to acccess the mission editor and then F4 to access the world editor creater. You will find 4 folders on your right, namely, interiors, shapes, static shapes and mission objects. Click on shapes -> Health. Then choose either healthkit or health patch to add it into the mission. Remember to click on the menu bar File -> Save Mission As if you want the change to persist after you exit from the mission.

Thats all. Hope it is useful to newbies.

Edited: To make the some lines in bold

#1
10/31/2007 (5:23 am)
I can't get "%user.applyRepair(%this.repairAmmount)" to work
#2
05/28/2009 (12:59 pm)
I'm having some problems with getting the health kit to add health automatically since in the game i'm working on we have no inventory. When my player touches the kit, nothing happens even in the console. If a different GUI has been made other than the basic one, could that be having a impact on it? or the code being changed?