Game Development Community

dev|Pro Game Development Curriculum

SetMoveObject() for AIPlayers

by Eric Hartman · 01/29/2006 (3:31 pm) · 5 comments

This code will allow you set an object for an AIPlayer to follow by calling .setMoveObject(), similar to the way .setAimObject() works. Calling .setMoveObject(%obj) on an AIPlayer will cause it to run to the worldBoxCenter of %obj.

FILE: game/aiPlayer.cc
near the top of the function bool AIPlayer::getAIMove(Move *movePtr) add the bold code
if (mAimObject || mAimLocationSet || mMoveState == ModeMove || mMoveObject) {
	[b]if(mMoveObject)
	{
		mMoveObject->getWorldBox().getCenter(&mMoveDestination);	
		mMoveState = ModeMove;
	}[/b]
...

Add these functions somewhere in the file:
void AIPlayer::setMoveObject( GameBase *targetObject )
{   
   mMoveObject = targetObject;
}

ConsoleMethod( AIPlayer, setMoveObject, void, 3, 3, "( GameBase obj )"
              "Sets the bot's follow object.")
{  
   // Find the target
   GameBase *targetObject;
   if( Sim::findObject( argv[2], targetObject ) )
      object->setMoveObject( targetObject );
   else
      object->setMoveObject( 0 );
}

ConsoleMethod( AIPlayer, getMoveObject, S32, 2, 2, "()"
              "Gets the object the AI is following.")
{
   GameBase* obj = object->getMoveObject();
   return obj? obj->getId(): -1;
}



FILE: game/aiPlayer.h

In the private: section, add:
SimObjectPtr<GameBase> mMoveObject;

In the public: section, add:
void setMoveObject( GameBase *targetObject );
GameBase* getMoveObject() const  { return mMoveObject; }

I did this in torque 1.2... ish.

#1
01/29/2006 (3:32 pm)
Thanks Eric. :)
#2
02/08/2006 (7:34 pm)
This is really helpfull for my zombie game, thanks alot. I'm kind of wondering why no one has done this before, or maybe i just didn't notice it.
#3
02/11/2006 (3:37 am)
I've been doing it for years just in script.
%this.schedule(2000,setMoveDestination,%this.getAimObject().getTransform());
Put that in OnReachDestination

Anyways, this is still a pretty cool way of accomplishing the same thing.
#4
04/04/2006 (12:36 pm)
%this.schedule(2000,setMoveDestination,%this.getAimObject().getTransform());

I've done that too, and it works like poop. This works like 100x better.
#5
04/15/2006 (11:40 pm)
It depends on how you go about doing it. This is much smoother though and is excellent if you don't have a whole lot of objects using it at the same time.