AIPlayers are acting up
by Arthur D. Burson · in Game Design and Creative Issues · 12/06/2010 (10:27 pm) · 1 replies
I am trying to implement more than 1 pathed AI. I wanted to go the easy route and I copied the aiPlayer.cs file and renamed it to aiPlayer2.cs. Inside the new aiPlayer2.cs I altered the references to AIPlayer to AIPlayer2. I executed it in the game.cs right underneath the aiPlayer.cs. AIPlayer2 just stands still and aiPlayer is gone. Any suggestions? My code is below:
//-----------------------------------------------------------------------------
// Demo Pathed AIPlayer2.
//-----------------------------------------------------------------------------
datablock PlayerData(DemoPlayer : PlayerBody)
{
shootingDelay = 2000;
};
function DemoPlayer::onReachDestination(%this,%obj)
{
// Moves to the next node on the path.
// Override for all player. Normally we'd override this for only
// a specific player datablock or class of players.
if (%obj.path !$= "") {
if (%obj.currentNode == %obj.targetNode)
%this.onEndOfPath(%obj,%obj.path);
else
%obj.moveToNextNode();
}
}
function DemoPlayer::onEndOfPath(%this,%obj,%path)
{
%obj.nextTask();
}
function DemoPlayer::onEndSequence(%this,%obj,%slot)
{
echo("Sequence Done!");
%obj.stopThread(%slot);
%obj.nextTask();
}
//-----------------------------------------------------------------------------
// AIPlayer static functions
//-----------------------------------------------------------------------------
function AIPlayer2::spawn(%name,%spawnPoint)
{
// Create the demo player object
%player = new AiPlayer() {
dataBlock = DemoPlayer;
path = "";
};
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint);
return %player;
}
function AIPlayer2::spawnOnPath(%name,%path)
{
// Spawn a player and place him on the first node of the path
if (!isObject(%path))
return 0;
%node = %path.getObject(0);
%player = AIPlayer::spawn(%name,%node.getTransform());
return %player;
}
//-----------------------------------------------------------------------------
// AIPlayer2 methods
//-----------------------------------------------------------------------------
function AIPlayer::followPath(%this,%path,%node)
{
// Start the player following a path
%this.stopThread(0);
if (!isObject(%path)) {
%this.path = "";
return;
}
if (%node > %path.getCount() - 1)
%this.targetNode = %path.getCount() - 1;
else
%this.targetNode = %node;
if (%this.path $= %path)
%this.moveToNode(%this.currentNode);
else {
%this.path = %path;
%this.moveToNode(0);
}
}
function AIPlayer2::moveToNextNode(%this)
{
if (%this.targetNode < 0 || %this.currentNode < %this.targetNode) {
if (%this.currentNode < %this.path.getCount() - 1)
%this.moveToNode(%this.currentNode + 1);
else
%this.moveToNode(0);
}
else
if (%this.currentNode == 0)
%this.moveToNode(%this.path.getCount() - 1);
else
%this.moveToNode(%this.currentNode - 1);
}
function AIPlayer2::moveToNode(%this,%index)
{
// Move to the given path node index
%this.currentNode = %index;
%node = %this.path.getObject(%index);
%this.setMoveDestination(%node.getTransform(), %index == %this.targetNode);
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
function AIPlayer2::pushTask(%this,%method)
{
if (%this.taskIndex $= "") {
%this.taskIndex = 0;
%this.taskCurrent = -1;
}
%this.task[%this.taskIndex] = %method;
%this.taskIndex++;
if (%this.taskCurrent == -1)
%this.executeTask(%this.taskIndex - 1);
}
function AIPlayer2::clearTasks(%this)
{
%this.taskIndex = 0;
%this.taskCurrent = -1;
}
function AIPlayer2::nextTask(%this)
{
if (%this.taskCurrent != -1)
if (%this.taskCurrent < %this.taskIndex - 1)
%this.executeTask(%this.taskCurrent++);
else
%this.taskCurrent = -1;
}
function AIPlayer2::executeTask(%this,%index)
{
%this.taskCurrent = %index;
eval(%this.getId() @ "." @ %this.task[%index] @ ";");
}
//-----------------------------------------------------------------------------
function AIPlayer2::singleShot(%this)
{
// The shooting delay is used to pulse the trigger
%this.setImageTrigger(0,true);
%this.setImageTrigger(0,false);
%this.trigger = %this.schedule(%this.shootingDelay,singleShot);
}
//-----------------------------------------------------------------------------
function AIPlayer2::wait(%this,%time)
{
%this.schedule(%time * 1000,"nextTask");
}
function AIPlayer2::done(%this,%time)
{
%this.schedule(0,"delete");
}
function AIPlayer2::fire(%this,%bool)
{
if (%bool) {
cancel(%this.trigger);
%this.singleShot();
}
else
cancel(%this.trigger);
%this.nextTask();
}
function AIPlayer2::aimAt(%this,%object)
{
echo("Aim: " @ %object);
%this.setAimObject(%object);
%this.nextTask();
}
function AIPlayer2::animate(%this,%seq)
{
//%this.stopThread(0);
//%this.playThread(0,%seq);
%this.setActionThread(%seq);
}
//-----------------------------------------------------------------------------
function AIManager::think(%this)
{
// We could hook into the player's onDestroyed state instead of
// having to "think", but thinking allows us to consider other
// things...
if (!isObject(%this.player))
%this.player = %this.spawn();
%this.schedule(500,think);
}
function AIManager::spawn(%this)
{
%player = AIPlayer2::spawnOnPath("Jimmy","MissionGroup/Path2");
if (isObject(%player))
{
%player.followPath("MissionGroup/Paths/Path2",-1);
%player.mountImage(M16Image,0);
%player.setInventory(M16Ammo,1000);
return %player;
}
else
return 0;
}
. I just fixed the aiPlayer2 problem but aiplayer is still not visible.
How do I make him fire at the player character? Thanks in advance.
//-----------------------------------------------------------------------------
// Demo Pathed AIPlayer2.
//-----------------------------------------------------------------------------
datablock PlayerData(DemoPlayer : PlayerBody)
{
shootingDelay = 2000;
};
function DemoPlayer::onReachDestination(%this,%obj)
{
// Moves to the next node on the path.
// Override for all player. Normally we'd override this for only
// a specific player datablock or class of players.
if (%obj.path !$= "") {
if (%obj.currentNode == %obj.targetNode)
%this.onEndOfPath(%obj,%obj.path);
else
%obj.moveToNextNode();
}
}
function DemoPlayer::onEndOfPath(%this,%obj,%path)
{
%obj.nextTask();
}
function DemoPlayer::onEndSequence(%this,%obj,%slot)
{
echo("Sequence Done!");
%obj.stopThread(%slot);
%obj.nextTask();
}
//-----------------------------------------------------------------------------
// AIPlayer static functions
//-----------------------------------------------------------------------------
function AIPlayer2::spawn(%name,%spawnPoint)
{
// Create the demo player object
%player = new AiPlayer() {
dataBlock = DemoPlayer;
path = "";
};
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint);
return %player;
}
function AIPlayer2::spawnOnPath(%name,%path)
{
// Spawn a player and place him on the first node of the path
if (!isObject(%path))
return 0;
%node = %path.getObject(0);
%player = AIPlayer::spawn(%name,%node.getTransform());
return %player;
}
//-----------------------------------------------------------------------------
// AIPlayer2 methods
//-----------------------------------------------------------------------------
function AIPlayer::followPath(%this,%path,%node)
{
// Start the player following a path
%this.stopThread(0);
if (!isObject(%path)) {
%this.path = "";
return;
}
if (%node > %path.getCount() - 1)
%this.targetNode = %path.getCount() - 1;
else
%this.targetNode = %node;
if (%this.path $= %path)
%this.moveToNode(%this.currentNode);
else {
%this.path = %path;
%this.moveToNode(0);
}
}
function AIPlayer2::moveToNextNode(%this)
{
if (%this.targetNode < 0 || %this.currentNode < %this.targetNode) {
if (%this.currentNode < %this.path.getCount() - 1)
%this.moveToNode(%this.currentNode + 1);
else
%this.moveToNode(0);
}
else
if (%this.currentNode == 0)
%this.moveToNode(%this.path.getCount() - 1);
else
%this.moveToNode(%this.currentNode - 1);
}
function AIPlayer2::moveToNode(%this,%index)
{
// Move to the given path node index
%this.currentNode = %index;
%node = %this.path.getObject(%index);
%this.setMoveDestination(%node.getTransform(), %index == %this.targetNode);
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
function AIPlayer2::pushTask(%this,%method)
{
if (%this.taskIndex $= "") {
%this.taskIndex = 0;
%this.taskCurrent = -1;
}
%this.task[%this.taskIndex] = %method;
%this.taskIndex++;
if (%this.taskCurrent == -1)
%this.executeTask(%this.taskIndex - 1);
}
function AIPlayer2::clearTasks(%this)
{
%this.taskIndex = 0;
%this.taskCurrent = -1;
}
function AIPlayer2::nextTask(%this)
{
if (%this.taskCurrent != -1)
if (%this.taskCurrent < %this.taskIndex - 1)
%this.executeTask(%this.taskCurrent++);
else
%this.taskCurrent = -1;
}
function AIPlayer2::executeTask(%this,%index)
{
%this.taskCurrent = %index;
eval(%this.getId() @ "." @ %this.task[%index] @ ";");
}
//-----------------------------------------------------------------------------
function AIPlayer2::singleShot(%this)
{
// The shooting delay is used to pulse the trigger
%this.setImageTrigger(0,true);
%this.setImageTrigger(0,false);
%this.trigger = %this.schedule(%this.shootingDelay,singleShot);
}
//-----------------------------------------------------------------------------
function AIPlayer2::wait(%this,%time)
{
%this.schedule(%time * 1000,"nextTask");
}
function AIPlayer2::done(%this,%time)
{
%this.schedule(0,"delete");
}
function AIPlayer2::fire(%this,%bool)
{
if (%bool) {
cancel(%this.trigger);
%this.singleShot();
}
else
cancel(%this.trigger);
%this.nextTask();
}
function AIPlayer2::aimAt(%this,%object)
{
echo("Aim: " @ %object);
%this.setAimObject(%object);
%this.nextTask();
}
function AIPlayer2::animate(%this,%seq)
{
//%this.stopThread(0);
//%this.playThread(0,%seq);
%this.setActionThread(%seq);
}
//-----------------------------------------------------------------------------
function AIManager::think(%this)
{
// We could hook into the player's onDestroyed state instead of
// having to "think", but thinking allows us to consider other
// things...
if (!isObject(%this.player))
%this.player = %this.spawn();
%this.schedule(500,think);
}
function AIManager::spawn(%this)
{
%player = AIPlayer2::spawnOnPath("Jimmy","MissionGroup/Path2");
if (isObject(%player))
{
%player.followPath("MissionGroup/Paths/Path2",-1);
%player.mountImage(M16Image,0);
%player.setInventory(M16Ammo,1000);
return %player;
}
else
return 0;
}
. I just fixed the aiPlayer2 problem but aiplayer is still not visible.
How do I make him fire at the player character? Thanks in advance.
Torque Owner Tim Heldna
You only need one aiPlayer.cs file! You simply use the spawn function to spawn as many different aiPlayer's as you want. The %this is what references the AI in question.