Using Emerald AI Unity Events
Emerald AI has 11 built-in Unity Events that are called during various Emerald AI actions. You can use these events to trigger custom function calls and other mechanics to expand functionality with AI.
The Emerald AI Unity Events can be found on the AI's Settings>Events tab. Here, you will see 2 tabs; one for General Events and one for Combat Events. Each Event has + and - button that allows you to add or remove an event. When the + button is pressed, it will create a new blank element event for that Event List. This will allow you to apply the AI gameobject as the reference object, to call any custom function or Emerald AI's API functions that are available on your AI.
An empty Emerald AI Unity Event:
Creating a new, unassigned, Emerald AI Unity Event after pressing the + button:
Assigning your AI object as the object reference on the Emerald AI Unity Event:
All available scripts, functions, components, and API on reference object for the Emerald AI Unity Event. An Emerald AI Unity Event is called when the specified event is invoked. In this Event's case, it's called each time the AI is damaged.
Emerald AI's Unity Events are located on the EmeraldAISystem script. To access these, you will need a reference to the EmeraldAISystem script. This can be done using the code below. It is best to have a class level variable for EmeraldComponent so it can be accessed anywhere within your script.
EmeraldComponent = GetComponent<EmeraldAISystem>();
You can add Emerald AI Unity Events programmatically using the following code format. These should be applied on Start.
EmeraldComponent.TheEmeraldAIUnityEvent.AddListener(() => { YourFunctionToCall(); });
For example, lets say you want to track each AI death and add it to a static variable so the player can track their kills. For this example, the kill count script name is KillCounterSystem and the static variable is an int called AmountOfKills.
...
void Start ()
{
//Create a DeathEvent by adding a listener and applying the CountDeath function.
//Even though this is assigned on start, the CountDeath function will be called when the AI dies.
EmeraldComponent.DeathEvent.AddListener(() => { CountDeath(); });
}
//The CountDeath function adds the AI's death to the static variable AmountOfKills for tracking kills.
void CountDeath ()
{
KillCounterSystem.AmountOfKills++;
}
...
The DeathEvent is invoked when an AI is killed.
Example:
EmeraldComponent.DeathEvent.AddListener(() => { YourCustomFunction(); });
The OnTakeDamageEvent is invoked each time an AI is damaged.
Example:
EmeraldComponent.DamageEvent.AddListener(() => { YourCustomFunction(); });
The OnDoDamageEvent is invoked each time an AI deals damage for both Melee and Ranged weapon types.
Example:
EmeraldComponent.OnDoDamageEvent.AddListener(() => { YourCustomFunction(); });
The OnReachedDestinationEvent is invoked when an AI reaches their destination location (must be using the Destination Wander Type).
Example:
EmeraldComponent.ReachedDestinationEvent.AddListener(() => { YourCustomFunction(); });
The OnStartEvent is invoked once within the AI's Start function.
Example:
EmeraldComponent.OnStartEvent.AddListener(() => { YourCustomFunction(); });
The OnEnabledEvent is invoked when an AI is enabled after it has been deactivated.
Example:
EmeraldComponent.OnEnabledEvent.AddListener(() => { YourCustomFunction(); });
The OnPlayerDetectionEvent is invoked when an AI detects a player object. Users can adjust the cooldown for how often this event is invoked within the Emerald AI editor.
Example:
EmeraldComponent.OnPlayerDetectionEvent.AddListener(() => { YourCustomFunction(); });
The OnAttackEvent is invoked when an AI triggers their attack animation.
Example:
EmeraldComponent.OnAttackEvent.AddListener(() => { YourCustomFunction(); });
The OnFleeEvent is invoked when an AI begins to flee (This only happens with AI that have a Brave or Coward Confidence Level).
Example:
EmeraldComponent.OnFleeEvent.AddListener(() => { YourCustomFunction(); });
The OnStartCombatEvent is invoked when an AI first enters combat and is reset when the battle is over.
Example:
EmeraldComponent.OnStartCombatEvent.AddListener(() => { YourCustomFunction(); });
The OnKillTargetEvent is invoked when an AI has killed their target. This event is called before an AI's target has been cleared so it can still be accessed if needed.
Example:
EmeraldComponent.OnKillTargetEvent.AddListener(() => { YourCustomFunction(); });
- Version 3.0 Upgrade Guide
- Creating an AI through the Setup Manager
- Setting up an AI's Animations
- Creating Attack Animation Events
- Creating an AI Ability Object
- Applying an AI's Head Transform
- Applying an AI's Ranged Attack Transform
- Setting up an AI's Factions
- Setting up an AI's Layers and Tags
- Using the Combat Text System
- Setting up an AI's Optimization Settings
- Setting up an AI with Ragdoll Deaths
- Creating a Shooter AI
- Creating a Floating or Hovering AI
- Using Emerald AI's Built in Object Pool
- Using the Emerald AI Needs System
- Using Emerald AI with Crux Spawner
- Using Location Based Damage
- Using the Emerald AI Camera Shake System
- Setting up an Equippable and Unequippable Weapon
- Setting up Ranged Weapon Hand IK
- Using the Target Position Modifier
- Using the Built-in IK System
- Setting up Non-AI Targets
- Setting up Custom Damage with Emerald AI
- Using the Sound Detector System
- Setting up the Built in Health Bars and Name Text
- Behaviors and Confidence Levels
- Wander Types and Movement
- Using Factions and Faction Manager
- Detection Type and Target Picking
- Adjusting an AI Hit's Transform
- Enabling an AI's Debugging Tools