Game Development Community

Spawn a BOT during the game

by University of Pisa (#0004) · in Technical Issues · 05/21/2006 (10:28 am) · 13 replies

Hi, I'm a new user, and I'm italian so I'm sorry for my bad english.
The problem is:
I want create a BOT during the game when I enter in a trigger area, but when I enter the console tell me that bots are created, but I don't see them in the world. I suppose that there's some problem with the rendering process and maybe I have to call some more funtions to create them.
Please help me.

Thanks,

#1
05/21/2006 (12:38 pm)
What code are you using to create them?
#2
05/21/2006 (1:37 pm)
Http://tdn.garagegames.com/wiki/Torque/Script/Tutorials/Using_Basic_AI_Commands

that article in tdn is a very good place to start if you just pop some of that code into the console you can get a bot running in game. then you just have that same code run when you enter the trigger and you're good to go.
#3
05/21/2006 (3:11 pm)
This is my code:
//function onEnterTrigger
function DefaultTrigger::onEnterTrigger(%this,%trigger,%obj)
{
Parent::onEnterTrigger(%this,%trigger,%obj);

%path = "MissionGroup/AIDropPoints";
if(!isObject(%path))
return;
%spawnObj = %path.getObject(1);
%killer = AIManager::spawnAIKiller(%spawnObj);
%killer.startKiller();
}

//function AIManager::spawnAIKiller
function AIManager::spawnAIKiller(%this,%spawnObj)
{
%player = new AIPlayer()
{
dataBlock = AIKiller;
position = %spawnObj.getTransform();

following = true;
playerToFollow = -1;
distanceVision = 1000;
shootingDelay = 2000;

type = %spawnObj.type;
};

MissionCleanup.add(%player);
%player.setMoveSpeed(0.8);

%player.mountImage(MachinegunBOTImage,$WeaponSlot);
%player.setImageAmmo($WeaponSlot,true);
%player.setInventory(MachinegunAmmo,10000);

return %player;
}


If in the trigger function I only write:
%killer = new AIPlayer() {datablock = AIKiller; position = %spawnObject.getTransform();}
is ok, but whit the code above it isn't.
Anyone can help me?

Thanks,
#4
05/21/2006 (5:23 pm)
Have you tried :

%killer = AIManager.spawnAIKiller(%spawnObj);

instead of AIManager::spawn...
#5
05/22/2006 (2:55 am)
Hi Bruno, thank you for your help. I've tried your code and finally it's ok.
But can you explain me why my code it isn't ok? What's wrong?

Thanks,
#6
05/22/2006 (3:20 am)
You were calling the function directly instead of as a method of the object... so there was no implicit defintion for %this. Instead you were passing %spawnObj for %this and the %spawnObj in your function was never being defined.

What Bruno changed is the proper way to call an object method.
#7
05/22/2006 (4:40 am)
I'm sorry, but I don't understand very well, I'm using torque for the first time.
Can you explain me better?

Thanks,
#8
05/22/2006 (6:54 am)
It's an OOP issue :

function f1(%a,%b)
{
   return %a * %b;
}

is an ordinary function that can be called by everyone ( public )

function MyClass::f2(%this,%a,%b)
{
   return %this.value * %a * %b;
}

is a method that belongs to the MyClass class ( "private" ). Only MyClass objects should use it

echo( f1(5,3) ) \ shows 15 

%object = new ScriptObject() { class = "MyClass"; value = 2; }; \ creates an object of class MyClass
echo( %object.f2(5,3) ); \ shows 30 ( 2 * 5 * 3 )

echo( MyClass::f2(5,3) ); \ shows 0

because %this in f2 isn't connected to any object, so %this.value == 0.

HTH
#9
05/22/2006 (7:19 am)
OK, now I understand. But what's the difference between:

%object = new ScriptObject() {class = "MyClass"; value = 2;};

and

%object = new MyClass() {};

Because I've some scripts where there are these differences, like:
// game.cs
new ScriptObject(AIManager) {};
AIManager.think;

// aiPlayer.cs - where a BOT is created:
%player = new AIPlayer() {
datablock = PlayerBody;
....
};

What's the difference?

Thanks,
#10
05/22/2006 (7:37 am)
AIManager is not actually an AI player it is supposed to be an AIBrain type thing. You can store variables into script objects they are like objects just they do not do anything visually. Some other examples of scriptobjects are "missionInfo" which stores the name of the mission and other related stuff. new AIPlayer() creates a new AI player which is defined in the C++ and can have commands such as setMoveDestination() given to it.
#11
05/23/2006 (9:32 am)
That's because TorqueScript doesn't have inheritance. It only simulates it with Namespaces.

%object = new MyClass() {};

You cannot do that. You can only use classes that are aleady defined in the engine like ScriptObject, AIPlayer, Player, etc. The only way to do a new class is hardcoding in C++.

As Master Treb already said :
new ScriptObject(AIManager) {};

Creates a scriptObject with a specified name ("AIManager") you may use this name as a namespace, creating something like :
function AIManager::foo()
{
    echo("foo");
}

But this only works in cases where you have only one object named AIManager. Spawning other will destroy the first.

Using :
%aiPlayer = new AIPlayer() { class = "killer"; }

will allow you to have several bots that may share a function like :
function killer::foo(%this)
{
    MessageAll("Killer Bot " @ %this.getName @ " says foo !!!" );
}
#12
07/11/2006 (6:20 pm)
I am try to write a trigger for spawn bots in the mission. I have never had any luck with triggers. Plaes help.
#13
07/25/2006 (11:17 am)
Michael:

They have already helped. Check out this post fully, comb through the code. It's all there. You just have to use the Mission Editor to manually add the trigger.