Author Topic: Items that restore health and ammunition on pick up?  (Read 866 times)

I've been planning on making these TF2 ammo and health packs, but I lack the scripting skills. Let's assume I have all the models done that I need. How do I make an item like any other but it restores your health when you walk over it? It doesn't go into your inventory, it just disappears for X seconds and restores X amount of the players health. The time and amount of health is set in RTB prefs. Also, if I were using Tier Tactical 2 ammo system, how would I make it recover ammo?

I am not a scripter so I am looking for more of copypaste snippets of code to add to the item script.

Yup, here you go. I'll modify something from CityRPG.

If there's any syntax or logical errors you cannot resolve, I don't bite.

Code: [Select]
function Armor::onCollision(%this, %obj, %col, %vec, %speed)
{
// Sanity Checks.
// + Does %obj exist?
// + Does %col exist?
// + Is %obj a player?
// + Is %col an item?
if( isObject(%obj) && isObject(%col) && (%obj.getType() & $TypeMasks::PlayerObjectType) && (%col.getType() & $TypeMasks::ItemObjectType))
{
// Get the datablock once.
%colDB = %col.getDatablock();
// Get our datablock once.
%objDB = %obj.getDatablock();
// Reference variable. If we don't use the pick-up, we don't need to delete it.
%wasUsed = false;

// If we do not have either significant property, just return the parent.
if(!%colDB.pickupHealth && !%colDB.pickupAmmo)
return parent::onCollision(%this, %obj, %col, %vec, %speed);

// Does the item's datablock have the .pickupHealth variable defined and grater than 0?
// Does our %obj have damage? (Also, define %damage to save time)
if(%colDB.pickupHealth > 0 && (%damage = %obj.getDamageLevel()) > 0)
{
// Healing amount is our max HP multiplied by the percent healing of the pick-up.
%heal = %objDB.maxDamage * (%colDB.pickupHealth / 100);

// If our heal is as much or greater than our damage, damage sets to zero.
if(%heal >= %damage)
%damage = 0;

// Else, damage is the remainder.
else
%damage -= %heal;

// Apply our damage modified level.
%obj.setDamageLevel(%damage);
// Set this to true.
%wasUsed = true;

// If you want to add an emote, do it here.
//%obj.emote(healImage, true);
}

// I have no loving clue how the ammo pick-up system works. You'll have to figure it out.
if(%colDB.pickupAmmo > 0 && %someSortOfObjPropertyOrSomething)
{
// Do stuff to give ammo.
doStuffToGiveAmmo();
// Set wasUsed to try.
%wasUsed = true;
}

// If we were used, delete our item and DO NOT return the parent.
if(%wasUsed)
%col.delete();

return %obj;
}

return parent::onCollision(%this, %obj, %col, %vec, %speed);
}

Package this.
« Last Edit: March 23, 2011, 02:39:51 PM by Iban »

nicely commented.

for healing - i found there is an applyRepair() function you can use.  so you can change things up a little:
Code: [Select]
// Apply a repair amount based on the pick-up item.
%obj.applyRepair(%colDB.pickupHealth);
// Set this to true.
%wasUsed = true;

// If you want to add an emote, do it here.
//%obj.emote(healImage, true);

no need to mess with percentages and such -- just add XX health to the player.

The TF2 HP/ammo packs do percentages rather than fixed amounts so Iban's is fine. You may want to just make the item invisible for a few seconds rather than delete it entirely for static health pickups, though - check if it's a dropped one (Candy Cane/Medieval do this) or placed one first.

Playertypes usually have 100 health anyways.

Is there a way to determine the playertype of the player who picks up the item? Since the amount of health gained (Not percentage, fixed value) depends on the item type (Small, medium, large) and the playertype. I intend to make this combatible with my TF2 playertypes, so I can add special values to the playertypes themselves. (Like smallHealAmount = 50, mediumHealAmount = 100 and so on.)

Also the regained health should not go over the max health of the playertype.

The TF2 HP/ammo packs do percentages rather than fixed amounts so Iban's is fine. You may want to just make the item invisible for a few seconds rather than delete it entirely for static health pickups, though - check if it's a dropped one (Candy Cane/Medieval do this) or placed one first.
The idea is that the item respawns after 10 seconds after it has been picked up.

Playertypes usually have 100 health anyways.
Not with my TF2 playertypes.

Edit: Upon further inspection, they do percentages. Small 20%, medium 50% and large 100%. So I need three types of health kits.
Edit2: Tested the script, the item goes to my inventory. It should not do that.
« Last Edit: March 25, 2011, 08:53:58 AM by Demian »

Did you even read the script? You need to give the item a property.

Did you even read the script? You need to give the item a property.
Did I mention I am not a scripter? I do not know what you mean by a property or how to assign one.