Game Development Community

dev|Pro Game Development Curriculum

Simple bot spawning improvement (script-only)

by Dan Keller · 05/15/2007 (8:54 am) · 4 comments

This resource lets you add any number of "AIDropPoints" to a mission that each spawn a bot when the mission loads. The bots can be spawned with a specific weapon and ammo, and the system is easily expandable (examples included).

In aiplayer.cs:

In the spawn function, add
%player.think();
after SetTransform();

After SpawnOnPath, add
function AIPlayer::think(%this)
{
	//do "thinking" here
	%this.schedule(500,think);
}

Delete the two AIManager functions at the bottom and replace them with:
function AIManager::think(%this)
{
   %groupName = "MissionGroup/AIDropPoints";
   %group = nameToID(%groupName);

   if (%group != -1) {
      %count = %group.getCount();
      for (%point = 0; %point < %count; %point++) {
         %this.spawn(%group.getObject(%point));
      }
   }
   else
      error("Missing spawn points group " @ %groupName);
}

function AIManager::spawn(%this, %spawnPoint)
{
   %player = AIPlayer::spawn("", %spawnPoint.getTransform());
   
   if (isObject(%player))
   {
      %player.followPath(%spawnPoint.path,-1);

      %player.mountImage(%spawnPoint.weapon.image,0);
      %player.setInventory(%spawnPoint.weapon,1);
      %player.setInventory(%spawnPoint.weapon.image.ammo,100000)
      return %player;
   }
   else
      return 0;
}

in stronghold.mis, before the last };, add
new SimGroup(AIDropPoints) {
      canSaveDynamicFields = "1";

      new SpawnSphere() {
         canSaveDynamicFields = "1";
         position = "462.592 294.402 224.525";
         rotation = "1 0 0 0";
         scale = "1 1 1";
         dataBlock = "SpawnSphereMarker";
         Radius = "1";
         sphereWeight = "100";
         indoorWeight = "100";
         outdoorWeight = "100";
			//change these to whatever you want to use
			path = "MissionGroup/Paths/Path1";
			weapon = "Crossbow";
      };
   };

Examples of other features

Setting the name:

Add
name = "Kork";
to the spawn point, and change

%player = AIPlayer::spawn("", %spawnPoint.getTransform());

to

%player = AIPlayer::spawn(%spawnPoint.name, %spawnPoint.getTransform());

Setting the datablock: (so the spawnpoint decides what to spawn)

Add
dataBlock="DemoPlayer";
to the spawnpoint.

Change the first few lines of the spawn function from
function AIPlayer::spawn(%name,%spawnPoint)
{
   // Create the demo player object
   %player = new AiPlayer() {
      dataBlock = DemoPlayer;
      path = "";
   };

to

function AIPlayer::spawn(%name,%spawnPoint, %datablock)
{
   // Create the demo player object
   %player = new AiPlayer() {
      dataBlock = [b]%datablock[/b];
      path = "";
   };

and change

%player = AIPlayer::spawn("", %spawnPoint.getTransform());

to

%player = AIPlayer::spawn("", %spawnPoint.getTransform(), %spawnPoint.dataBlock);

Other changes work basically the same way.

#1
06/19/2007 (11:55 am)
I'm impressed. So simple, yet it really helped me figure out what I was doing wrong with my own spawnpoints. Have a 5.
#2
09/09/2007 (9:21 pm)
very impressed, great script, would love to see someone create a resource or a tutorial on how to add a menu bar function under world editor for AI creation, spawning etc
#3
10/06/2007 (8:16 am)
Wowza, I did EXACTLY this with my own AI system. Too bad I didn't find this resource first.
#4
08/16/2009 (5:31 pm)
noob question...where is the aiplayer.cs located?