Using the Command System
In the following you will find an explanation on how the command system works, enabling you to optimize your workflow and understanding of the Advanced Companion AI.
Default Commands
Using the command system, you give the player a great way to interact with the AI. Additional to the custom commands that you are able to add using the Custom Commands feature, the command system comes with a bunch of predefined commands that are already integrated and ready to use:
Add Custom Commands
You can also dynamically add custom commands without doing any changes to the original code. Check out the “custom commands” documentation for more details on how to implement your own custom commands.
Whats the difference between selecting the AI and selecting in command mode?
When using the command system, there will be some definitions that may be confusing to you as someone who did not develop the system. Two definitions we want to clarify are: Selected and Selected in Command Mode.
Entering the Command Mode
Before the AI can receive and perform any commands, the player has to enter the command mode. The logic for this is mostly provided by the Player Component.
Inside of the player component, a ray is permanently fired forward from the players camera. As soon as the ray detects a companion, the companion is notified that it is selected by the player which does affect the AI behavior. Also, the HUD Widget is informed to edit its elements accordingly. As soon as the companion is selected, the companions AI controller is enabled to receive input from the player.
Now when the player presses “E” and the AI isn’t already in command mode, the AI will be informed that it is under command, the widget changes its elements, and the “Command Detection” is started in the player component.
Get Command Information
When the AI is in command mode, the player component executes the command detection logic. This is another ray sent forward from the player camera that checks what command (if available) should be executed for the currently hit actor.
When hitting an actor, the “Determine Command Type” function is called. This function first checks if the hit actor has the custom command component which would mean that it is containing a custom command. If so, it gets the command type from the component and returns it. If the actor does not own a custom command component, the system will check if any other of the predefined commands is suitable for this actor. For example, if the actor has the “enemy” tag it will be considered as an enemy and the “Attack” command type will be returned.
When the function is finished, a structure will be filled with values, needed by the companion AI to execute the command. The structure does for example contain the command type and the location of the hit actor. If you want to add custom commands, it might be that you have to add custom data to this structure if needed by your command.
Apply a Command
Now that the system has all the information about the currently available command, the player is able to actually tell the AI to perform the command. Until now, all we did is collecting information about what actor is currently selected, what command could be used on this actor and gathering information needed for the AI to perform its command.
If the player now presses “E” again, the system will see that the AI is already commanded so it won’t set the AI in command mode again, but instead it would make the AI actually perform a command. To achieve this the AI is told that it has an active command. Afterwards it is given the needed information and it is told that it is not actively commanded by the player anymore (having an active command and being commanded is a difference).
The AI now knows that it has an active command and has all the necessary information to perform this command.
Stopping a Command or leaving Command Mode
There is currently no way to just stop an already given command. The only way to “stop” a command is to replace it with a new one. That’s just how the command system works. For example, if you told the AI to attack an enemy, you could cancel it by giving the AI a “move” command, back to your position.
If you want to leave the command mode while trying to give the AI a command, this is fairly simple. When in command mode, the AI controller is able to receive input from the player. When pressing the “TAB” Key, you leave the command mode and can return to normal gameplay. Please note that leaving the command mode does not mean that you are cancelling the currently active command.
Last updated