How to apply damage to game objects?
by Markus Nuebel · in Torque Game Engine · 04/26/2004 (11:11 pm) · 0 replies
Hi guys.
I have a problem, finding the right way to apply damage to game objects and destroy them, when the damage exceeds a limit. In my case I want to apply damage to vehicle objects.
Out of the box no damage is applied to vehicles, so I checked out some of the sample scipts and tried the following, with the result, that my vehicle get's invisible when the damage get's to high, but is still part of the scene (checked this with "tree();") and still makes sounds and stuff like that.
I simply want a projectile to damage a vehicle and have the vehicle explode when the damage reaches a limit.
Heres what I did:
Scriptcode: When the projectile hits a target it calls %obj.damage()
Scriptcode: I found the follwing script functions in the sample code (RW, I think) which is quite handy, since it delegates the damage() call to an objects datablock instance. This way I can do different damage reactions for different game objects. But here I am just sticking with the default: The datablock function damage() calls applyDamage() on the target object.
C++ code: By now we have called into C++ code: ShapeBase::applyDamage(). This simply adds to the total damage amount and in turn calls ShapeBase::setDamageLevel() which calls ShapeBase::updateDamageLevel(). updateDamageLevel() does nothing for vehicles or items except for calling back into script code: ShapeBase::onDamage()
Scriptcode: In the scriptcode I have used the same trick as seen above, to delegate the Vehicle::onDamage() call to the datablock onDamage() call. (Which works fine)
In the datablocks onDamage() function I am checking the objects damage level and eventually set the objects damagestate to "Destroyed". In case the current damage level has reached maxDamage.
C++ code: Now we are back in C++ code.
ShapeBase::setDamageState("Destroyed") internally calls ShapeBase::setDamageState("Disabled"), wich then calls the script callback:onDisabled() and then the script callback:onDestroyed().
!!At this point my vehicle get's invisible, but is not removed from the MissionCleanup group.
The vehicle still emitts sounds, which is really odd.
ScriptCode: I have a last piece of script code, that is intended to delete the vehicle, once it is destroyed, but this is never called. I guess, since the C++ code first sets the object to disabled, no more callbacks are executed on it ??
Somewhere in this call hierarchy there must be mistake, but I am not able to find it.
Can anybody help here?
Maybe the process is completely wrong?
So what is the correct (or at least standard) way, to apply damge to game items and have them explode at some point?
Thanks a lot.
-- Markus
I have a problem, finding the right way to apply damage to game objects and destroy them, when the damage exceeds a limit. In my case I want to apply damage to vehicle objects.
Out of the box no damage is applied to vehicles, so I checked out some of the sample scipts and tried the following, with the result, that my vehicle get's invisible when the damage get's to high, but is still part of the scene (checked this with "tree();") and still makes sounds and stuff like that.
I simply want a projectile to damage a vehicle and have the vehicle explode when the damage reaches a limit.
Heres what I did:
Scriptcode: When the projectile hits a target it calls %obj.damage()
Projectile::onCollision(...)
{
// Apply damage to collision object
%obj.damage()
}Scriptcode: I found the follwing script functions in the sample code (RW, I think) which is quite handy, since it delegates the damage() call to an objects datablock instance. This way I can do different damage reactions for different game objects. But here I am just sticking with the default: The datablock function damage() calls applyDamage() on the target object.
ShapeBase::damage(...)
{
%this.getDataBlock().damage();
}
ShapeBaseData::damage(...)
{
%target.applydamage();
}C++ code: By now we have called into C++ code: ShapeBase::applyDamage(). This simply adds to the total damage amount and in turn calls ShapeBase::setDamageLevel() which calls ShapeBase::updateDamageLevel(). updateDamageLevel() does nothing for vehicles or items except for calling back into script code: ShapeBase::onDamage()
ShapeBase::applyDamage() --> ShapeBase::setDamageLevel() --> ShapeBase::updateDamageLevel() --> calls script code ShapeBase::onDamage()
Scriptcode: In the scriptcode I have used the same trick as seen above, to delegate the Vehicle::onDamage() call to the datablock onDamage() call. (Which works fine)
In the datablocks onDamage() function I am checking the objects damage level and eventually set the objects damagestate to "Destroyed". In case the current damage level has reached maxDamage.
Vehicle::onDamage(...)
{
%this.getDataBlock().onDamage();
}
VehicleData::onDamage(...)
{
// Destroy object when we reach the limit
if(%obj.getDamageLevel() >= %this.maxDamage)
%obj.setDamageState("Destroyed");
}C++ code: Now we are back in C++ code.
ShapeBase::setDamageState("Destroyed") internally calls ShapeBase::setDamageState("Disabled"), wich then calls the script callback:onDisabled() and then the script callback:onDestroyed().
ShapeBase::setDamageState("Destroyed") --> ShapeBase::setDamageState("Disabled"), ScriptCode: onDisabled(), ScriptCode: onDestroyed(),!!At this point my vehicle get's invisible, but is not removed from the MissionCleanup group.
The vehicle still emitts sounds, which is really odd.
ScriptCode: I have a last piece of script code, that is intended to delete the vehicle, once it is destroyed, but this is never called. I guess, since the C++ code first sets the object to disabled, no more callbacks are executed on it ??
Vehicle::onDestroyed(...)
{
%obj.schedule(1000, "delete");
}Somewhere in this call hierarchy there must be mistake, but I am not able to find it.
Can anybody help here?
Maybe the process is completely wrong?
So what is the correct (or at least standard) way, to apply damge to game items and have them explode at some point?
Thanks a lot.
-- Markus
About the author