AIplayer
by Adam · in Torque Game Engine · 09/21/2006 (10:11 am) · 9 replies
I'm trying to learn how the aiplayer.cs file works. I thought this would work to create two enemies:
Maybe I'm misunderstanding what the AI manager is for because I really thought it would be able to handle multiple enemies with only one instance of the manager. For some reason when I run this I get one enemy that just stands and does nothing. I end up with this error in the script.
ResourceObject::construct: NULL resource create function for 'unnamed'.
Could someone point me in the right direction, maybe I should be creating a new instance of the AIManager, but I really don't think so. I also tried creating an AI object in aiplayer.cs that was linked into the think function.
Thanks in advance for any help
Adam
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("Kork");
if (!isObject(%this.player2))
%this.player2 = %this.spawn("Mr.T");
%this.schedule(500,think);
}
function AIManager::spawn(%this, %name)
{
%player = AIPlayer::spawnOnPath(%name,"MissionGroup/Paths/Path1");
%player.followPath("MissionGroup/Paths/Path1",-1);
%player.mountImage(CrossbowImage,0);
%player.setInventory(CrossbowAmmo,1000);
return %player;
}Maybe I'm misunderstanding what the AI manager is for because I really thought it would be able to handle multiple enemies with only one instance of the manager. For some reason when I run this I get one enemy that just stands and does nothing. I end up with this error in the script.
ResourceObject::construct: NULL resource create function for 'unnamed'.
Could someone point me in the right direction, maybe I should be creating a new instance of the AIManager, but I really don't think so. I also tried creating an AI object in aiplayer.cs that was linked into the think function.
Thanks in advance for any help
Adam
About the author
#2
Thanks again for replying
Adam
09/21/2006 (6:36 pm)
I don't have Torsion installed, I probably should and see what I get. That error is logged in console.txt. So to you the code looks logical then?Thanks again for replying
Adam
#3
player[0]
player[1]
player[2]
But for some reason the values are null when I need them later, I can only get it to work with one enemy.
Here is what I have so far:
I thought I could add multiple enemies in this function and then loop through them, but I get a bunch of can't find object "" attempting to call ... warnings.
09/23/2006 (7:25 pm)
Now I've got one guy shooting back at me when I get so close, I would like to be able to add multiple enemies though. If anyone has any suggestions I would greatly appreciate it. I originally tried to add multiple enemies in an array like this.player[0]
player[1]
player[2]
But for some reason the values are null when I need them later, I can only get it to work with one enemy.
Here is what I have so far:
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("Kork");
%this.player.setImageTrigger(0,0);
echo("spawning new enemy " SPC %this.player);
}
/* if (!isObject(%this.player[1]))
{
%this.player[1] = %this.spawn("Adam");
%this.player[1].setImageTrigger(0,0);
}*/
if(%this.player.getDamageLevel()==%this.player.maxDamage)
{
%count = ClientGroup.getCount();
%botPos = %this.player.getPosition();
%found = false;
%client = ClientGroup.getObject(0);
for(%i = 0; %i < %count; %i++)
{
%client = ClientGroup.getObject(%i);
echo(%client);
if (%client.player $= "" || %client.player == 0 )
{
//Do Nothing
%found = false;
}
else
{
%playPos = %client.player.getPosition();
echo(%playPos);
%tempDist = VectorDist(%playPos, %botPos);
%found = true;
if(%tempDist > 35)
%found = false;
}
}
if(%found)
{
echo(%tempDist);
%this.player.setAimObject(%client.player, "0 0 1");
%this.player.setImageTrigger(0,1);
}
else
{
//Player is not found make sure your not firing.
%this.player.setImageTrigger(0,0);
}
}
%this.schedule(500,think);
}I thought I could add multiple enemies in this function and then loop through them, but I get a bunch of can't find object "" attempting to call ... warnings.
#4
Really, trying to write code (script or otherwise) without being able to see how the code actually behaves, and what the values of your variables are, is kind-of like trying to whistle a song you've never heard, while underneath a jet plane taking off.
09/23/2006 (10:56 pm)
So... what does the debugger (like Torsion) say when you get those warnings?Really, trying to write code (script or otherwise) without being able to see how the code actually behaves, and what the values of your variables are, is kind-of like trying to whistle a song you've never heard, while underneath a jet plane taking off.
#5
09/24/2006 (11:19 am)
I have Torsion installed now, I just have to figure out how to load starter.fps into it.
#6
09/24/2006 (12:25 pm)
I want to thank you for pushing me to use Torsion because it is awesome, I was using Codeweaver for a while but I never could get debug to work. I can't really get the debug working correctly in Torsion, because I don't know why when I hit F5 or Start it tries to connect to an IP address. Do you know what this is about?
#7
So, if you point Torsion at the right Torque executable (typcially torqueDemo.exe), and set the right project directory (typically "example"), then hitting F5 should compile your scripts, then start Torque, then connect to Torque, and you're in business. You can set breakpoints, step the code, look at variables, etc.
09/24/2006 (2:32 pm)
The IP address is the local machine. The debugger works by making a connection to the instance of Torque using networking. Torsion will edit your "main.cs" to insert a call to start up the debugger interface. That's how you can debug the scripts.So, if you point Torsion at the right Torque executable (typcially torqueDemo.exe), and set the right project directory (typically "example"), then hitting F5 should compile your scripts, then start Torque, then connect to Torque, and you're in business. You can set breakpoints, step the code, look at variables, etc.
#8
Actually, you've got two AI players locked together in the same spot. You can't spawn multiple player objects in exactly the same spot or they get locked together. Try spawning different AIs on different nodes of the path.
09/25/2006 (8:12 am)
Quote:Maybe I'm misunderstanding what the AI manager is for because I really thought it would be able to handle multiple enemies with only one instance of the manager. For some reason when I run this I get one enemy that just stands and does nothing. I end up with this error in the script.
Actually, you've got two AI players locked together in the same spot. You can't spawn multiple player objects in exactly the same spot or they get locked together. Try spawning different AIs on different nodes of the path.
#9
Thank you that is exactly what was happening, I added code to make the enemies spawn on spawnspheres, but first check to make sure there is at least 10 units between the spawnsphere and the nearest enemy. Once I got this working it was easy to increase the number of AI players in the world. I now have 6 spawning and fighting eachother and the player. I also added a strafing feature that makes it look they are dodging eachother.
Back to Torsion
When I type %this.player.
I get a popup dialog with a getLeftVector and getRightVector option. I searched the forums and played around with the code and couldn't get them to work so I made my strafe function like this.
Have any of you ever seen getLeftVector and getRightVector before or is it a bug in Torsion?
Thanks again for all the help.
Adam
09/25/2006 (9:44 am)
MarkThank you that is exactly what was happening, I added code to make the enemies spawn on spawnspheres, but first check to make sure there is at least 10 units between the spawnsphere and the nearest enemy. Once I got this working it was easy to increase the number of AI players in the world. I now have 6 spawning and fighting eachother and the player. I also added a strafing feature that makes it look they are dodging eachother.
Back to Torsion
When I type %this.player.
I get a popup dialog with a getLeftVector and getRightVector option. I searched the forums and played around with the code and couldn't get them to work so I made my strafe function like this.
function AIPlayer::strafe(%this)
{
echo("Calling AIPlayer::strafe");
if(%this $= "")
return;
%rand = getRandom(0, 100);
%vector = %this.getPosition();
%lx = getWord(%vector, 0);
%ly = getWord(%vector, 1);
%lz = getWord(%vector, 2);
if(%rand < 50)
{
//Move left
%ly = %ly * -1;
%newVector = %ly SPC %lx SPC %lz;
%newVector = VectorNormalize(%newVector);
%tempScale = %rand * 0.10;
%newVector = VectorScale(%newVector, %tempScale);
%this.setMoveDestination(VectorAdd(%this.getPosition(), %newVector));
}
else
{
//Move right
%lx = %lx * -1;
%newVector = %ly SPC %lx SPC %lz;
%newVector = VectorNormalize(%newVector);
%tempScale = %rand * 0.05;
%newVector = VectorScale(%newVector, %tempScale);
%this.setMoveDestination(VectorAdd(%this.getPosition(), %newVector));
}
}Have any of you ever seen getLeftVector and getRightVector before or is it a bug in Torsion?
Thanks again for all the help.
Adam
Torque Owner J "hplus" W
If you step through the code using a debugger (like Torsion), where is that error message generated?