modifying the health script to replenish energy
by Dr. John Nobody · in Torque Game Engine · 06/11/2003 (9:27 pm) · 15 replies
hey, I need an item which replenishes the characters energy instead of health. I figure the easiest way would be to modify the healthpatch script. Heres what I did...
I figure all I need to do now is write a line of code to add energy to the player who collides with the object, I figure theres probably some function to do this... am I right? can I just change
%col.applyRepair(%this.repairAmount);
to use an energy replenishing function instead of applyRepair? does anyone know what this mystery function might be? thanks for any help you can offer!
datablock ItemData(Energy)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.
category = "Energy";
// Basic Item properties
shapeFile = "~/data/shapes/items/healthPatch.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
// Dynamic properties defined by the scripts
energyAmount = 20;
maxInventory = 0; // No pickup or throw
};
function Energy::onCollision(%this,%obj,%col)
{
// Apply Energy to colliding object if it needs it.
// Works for all shapebase objects.
// if (%col.getDamageLevel() != 0 && %col.getState() !$= "Dead" ) { I commented this out because I dont care if they are full on energy
%col.applyRepair(%this.repairAmount);
%obj.respawn();
if (%col.client)
messageClient(%col.client, 'Energy found', '\c2Energy found');
//}
}I figure all I need to do now is write a line of code to add energy to the player who collides with the object, I figure theres probably some function to do this... am I right? can I just change
%col.applyRepair(%this.repairAmount);
to use an energy replenishing function instead of applyRepair? does anyone know what this mystery function might be? thanks for any help you can offer!
#2
Thanks...
06/11/2003 (11:59 pm)
@LabRat: Just curious, did you know all those off the top of your head? If so, wow... good memory. If not, where did you get them? Just out of one of the actual scripts or is there some kind of dump or documentation I can look up?Thanks...
#3
I want to know too Labrat. You suspiciously know a lot about torque. Do you have a black book or something?
Alex
06/12/2003 (1:47 am)
Hehe.I want to know too Labrat. You suspiciously know a lot about torque. Do you have a black book or something?
Alex
#4
%object.setEnergyLevel(%object.getEnergyLevel() + 20);
will that do it? or will that crash torque if it goes over the max energy level?
my other idea is to use
%object.getEnergyPercent(); and %object.getEnergyLevel();
and then use the current energy level and percentage to calculate the players max energy level, then set energy approprietly so it doesnt go over, so once again will it hurt things if I do it the first way? and second, does getenergypercent return a number between 0-100? or 0-1?
oh and I had a kinda crazy third idea, I was thinking I could use
%object.setRechargeRate();
and just set the recharge rate rediculously high for one cycle, but I'm not sure how to just do it for one cycle, does anyone know a little more aobut this and how it works? like does it apply the recharge every half a second? every frame? whenevery it feels like it? and if so any ideas how I could set it up for cycle and then set it back down? (like I said, its kinda a crazy idea)
thanks guys
06/12/2003 (6:46 am)
ooo thanks harold! these are gonna come in handy. I see that the energy functions are a little less robust then the health ones, no real way of applying additional energy. Got a couple questions, first off, will it hurt anything if I try and assign more energy than the player's max value? Will it simply cut off at the max or will it crash my whole program? ( I'm thinking something along the lines of %object.setEnergyLevel(%object.getEnergyLevel() + 20);
will that do it? or will that crash torque if it goes over the max energy level?
my other idea is to use
%object.getEnergyPercent(); and %object.getEnergyLevel();
and then use the current energy level and percentage to calculate the players max energy level, then set energy approprietly so it doesnt go over, so once again will it hurt things if I do it the first way? and second, does getenergypercent return a number between 0-100? or 0-1?
oh and I had a kinda crazy third idea, I was thinking I could use
%object.setRechargeRate(
and just set the recharge rate rediculously high for one cycle, but I'm not sure how to just do it for one cycle, does anyone know a little more aobut this and how it works? like does it apply the recharge every half a second? every frame? whenevery it feels like it? and if so any ideas how I could set it up for cycle and then set it back down? (like I said, its kinda a crazy idea)
thanks guys
#5
You can also do a %object.dump(); to get a list of functions and properties for an object printed to the console.
06/12/2003 (6:46 am)
Some of them I remembered off the top of my head. But for verification I go to the C++ source code where all of the functions are listed.You can also do a %object.dump(); to get a list of functions and properties for an object printed to the console.
#6
one last question, anyone know how to make an item that would say, give you infinite energy for 15 seconds?
06/12/2003 (7:35 am)
ok dont worry about answering that stuff up there, I played around a bit and the first way seems to work just fine, thanks allone last question, anyone know how to make an item that would say, give you infinite energy for 15 seconds?
#7
Something like this should be what you need. The player datablock will need to have a rechargeRate specified,
06/12/2003 (4:15 pm)
function InfEnergy::onCollision(%this,%obj,%col)
{
%origRate = %col.getDatablock().rechargeRate;
%col.setRechargeRate(1.0);
%col.schedule(15000, setRechargeRage,%origRate);
%obj.respawn();
if (%col.client)
messageClient(%col.client, 'MsgInfEnergyUsed', '\c2You will have Infinite Energy for 15 seconds!');
}Something like this should be what you need. The player datablock will need to have a rechargeRate specified,
#8
%currentAmmo = %col.getInventory(%this.ammo);
%col.client.setAmmoAmountHud(%currentAmmo + 10);
in the onCollision function with no luck, anyone know the correct way to do this?
I would also appriciate a little insight on how ammo works in general. It doesnt seem to have an onCollision function in the rifle script, or a place to define how much ammo to give on pickup
07/22/2003 (5:37 pm)
I thought I'd write this here instead of cluttering up the forums with another thread. I'd like to expand on this script to not only replenish energy, but RifleAmmo as well. I tried this%currentAmmo = %col.getInventory(%this.ammo);
%col.client.setAmmoAmountHud(%currentAmmo + 10);
in the onCollision function with no luck, anyone know the correct way to do this?
I would also appriciate a little insight on how ammo works in general. It doesnt seem to have an onCollision function in the rifle script, or a place to define how much ammo to give on pickup
#9
07/23/2003 (4:01 pm)
Bump :)
#10
07/24/2003 (11:58 am)
Sorry, another bump :)
#11
01/28/2007 (5:30 am)
Deos anyone have the actuall code for this that would work? im trying to make a game that uses a halo like style of health
#12
First to be called
Then this is called
Then finally this..
01/28/2007 (9:50 pm)
All pickups in stock TGE and TGEA are handled by these functions.First to be called
function Armor::onCollision(%this,%obj,%col)
{
if (%obj.getState() $= "Dead")
return;
// Try and pickup all items
if (%col.getClassName() $= "Item")
%obj.pickup(%col);Then this is called
function ShapeBase::pickup(%this,%obj,%amount)
{
// This method is called to pickup an object and add it
// to the inventory. The datablock onPickup method is actually
// responsible for doing all the work, including incrementing
// the inventory.
%data = %obj.getDatablock();
// Try and pickup the max if no value was specified
if (%amount $= "")
%amount = %this.maxInventory(%data) - %this.getInventory(%data);
// The datablock does the work...
if (%amount < 0)
%amount = 0;
if (%amount)
return %data.onPickup(%obj,%this,%amount);
return false;
}Then finally this..
function ItemData::onPickup(%this,%obj,%user,%amount)
{
// Add it to the inventory, this currently ignores the request
// amount, you get what you get. If the object doesn't have
// a count or the datablock doesn't have maxIventory set, the
// object cannot be picked up.
%count = %obj.count;
if (%count $= "")
if (%this.maxInventory !$= "") {
if (!(%count = %this.maxInventory))
return;
}
else
%count = 1;
%user.incInventory(%this,%count);
// Inform the client what they got.
if (%user.client)
messageClient(%user.client, 'MsgItemPickup', '\c0You picked up %1', %this.pickupName);
// If the item is a static respawn item, then go ahead and
// respawn it, otherwise remove it from the world.
// Anything not taken up by inventory is lost.
if (%obj.isStatic())
%obj.respawn();
else
%obj.delete();
return true;
}
#13
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');
}
}
creating a new one wouldnt be too difficult.
function EnergyPatch::onCollision(%this,%obj,%col)
{
// Apply energy to shapebase objects instead of health
if (%col.getDamageLevel() != 0 && %col.getState() !$= "Dead" ) {
%col.setEnergyLevel(%col.getEnergyLevel() + %this.repairAmount);
%obj.respawn();
if (%col.client)
messageClient(%col.client, 'MsgEnergyPatchUsed', '\c2Energy Patch Applied');
}
}
this would suspect %this.repairAmount contains the energy to be applied in much the same way as with the healthpatch. its a bit ugly since there are no applyEnergy functions (you are free to write those i suppose) but it works.
08/09/2007 (4:07 am)
Not sure if its still usefull, but if you want to apply energy instead of health in the same style as picking up a healthPatch (the one u dont have to use pressing H by default) you could look into this: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');
}
}
creating a new one wouldnt be too difficult.
function EnergyPatch::onCollision(%this,%obj,%col)
{
// Apply energy to shapebase objects instead of health
if (%col.getDamageLevel() != 0 && %col.getState() !$= "Dead" ) {
%col.setEnergyLevel(%col.getEnergyLevel() + %this.repairAmount);
%obj.respawn();
if (%col.client)
messageClient(%col.client, 'MsgEnergyPatchUsed', '\c2Energy Patch Applied');
}
}
this would suspect %this.repairAmount contains the energy to be applied in much the same way as with the healthpatch. its a bit ugly since there are no applyEnergy functions (you are free to write those i suppose) but it works.
#14
how does energy affect the player or game state? i know health affects the health bar, creates a damage flash, and sets the damage state, but what does chaning the energy level do?
08/09/2007 (7:40 am)
A bit off topic but...how does energy affect the player or game state? i know health affects the health bar, creates a damage flash, and sets the damage state, but what does chaning the energy level do?
#15
08/09/2007 (5:31 pm)
You can enable certain tasks that the player may perform to drain energy. For example, jumping, sprinting, jetting (ala Tribes) etc. When you run out of energy the action is no longer permitted, until your energy level has regenerated.
Torque Owner Harold "LabRat" Brown
%object.getRechargeRate();
%object.setRechargeRate(
%object.getEnergyLevel();
%object.getEnergyPercent();
%object.setEnergyLevel(
// Health Functions
%object.getRepairRate();
%object.setRepairRate(
%object.getDamageLevel();
%object.getDamagePercent();
%object.setDamageLevel(
%object.applyDamage(
%object.applyRepair(
%object.getDamageState();
%object.setDamageState(