| Home Page | Recent Changes | Preferences

Spawning Your Own Bot

Spawning and Controlling a Bot

This question I have noticed gets asked a lot on the Unreal Forums. In fact I was one of those who asked the question, and after numerous tries and asking around. I eventually found the answer. So I decided that a Unreal Wiki Page should be written about it. You can look at forum discussion I had about it where I final got it to work at Thread logo 123677.

Now on to some work. The first thing you need to understand is how to make a gametype.

See Link: INT File.

Okay now we will be creating 3 classes. The first on is going to be a new xDeathMatch subclass. The only really thing we are going to do in this class is to rewrite the SpawnBot function. Just cut and copied the spawn function from the DeathMatch class and changed the thinks in bold. So our class should look like:

class NewDeathMatch extends xDeathMatch; 

function Bot SpawnBot(optional string botName)
{
local Bot NewBot;
local RosterEntry Chosen;
local UnrealTeamInfo BotTeam;

    BotTeam = GetBotTeam();
    Chosen = BotTeam.ChooseBotClass(botName);

    //set the pawn class name and pawnclass to our own pawn class
    Chosen.PawnClassName = "MutSpawningBot.NewPawn";
    Chosen.PawnClass = class'MutSpawningBot.NewPawn';
    
    //comment out these because the because we just set the pawnclass
    /*  if (Chosen.PawnClass == None)
            Chosen.Init(); //amb
        // log("Chose pawn class "$Chosen.PawnClass);
    */
    
    NewBot = Bot(Spawn(Chosen.PawnClass.default.ControllerClass));
    
    if ( NewBot != None )
        InitializeBot(NewBot,BotTeam,Chosen);
    return NewBot;
}


defaultproperties
{
     MapPrefix="BS"
     BeaconName="BS"
     GameName="NEW DeathMatch"
}

Note the function Spawnbot was provided with assistance by smattbac. Now we will make our new Pawn class that will be a subclass of xPawn class.

class NewPawn extends xPawn;

//used to just show that the class has been initalized
event PostBeginPlay()
{
    Super.PostBeginPlay();
    Log("Pawns begin Play");
}
//assign our controller class through the default properties
defaultproperties
{
    ControllerClass=class'MutSpawningBot.NewController'
}

And our last class will be the Controller class where we will make a subclass of the xBot class.

class NewController extends xBot;

//copied from bot class
function SetAttractionState()
{
    if ( Enemy != None )
        GotoState('FallBack');
    else
    {   //want to change this to whatever your default state is you     
           //want for your bot.
        GotoState('Roaming');

    }//close if
}

As far as I can tell the only way to get the controller to start in whatever your default state is, you must override the Function function SetAttractionState() and change the GotoState(‘Roaming’) to whatever your state is. I have tried using auto state but it doesn’t seem to work.

Now when you load up UT2k3 change the Bot count to 1. Type in the following console command: showdebug and if your controller says: NewController and your Pawn says: NewPawn then there you have it the bot is now under your control.:)

Here is another way of doing the same thing that was sent to me:

This one provided by NickR

class YourGame extends TeamGame; 

function Bot SpawnBot(optional string botName)
{
    local Bot NewBot;
    local RosterEntry Chosen;
    local UnrealTeamInfo BotTeam;

    BotTeam = GetBotTeam();
    Chosen = BotTeam.ChooseBotClass(botName);

    if (Chosen.PawnClass == None)
        Chosen.Init();

    NewBot = Spawn(class'YourPackage.YourAIController');

    if (NewBot != None)
        InitializeBot(NewBot, BotTeam, Chosen);

    return NewBot;
}

defaultproperties
{
    PlayerControllerClassName="YourPackage.YourPlayerController"
    DefaultPlayerClassName="YourPackage.YourPawn"
}
////////////////////////////

class YourAIController extends Controller;

function SetPawnClass(string inClass, string inCharacter)
{
    Super.SetPawnClass("YourPackage.YourPawn", inCharacter);
}

defaultproperties
{
    PawnClass=class'YourPackage.YourPawn'
}

////////////////////////////

class YourPlayerController extends Controller;

function SetPawnClass(string inClass, string inCharacter)
{
    Super.SetPawnClass("YourPackage.YourPawn", inCharacter);
}

defaultproperties
{
    PawnClass=class'YourPackage.YourPawn'
}

Category Tutorial

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

Mapping Topics

Mapping Lessons

UnrealEd Interface

Questions&Answers

Scripting Topics

Scripting Lessons

Making Mods

Class Tree

Questions&Answers

Modeling Topics

Questions&Answers

Log In