Game Development Community

Adding a new AI character

by Matthew Barney · in Torque X 2D · 10/23/2007 (8:02 pm) · 5 replies

Hello, I'm working on a game where the player character can call upon an AI helper to help them out. I've written my component for the AI character and I've written a controller class that determines what moves the AI character makes. I only have one problem now, how do I get my AI character into the world? Do I really have to use TXB and set up all those boxes and what not or is there a way I can do this in code? Also I'd like to pass a list of data to the AI character on creation, is this possible?

#1
10/23/2007 (9:01 pm)
Matthew, I'm working on an AI system for a team sports game, so I've run into some of these issues too. The answers to your questions really depends upon the behavior of your AI. Here are a few thoughts to your questions...

I only have one problem now, how do I get my AI character into the world?
Getting the AI character into the world can be done completely in code, if you want to. But using Torque X Builder is helpful for positioning the character. You can either place the character scene object into the scene, or turn it into a Template and use a Spawner to create (one or more) AI characters on demand. Or, you can just place a blank scene object as a reference point for where to create the AI.

Do I really have to use TXB and set up all those boxes and what not or is there a way I can do this in code?
Again, everything that you can setup from within Torque X Builder, you can also setup in code. Torque X Builder is simply a handy way to drag & drop objects, quickly see values, and gauge relative positioning within the scene. But, you can always just use Torque X Builder to position objects and then use code to set properties if you want to.

Also I'd like to pass a list of data to the AI character on creation, is this possible?
The strongly typed list collection is a great way to share data. You can pass a list to another component as a method parameter, as a reference parameter, as a property, or even have the list exposed as an interface to other components. Exposing a list an interface is probably the most efficient approach.

John K.
#2
10/23/2007 (9:05 pm)
It's quite simple. (I think...)

This should work.
You'd start by creating a scene object, adding your AI component onto it, and then inserting it into the scene.
T2DSceneObject aiPlayer = new T2DSceneObject();
aiPlayer.Position = new Vector2(wherever, youWant);

AIComponent aiComponent = new AIComponent(yourData); 
aiPlayer.Components.AddComponent(aiComponent);

TorqueObjectDatabase.Instance.Register(aiPlayer);


Or, you could set him up in TXB as as a template, and clone him.
T2DSceneObject aiPlayer = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("aiPlayerTemplate").Clone() as T2DSceneObject;
aiPlayer.Position = new Vector2(wherever, youWant);

AIComponent aiComponent = aiPlayer.Components.FindComponent<AIComponent>();
aiComponent.setData(yourData);
            
TorqueObjectDatabase.Instance.Register(aiPlayer);
#3
10/24/2007 (1:05 am)
Thanks guys, I tried something similar to what Shawn suggested but the game threw an exception when I registered the aiPlayer. I had a bunch of excess stuff though so that might've caused a problem. I'll try and simplify it down to what you've got and give it a whirl. Glad to know I was on something of the right path though.
#4
10/25/2007 (1:39 pm)
Hmm still having a little trouble with this. Are either of you guys working with the Platformer starter kit? I was using a modified version of PlayerController to do my AI control but it derives from ActorController and not TorqueComponent so I can't add it as a component. I tried writing a different controller that derived from TorqueComponent. I'm able to add this, but because it has a different base class I don't have access to all the same methods as before. Is there a way to use the Controller that is like the PlayerController (which would allow me to use a bunch of existing code to handle movements and animation) or will I have to write a different version and thus reimplement all those methods?
#5
10/25/2007 (6:36 pm)
Okay I keep getting the following error:

ActorPuppetComponent must be used on a T2DAnimatedSprite that's mounted to a T2DSceneObject that has an ActorComponent.

I'm using the Platformer framework and I copied the way they have the existing player and just trying to control the spawn with code.

So I have a T2DAnimatedSprite that is set as a template. Then I have a T2DSpawnObject that is mounted to a T2DSceneObject that has my GhostActorComponent attached to it.

I feel like this should work but it seems like there's one step missing...