ConvaiActionsHandler.cs
Only go through this if you want to customize the behavior of the NPC. Only proceed if you are familiar with gRPC and how to use it in Unity.
To understand the script in even more depth, please check out the comments in the script.
Class Overview
The ConvaiActionsHandler
class is a component in Unity used to handle and execute a list of custom actions for a Convai NPC. It allows parsing and processing of user-defined actions, including animations and interactions.
Requirements
This class assumes the existence of a
ConvaiInteractablesData
object in the scene to provide global action settings.The GameObject containing this script should have a
ConvaiNPC
component attached to it for NPC-related actions.
Properties
actionMethods
: An array ofActionMethod
objects, defining custom actions with names, animations, and choices.ActionConfig
: An instance of theActionConfig
class used to store action configuration data.
Hidden Fields
_actionList
: A list that stores parsed actions to be executed._actions
: A list of strings representing individual actions extracted from the input string._currentNPC
: A reference to theConvaiNPC
component attached to the GameObject containing this script._interactablesData
: A reference to the global action settings object,ConvaiInteractablesData
, in the scene
Other Reference
ActionMethod
: A serializable class used to define individual action methods with name, animation, and choice.ConvaiAction
: A private class used to represent a parsed action with a verb, target GameObject, and animation name.
Functions
Awake()
: Initializes the Convai Actions Handler, finding global settings and checking for a Convai NPC component.Start()
: Sets up action configuration, including actions, characters, and objects, and starts playing the action list.ParseActions(string actionsString)
: Parses a string of actions and matches them with defined Convai actions and global action settings.PlayActionList()
: Manages playing actions from the action list using a coroutine.DoAction(ConvaiAction action)
: Executes specific actions based on theActionChoice
enum.AnimationActions(string animationName)
: Plays an animation for the Convai NPC with the provided animation name.ActionMethod
(Serializable Class): Defines individual action methods with name, animation, and choice.ConvaiAction
(Private Class): Represents a parsed action with a verb, target GameObject, and animation name.Crouch()
: Handles the crouching action, including animation and collider adjustments.MoveTo(GameObject target)
: Moves the Convai NPC to a target GameObject.PickUp(GameObject target)
: Performs the pick-up action on a target GameObject.Drop(GameObject target)
: Drops a target GameObject.Jump()
: Initiates a jump action with a specified jump force.
Imports
We will import Convai NPC Script which we will use to keep track of the current NPC by which the actions are supposed to be performed by.
ActionChoice
The Action Choice enum is used to display the list of actions in the editor. This helps us choose the action in the script. We use the enum later to choose the function corresponding to the action.
ActionMethod
This class is used to keep track of the animations and the action enum corresponding to each action. An array of this class is used to keep track of all the actions that the character can perform.
ConvaiAction
This class is used to internally track the actions. While parsing the action, we break it down into a verb and, if present, a target. We use this data in our parser.
Awake()
In the awake function, we initialize the Global Actions settings and get an instance of the current NPC's Convai NPC component.
Start()
In the start function, we will initialize the actions config that contains all the actions related information like the actions that the current character can do and objects and the characters present in the scene. It also starts the coroutine PlayActionList()
that listens and parses the actions that we receive as response from the server.
ParseActions()
The parse actions function takes the string containing the list of actions as an input and breaks it down into an object of ConvaiAction type. It converts the action to a verb and an object (or a target). It also gets a animation corresponding to the action string and the corresponding action enum.
After creating the Convai Action object, it adds it to a playlist, that will be played with the PlayActionList()
function.
PlayActionList()
This function checks a action playlist and execute the actions in order they are added to the playlist.
DoAction()
The Do Action function uses the action Enum and the Target to execute the function corresponding to the action.
AnimationActions()
The animation Actions function executes actions that do not have a corresponding functions. Specifically cosmetic actions that are only supposed to play an animation.
Action Implementation Methods
This region is where we add the functions corresponding to each action.
Last updated