AI/NPC Death function
by Kyle Cook · in Torque Game Engine · 06/14/2006 (9:36 am) · 4 replies
Where does the server, or client for that matter, handle what occurs during the death of a NPC. It is obviously somewhere because the NPC/AI plays a death animation and respawns. I wanted to add some code, say a death message for the AI/NPC and perhaps award the player something.. I have managed to locate where the Player death is called, but there is nothing here for NPCs.
snipit taken from game.cs
So how would one go about editing what happens on an NPC/AI death?
snipit taken from game.cs
function GameConnection::onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
{
//%dead = %this.player.getShapeName();
//%kill = %sourceClient.player.getShapeName();
// Clear out the name on the corpse
%this.player.setShapeName(%dead@"'s corpse");
// Switch the client over to the death cam and unhook the player object.
if (isObject(%this.camera) && isObject(%this.player)) {
%this.camera.setMode("Corpse",%this.player);
%this.setControlObject(%this.camera);
}
%this.player = 0;
// Doll out points and display an appropriate message
if (%damageType $= "Suicide" || %sourceClient == %this) {
%this.incScore(-1);
messageAll('MsgClientKilled','%1 takes his own life!',%this.name);
}
else {
%sourceClient.incScore(1);
AddDeath(%sourceClient.name);
AddKill(%this.name);
messageAll('MsgClientKilled','%2 has killed %1!',%this.name,%sourceClient.name);
if (%sourceClient.score >= $Game::EndGameScore)
cycleGame();
}
}So how would one go about editing what happens on an NPC/AI death?
#2
For some reason this isn't looking like the function that is called when an AI player is killed..
If it is, how would I check to see if the %obj is a human or AI controlled?
06/14/2006 (12:41 pm)
How would I make it to where instead of %client.onDeath it was something like %aiplayer?function Armor::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
{
if (%obj.getState() $= "Dead")
return;
%obj.applyDamage(%damage);
%location = "Body";
// Deal with client callbacks here because we don't have this
// information in the onDamage or onDisable methods
%client = %obj.client;
%sourceClient = %sourceObject ? %sourceObject.client : 0;
if (%obj.getState() $= "Dead")
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
}For some reason this isn't looking like the function that is called when an AI player is killed..
If it is, how would I check to see if the %obj is a human or AI controlled?
#3
edit: I just checked, and I guess this would help:
In server/scripts/aiPlayer.cs add this line
06/14/2006 (1:09 pm)
The 'client' of your AIPlayer is most likely the AIManager, using stock(I think) Torque scripts. (I could be wrong but this is how I've been using it and it seems to work)function AIManager::onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
{
// Doll out points and display an appropriate message
if (%sourceClient == %this) {
//if (%damageType $= "Suicide") %this.incScore(-1);
messageAll('MsgClientKilled','%1 dies of natural causes!',%this.name);
}
else {
%sourceClient.incKills(1);
messageAll('MsgClientKilled','%1 is killed by %2!',%this.name,%sourceClient.name);
}
}edit: I just checked, and I guess this would help:
In server/scripts/aiPlayer.cs add this line
function AIManager::spawn(%this)
{
%player = AIPlayer::spawnOnPath("Kork","MissionGroup/Paths/Path1");
%player.followPath("MissionGroup/Paths/Path1",-1);
[b]%player.client = %this;[/b]
%player.mountImage(CrossbowImage,0);
%player.setInventory(CrossbowAmmo,1000);
return %player;
}
#4
This code was added in the aiplayer.cs file..
Now in your server/game.cs I have added this.
I looked in the player.cs file and on line 574 there is nothing about onDeath, that is about 100 lines further down in the file. I am beginning to think I might have to mod this chunk of code in player.cs to somehow check to see if the player.isBot is true and then call a sepperate function than when a client dies.
06/15/2006 (9:00 am)
@Midhir : Your code has helped a tremendous amount and I think has steered this mission in the right direction. Except the server is coughing up some wierd error when an AIPlayer dies.Quote:
Felswourne/server/scripts/player.cs (574): Unable to find object: '' attempting
to call function 'onDeath'
This code was added in the aiplayer.cs file..
function AIManager::spawn(%this)
{
%player = AIPlayer::spawnOnPath("Kelna","MissionGroup/Paths/Path1");
%player.followPath("MissionGroup/Paths/Path1",-1);
[b]%player.isBot=true;[/b]
%player.mountImage(CrossbowImage,0);
%player.setInventory(CrossbowAmmo,1000);
return %player;
}Now in your server/game.cs I have added this.
function GameConnection::onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
{
...
[b] if(%this.player.isBot) commandToClient(%sourceClient,'You have been awarded 1 Karma Point',3,2);[/b]
...
}I looked in the player.cs file and on line 574 there is nothing about onDeath, that is about 100 lines further down in the file. I am beginning to think I might have to mod this chunk of code in player.cs to somehow check to see if the player.isBot is true and then call a sepperate function than when a client dies.
function Armor::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
{
if (%obj.getState() $= "Dead")
return;
%obj.applyDamage(%damage);
%location = "Body";
// Deal with client callbacks here because we don't have this
// information in the onDamage or onDisable methods
%client = %obj.client;
%sourceClient = %sourceObject ? %sourceObject.client : 0;
if (%obj.getState() $= "Dead")
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
}
Torque Owner Paul /*Wedge*/ DElia
if (%obj.getState() $= "Dead") %client.onDeath(%sourceObject, %sourceClient, %weapon, %hitLocation);And that happens for any player class. You'll just need to write your own alternate function to be called for AIPlayer classes when they die that handles what you want to happen for them.