Game Development Community

Help: Game of tag

by Anthony Messier · in Torque Game Engine · 03/22/2007 (7:06 am) · 8 replies

My plan is to make a simple game of tag. Player spawns 2 bots spawn, bot1 runs from player, while bot2 chases player.
Thats all I plan to accomplish right now. I will worry about the onCollision tag stuff later. I found some tutorials on how to make bots follow the player, but when I implement both bots follow the player.
How do I implement different AI for each of the bots?
Do I need to make a duplicate player class for bot2?

#1
03/22/2007 (7:38 am)
You probably just need to make 2 different datablocks:

datablock AIPlayerData(FollowerAI)
{
   type = 0;
};

datablock AIPlayerData(RunnerAI)
{
   type = 1;
};

new AIPlayer(FollowerBot)
{
   datablock = FollowerAI;
};

new AIPlayer(RunnerBot)
{
  datablock = RunnerAI;
};

I'm just using arbitrary values related to what you might be doing, these aren't exact datablocks or classes in the engine necessarily. The whole idea is having one class (AIPlayer), two datablocks (RunnerAI and FollowerAI), and two AI Objects (FollowerBot and RunnerBot).
#2
03/22/2007 (8:02 am)
Thanks, I wasn't sure the terminology used in torque yet so searching for things is difficult. Also the search function does not work very well being able to limit your search to different forums would help alot.

But this discribes what you just told me in more detail.
http://tdn.garagegames.com/wiki/TorqueScript/Datablocks
#3
03/23/2007 (3:34 pm)
I got my Bots in game but now I dont know where the main gameloop is? Is there a gameloop needed in torque? Right now I have bots that follow paths something is telling them to repeat.

Do I need to make a Loop? something like this?
While(!victory && !defeat)
{
bot1.stuff();
bot2.stuff();
}
and where would I put it?

This is the function I would like to put it in my loop

//--------------------------------------------------------------------
// checks if the requested object is in a certain range
// given is: bot = %this / human = %object
// return is: 0 = not in sight / 1 = in sight
//--------------------------------------------------------------------
function AIPlayer::isObjectInView(%this, %object)
{
...
return false;
}
#4
03/24/2007 (8:34 am)
Torque does not have a main loop like you would think. The real main loop is in the engine, in engine\game\main.cc. However, unless you have a huge system to update (like Net), you don't want to put your stuff in there because it bypasses a lot of the architecture, particularly the client/server stuff.

However, almost every object has it's own mini-main. Typically this is an update() or processTick(). This is where you want to place the object update information. If you are using Torque's default AIPlayer, you'll want to have a look at engine\game\AIPlayer.cc/.h.

If you want to continue to work in script, you'll want to take advantage of global scheduling or object scheduling:

"pseudo code"
function InitAI()
{
    if(!victory && !defeat)
    {
          bot1.schedule(1000, stuff());
          bot2.schedule(1000, stuff());
    }
}

That's just one concept, but every time you have a new AI you'll have to change InitAI(). Using an engine process, such as processTick() will affect all AI with less script changes for you.
#5
03/24/2007 (9:02 am)
I see, So with the example bots
function AIManager::think( %this )
{
if( !isObject( %this.player ) )
%this.player = %this.spawn();

%this.schedule( 500, think );

// I added these
echo("This");
%this.schedule(1000,isObjectInView(%this, %player));
}
This is the bots loop.


However now I get an ERROR message.
GameOne/server/aiplayer.cs (194): Unable to find function isObjectInView
This
GameOne/server/aiplayer.cs (194): Unable to find function isObjectInView


The funciton is in the same .cs file as where im calling it from.
function AIPlayer::isObjectInView(%this, %object)
{
}

I tried to change its class to AIManager with the same results.
#6
03/24/2007 (10:45 am)
You're close....have a look at your function:
//--------------------------------------------------------------------
// checks if the requested object is in a certain range
// given is: bot = %this / human = %object
// return is: 0 = not in sight / 1 = in sight
//--------------------------------------------------------------------
function AIPlayer::isObjectInView(%this, %object)
{
...
return false;
}

isObjectInView is an object method, not a stand alone function. So you can't call that independent of itself.

Check out the parameter list of schedule, or look it up here in on the site (TDN/Documentation/Forums). You would have to do something like this:

%object = [b]someValue[/b];
%this.schedule(1000, "isObjectInView", %object);
#7
03/24/2007 (12:45 pm)
Why do i need to schedule my function? How do I get the return value when I do?


I found this post which explains the schedule() function well.
http://www.garagegames.com/mg/forums/result.thread.php?qt=22272

Is there an offical place to look up functions?
#8
03/25/2007 (11:20 am)
I picked up torsion so I could debug scripts it helps a lot.
I have this working correctly now.

%bot=%this.player;
   %found=AIPlayer::isObjectInView(%bot,$player);