Have something controllable by both player and A.I.

What are the things you should keep in mind when you are making something like a tank script for an example - that needs to be controlled by both a player script and an A.I. script?

I did something similar where I had the tank script take in a direction input both from the player and from the A.I. which turned out awful and hard to work with so I want to know if there are any practices you use to make it work well with both player and A.I.

I think the level of how much control you give the AI to make the player controls comfortable will vary from game to game. I suggest trying out several control methods and finding which ones work best. Have non-programmers try it and see what they find fun or frustrating.

In my group of gamers, we unanimously hate AI that can “undo” our actions. For example, if the AI and player can both control a door, it would be really annoying if I wanted the door to always be open and the AI would occasionally close it on me. It would also be annoying if we were both trying to aim the same weapon at 2 different objects. To sum it up, my 2 tips would be: 1) when a player has a repeated behavior, try not to let the AI interfere with that… 2) allow the player to override AI decisions, but don’t allow the AI to override player decisions when it’s a choice the player might disagree with.

But again, I think this need to be tailored to your game, so play around with it.

2 Likes

Are you talking about an AI that is actively controlling the same object as the player? Or are you talking about building out your scripts so you can have both player controlled objects and separate AI controlled objects?

The 1st one doesn’t even make sense to me, so I’m going to assume you meant the 2nd one. I like to build a common set of control scripts for movement, firing weapons, etc, that have nothing to do with either the AI logic or the player input. Then create a player control script and an AI control script that both access the same common scripts. So the AI basically controls the object in the same way the player would.

Then I just set up a procedure for telling the object if it is player or AI controlled, and the appropriate script takes over control of the object. Usually it is something like setting a bool on the AI script so it knows to control the object.

That’s exactly what I’m talking about. I was wondering if there was something you had to re-do because you thought it would work but found out it didn’t - something you can tell me before I do the same thing and have to rewrite the whole code.

Many studios do this. I wrote a bit here about Brett Laming’s article in AI Game Programming Wisdom that describes Rockstar’s MARPO architecture, which includes a virtual controller layer that allows player-controlled and AI-controlled characters to share as much functionality as possible. The article in AIGPW4 is the best resource, but if you can’t get your hands on the book you can find Laming’s AI Architecture and Design Patterns powerpoint here.

1 Like

My biggest mistake the first time was writing the player input controls into the main logic scripts with no separation between getting inputs and doing some action. It makes it very hard to later tie in your AI scripts. I ended up doing what I said in my previous comment, where I separated out all the player inputs from what the inputs tell the object to do.

As a side effect, when doing this it makes it really easy to take inputs from multiple sources to do the same thing, such as having a keyboard key press and a UI button both tell a tank to fire. Both the code that handles your keyboard input and the code that handles the button press just need to call the common Fire() method, the same as your AI script just calls the same Fire() method.