Reactors
Reactors allow you to tap into state changes and messages that are passed around. The reactors are part of the ActorCore component.
In fact, the new combat flow uses the reactors to send and respond to combat messages. With this new approach, you can customize your character’s behavior or simply report on events using simple reactors.
This provides you with a way of modifying the game flow without having to modify any of ootii’s code directly.
In the list, we define the following:
- Type
- Name
- Priority (not used)
- Is Enabled
All reactors that can activate (and are enabled) will. They will activate in the order they are listed from top to bottom.
Activation
While custom reactors can be created to activate when ever needed, some of the stock reactors allow you to specify when the reactor will activate.
Choose an ‘Activation Type’ to determine if the reactor will activated based on the message ID received or state changes.
By selecting the type, other properties will enable and disable as appropriate.
In the case of ‘Message Received‘ activation, you’ll specify the message ID that is being looked for. These message IDs are simply integers that correspond to the ‘ID’ property of the IMessage interface. They can be any value you need, but a list of standard message IDs that I use can be found here.
In the case of ‘State Set‘ and ‘State Changed‘ activation, you’ll specify the state name that corresponds to an Actor Core State and the value you’re to react to.
Stock Reactors
The Actor Core includes several stock reactors that you can use out-of-the-box to handling combat, debugging, and more.
Basic Attacked Reactor – When a defender is attacked, this reactor runs and determines if the attack is blocked. Most of the work is done in the Basic Melee Block motion itself.
Basic Damaged Reactor – Used to apply damage to the specified health attribute and play the damaged animation.
Basic Killed Reactor – Used to flag the actor as dead and play the death animation.
Debug Log – Simply writes a line of text to Unity’s console.
Unity Event Proxy – This is a powerful reactor that will trigger custom functions you write.
Example
We could use a reactor to modify the character’s sword swing right before it happens. This way, we could change the attack style based on the environment, the enemy, etc.
Below, I’ve added a Unity Event Proxy as I’ll have some custom external code decide which attack style to use.
This reactor looks for the message ID “1100” which equates to “Before I actually swing my weapon”. When a message with that ID comes in, it uses Unity’s event system to call any function you want. In this case, I’m calling a function I created named “OnPreAttack”.
This function exists on a totally different component which means you can customize it without modifying any ootii code at all. In this case, the OnPreAttack function looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 using UnityEngine;
using com.ootii.Actors.Combat;
using com.ootii.Reactors;
public class NPCDemo01_CustomNPC : MonoBehaviour
{
public void OnPreAttack(ReactorAction rAction)
{
if (rAction == null || rAction.Message == null) { return; }
CombatMessage lCombatMessage = rAction.Message as CombatMessage;
if (lCombatMessage == null) { return; }
lCombatMessage.StyleIndex = 2;
}
}
In this function, we verify the message and choose which attack style to use.
This becomes pretty powerful as you can change which attack the NPC uses right before he does it. For example, you could check who the defender is in the CombatMessage is and change your attack to be more effective.
This is a simple comment.