Player Taking Control of an AI Player
by David Wyand · in Torque Game Engine · 05/17/2002 (11:07 am) · 1 replies
Greetings!
Introduction
I've just begun working on some code to allow a player to take control of an AI player's movement (such as driving around a R/C car). To try out some concepts, I came up with the idea of allowing the player to hop into the body of an AI player upon collision (inspired by the movie Fallen, one that I would recommend). While this code is nowhere near complete, I thought I'd share it with you all and perhaps others can run with it.
Enter the Code
There really isn't very much to it. Open up the fps/server/scripts/player.cs file. Find the Armor::onCollision() function, and add the following to the bottom of it:
There are quite a few echo() statements so you can see what's going on. If you were to remove all of them, there really isn't much to this.
First, we check if there is a collision with a player. Then we check if we collided with an AI player. If so, then essentially swap around which client controls which player.
Try It Out!
OK, start up the FPS game, and add a bot to the map (use one of the resources here if you're not sure how to do that). Now run yourself into the bot. You should now be the bot! I can hear it now: 'BE the bot'.
Issues
Well, this isn't perfect. That's why I posted it here for you to see, rather than as a resource. Hopefully this is enough to get you started, and others can find ways around these issues.
The first one is that you cannot get back into your original body (even if you remove the check for AI players). The second issue I've found is that if you collide with an AI player while in an AI player's body, your camera will freeze. Also, if you send any text messages, you'll notice that it's under your original name, so I'm not swaping everything between the players. And I'm sure that allowing a human to directly control an AI player may have some consequences.
Well, that's all. I'm not sure if I'm going to pursue this any further myself. Maybe it's given someone a direction for a game idea. The idea of being able to hop from body to body does certainly open up some possibilities :o)
Enjoy!
- LightWave Dave
Introduction
I've just begun working on some code to allow a player to take control of an AI player's movement (such as driving around a R/C car). To try out some concepts, I came up with the idea of allowing the player to hop into the body of an AI player upon collision (inspired by the movie Fallen, one that I would recommend). While this code is nowhere near complete, I thought I'd share it with you all and perhaps others can run with it.
Enter the Code
There really isn't very much to it. Open up the fps/server/scripts/player.cs file. Find the Armor::onCollision() function, and add the following to the bottom of it:
// Give the client control of the AI Player
if(%obj.getClassName() $= "Player")
{
echo("Checking...");
echo("%col.client =" SPC %col.client);
echo("Ai Controlled?" SPC %col.client.isAIControlled());
echo("%obj.client =" SPC %obj.client);
echo("Ai Controlled?" SPC %obj.client.isAIControlled());
if(%obj.client.isAIControlled() == 1)
{
echo("Changing client control...");
echo("original %col.client.player =" SPC %col.client.player);
%col.client.camera.setTransform(%obj.getEyeTransform());
%col.client.player = %obj;
echo("%obj =" SPC %obj);
%col.client.setControlObject(%obj);
echo("%col.client.player =" SPC %col.client.player);
echo("original %obj.client.player =" SPC %obj.client.player);
%obj.client.camera.setTransform(%col.getEyeTransform());
%obj.client.player = %col;
echo("%col =" SPC %col);
%obj.client.setControlObject(%col);
echo("%obj.client.player =" SPC %obj.client.player);
}
}There are quite a few echo() statements so you can see what's going on. If you were to remove all of them, there really isn't much to this.
First, we check if there is a collision with a player. Then we check if we collided with an AI player. If so, then essentially swap around which client controls which player.
Try It Out!
OK, start up the FPS game, and add a bot to the map (use one of the resources here if you're not sure how to do that). Now run yourself into the bot. You should now be the bot! I can hear it now: 'BE the bot'.
Issues
Well, this isn't perfect. That's why I posted it here for you to see, rather than as a resource. Hopefully this is enough to get you started, and others can find ways around these issues.
The first one is that you cannot get back into your original body (even if you remove the check for AI players). The second issue I've found is that if you collide with an AI player while in an AI player's body, your camera will freeze. Also, if you send any text messages, you'll notice that it's under your original name, so I'm not swaping everything between the players. And I'm sure that allowing a human to directly control an AI player may have some consequences.
Well, that's all. I'm not sure if I'm going to pursue this any further myself. Maybe it's given someone a direction for a game idea. The idea of being able to hop from body to body does certainly open up some possibilities :o)
Enjoy!
- LightWave Dave
About the author
A long time Associate of the GarageGames' community and author of the Torque 3D Game Development Cookbook. Buy it today from Packt Publishing!
Torque Owner Sabrecyd
Cool idea. I can think of some uses for something like this.