Spawning a new AI player from the engine
by Ingo Seidel · in Torque Game Engine · 12/15/2006 (3:42 am) · 2 replies
Hello!
I think the usual way to add AI players to a mission is done through script code but I want to add them directly from the engine code. I tried to copy the code from the script files and adapt it for the engine. This is what the script code for adding an AI player looks like
I wrote the following code for the engine:
But when running this code I get animation sequence errors like this
Warning: (i:\.....engine\game\player.cc @ 290) PlayerData::preload - Unable to find named animation sequence 'root'!
My problem now is that I don't know how to attach the appropriate animation sequences to the PlayerShape. I know that this happens in the script in the code
which contains
But I can't 'transform' this script code into engine code. The TSShapeConstructor class in the engine contains all private members and those members are only exposed to the script engine. I guess that I won't have to use the TSShapeConstructor in my engine code, but I just couldn't figure out where I have to set the paths for the animation sequences - maybe in the TSShape class? Does anyone how to do this or could it also be that the animation sequence problem is minor and that the error lies somewhere else (with the above engine code no new avatar gets visible in the mission - is this caused by the failed animation sequence loading?)?
thanks
I think the usual way to add AI players to a mission is done through script code but I want to add them directly from the engine code. I tried to copy the code from the script files and adapt it for the engine. This is what the script code for adding an AI player looks like
Exec("~/data/models/avatars/agent/agent.cs");
datablock PlayerData(AgentAvatar)
{
renderFirstPerson = false;
emap = true;
className = AgentAvatar;
shapeFile = "~/data/models/avatars/agent/agent.dts";
};
function AIPlayer::spawnBot(%index,%location)
{
%me = new AIPlayer() {
dataBlock = AgentAvatar;
};
%me.setTransform(%location);
%me.name="Agent " @ %index;
}I wrote the following code for the engine:
function spawnAvatar() {
AIPlayer *avatar = new AIPlayer();
avatar->registerObject();
PlayerData *data = new PlayerData();
data->shapeName = "./3dei/data/models/avatars/agent/agent.dts";
data->renderFirstPerson = false;
data->emap = true;
char errBuff[256];
data->preload(true,errBuff);
Con::errorf(errBuff);
avatar->setDataBlock(data);
// The code below is copied from ConsoleMethod( SceneObject, setTransform, ...) and calculates the
// appropriate transformation matrix for a particular 3D point
Point3F pos(-457.674,253.89,352.402);
const MatrixF& tmat = avatar->getTransform();
tmat.getColumn(3,&pos);
AngAxisF aa(tmat);
MatrixF mat;
aa.setMatrix(&mat);
mat.setColumn(3,pos);
avatar->setTransform(mat);
}But when running this code I get animation sequence errors like this
Warning: (i:\.....engine\game\player.cc @ 290) PlayerData::preload - Unable to find named animation sequence 'root'!
My problem now is that I don't know how to attach the appropriate animation sequences to the PlayerShape. I know that this happens in the script in the code
Exec("~/data/models/avatars/agent/agent.cs");which contains
datablock TSShapeConstructor(AgentDts)
{
baseShape = "./agent.dts";
sequence0 = "./agent_root.dsq root";
// ... and some more sequences
};But I can't 'transform' this script code into engine code. The TSShapeConstructor class in the engine contains all private members and those members are only exposed to the script engine. I guess that I won't have to use the TSShapeConstructor in my engine code, but I just couldn't figure out where I have to set the paths for the animation sequences - maybe in the TSShape class? Does anyone how to do this or could it also be that the animation sequence problem is minor and that the error lies somewhere else (with the above engine code no new avatar gets visible in the mission - is this caused by the failed animation sequence loading?)?
thanks
#2
Unfortuanetly my player does not get displayed, the code works fine (without errors) but there gets no player spawned in my world. In contrast to the class Item, in the class AIPlayer you can't access the setPosition method. So I am doing it the way as described above (with setTransform). Maybe this is the problem why my player does not get displayed or it still lies somewhere else.
But now it's weekend, will continue to worry on monday :)
12/15/2006 (8:15 am)
Thanks for the link, it is kind of strange to define the player data in script code and then access it from the engine through Sim::findObject. So there is a very tight coupling between engine and script code but obviously this is how it is done in Torque.Unfortuanetly my player does not get displayed, the code works fine (without errors) but there gets no player spawned in my world. In contrast to the class Item, in the class AIPlayer you can't access the setPosition method. So I am doing it the way as described above (with setTransform). Maybe this is the problem why my player does not get displayed or it still lies somewhere else.
But now it's weekend, will continue to worry on monday :)
Associate Stefan Beffy Moises