Game Development Community

how to have enemy buildings spawn units

by Jeff Yaskus · in RTS Starter Kit · 03/11/2010 (1:18 pm) · 3 replies

I'm trying to "fake" the AI building units ... when I spawn the AI, I create a barracks

after creating the barracks (works ok) ... I then setup a schedule for it to call the following function.

Problem is -- the function repeats a zillion times and then crashes the game.

i.e. it seems to be ignoring the WAIT time on the schedule command.

I've tested all the variables and such using STOP points in TORSION ... but can't figure out why the schedule isn't waiting.

// -------------------------------------
function RTSUnit::AiTrain(%this,%index,%conn)
{
   echo("c2 AiTrain -- enter");
   %state = %this.getState();
   
   // cancel any pending spawns if building is destroyed
   if ( (%state $= "Dead") || (! isObject(%this)) )
      {
         echo("AI building destroyed - canceling its build cycle");
         cancel(%this.spawnAI);
         return;
      }   
   
   // location to build unit
   %location = %this.getId().getTransform();
   %startX = getWord(%location, 0);              
   %startY = getWord(%location, 1);   
   %location = %startX SPC %startY SPC "250" SPC "0 0" SPC %conn.getTeam();
   
   // build the unit
   %conn.createPlayer(%location, %index);   
   MapHud.createPingEvent(%startX SPC %startY, "1 0 0"); // red ... DEBUG (JY)
   
   // determine the next unit to spawn
   %newUnit = getRandom(4,6);
   
   // and time to build it
   switch(%newUnit)
   {
      case 4:      
        %builtTime = 4000; // 4s
      case 5:
        %builtTime = 8000; // 8s
      case 6:
        %builtTime = 9000; // 9s
   }
   
   // create next spawn            
   %this.spawnAI = %this.schedule(%buildTime,"AiTrain",%newUnit,%conn); 
   echo("c2 AiTrain -- exit");
}

Is this some odd client / server (ghost?) related issue here ? Got me stumped.

And oddly enough, why would buildings be in the "Move" state constantly ??
(noticed this when debugging the above)

Why does the schedule not wait 4-8 seconds ... is there a better approach ?

#1
03/12/2010 (1:28 pm)
Jeff, is a typo:

%builtTime and %buildTime
#2
03/12/2010 (4:41 pm)
Thanks Novack -- I just realized that last night. Well at least its an easy fix.
#3
03/14/2010 (2:35 pm)
Got this function working well now -- so long as the enemy building is "alive" ... it will spit out units every 8/16/18 seconds ... and the units start "searching" for targets as well.

When the building gets destroyed, the timer stops getting re-scheduled.

still lots of work to got to have a functional enemy AI though.