how do you guys switch control between player and ai

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By chastifole

im currently making a flexible template class for variable of characters and an ai class that takes a character as a parameter and applies default ai on that
so as an example i store the function that handles player movement on a variable as a funcref and when i pass this character to ai class it reroutes the movement function to its own and player cant control the character anymore

So you’re trying to design your system of movement such that the player can sometimes move the character, and at other times, an AI unit can move the character?

Ertain | 2019-08-27 17:46

:bust_in_silhouette: Reply From: Skipperro

There are couple of ways to do it. Simplest would be to make one boolean like player_controlled and simply check if it’s true or not. If true, then make your whole routine of checking for inputs and other stuff, if false, then skip this and run your AI functions instead.

You could also attach different script to a node, depending on whether it should be player controlled or not. One script would be for the player, one for AI.

Which implementation would be optimal depends on your specific game.

I’ve done something similar, where I switch AI behavior this way - I got one variable, that takes whole script through one of many different paths, to create more passive or aggressive AI, change AI goal between killing player, attacking NPC, protecting important location ect.