Demo: Trigger
1. Opening the Sample Scene
In the Project Panel
, click Assets
> Inworld.AI
> Scenes
> SampleTrigger
to open the demo scene that showcases Goals and Actions in use. In this scene, there is a character named Prince Stone in the room. However, he is initially in the form of a stone (specifically, a cube) and the player must provide the magic words to reverse his state.
2. Creating Goals and Actions
Goals and actions can be created for each character at https://studio.inworld.ai/ by going to their respective edit pages.
Since this is currently an experimental feature, it must be enabled in the Experimental Features tab.
The script TransfromCanvas
attached to this scene will immediately send its first trigger, initconvo
, which corresponds to the goal Start Conversation
, as soon as the character is registered on the server during runtime (detailed below in Sending Triggers).
This is an Invoke Response trigger, which means that when it is sent to the server, the character will respond immediately. In this scene, instead of having to initiate a conversation with the character, the character will speak to you first. Characters can be given instructions in the format detailed here and can also be configured to have their emotions change on receiving the corresponding event.
3. Sending Triggers
There are two triggers being sent in this sample: initconvo
and magicwords
. The script TransformCanvas
and InworldKeywordListener
are both on the same gameobject and they both send each trigger respectively.
initconvo
is sent as soon as the character is registered on the server during runtime so it will begin a conversation without being prompted. This is shown below:
void Start()
{
InworldController.Instance.OnStateChanged += OnStatusChanged;
InworldController.Instance.OnCharacterChanged += OnCharacterChanged;
InworldController.Instance.OnPacketReceived += OnPacketEvents;
}
protected override void OnCharacterChanged(InworldCharacter oldCharacter, InworldCharacter newCharacter)
{
if (!newCharacter && oldCharacter)
m_Title.text = $"{oldCharacter.transform.name} Disconnected!";
else
{
m_Title.text = $"{newCharacter.transform.name} connected!";
m_CurrentCharacter = newCharacter;
if (m_CurrentCharacter.Data.characterName == m_CharData.characterName)
{
InworldAI.Log("Sending trigger " + m_CurrentCharacter.Data.triggers[0] + " for the character to start the conversation first");
m_CurrentCharacter.SendTrigger(m_CurrentCharacter.Data.triggers[0]);
}
}
}
magicwords
is sent on saying the correct magic words to turn the stone back into a prince. In this scene, the actual solution to lift the curse isWWW
. If you sayWWW
(or a related phrase) to the character, then he will transform back into a human after theInworldKeywordListener
sends the triggermagicwords
to the server. You can also account for variations of words or synonyms accordingly.
You will see that the character will respond differently and thank you for your help if the Magic Words
goal is activated. This follows from how the instruction for the goal has been configured.
You can also send scene triggers to add immediate context to a change that may occur in a scene (see: here).
4. Receiving User-Defined Events
The Magic Words
goal also has a user-defined event turnback
which is sent to the Unity client when it is activated. In this case, it is used to determine that the prince can be turned back into a human.
This is also received in the InworldKeywordListener
where the TriggerResponse
field has been defined as turnback
.
Inside the InworldKeywordListener
, the OnPacketEvents
callback is used to determine if the incoming packet is a CustomEvent
and the name of the event corresponds to the goal which was activated.
void _HandleCustomEvent(CustomEvent customEvent)
{
if (customEvent.Name == triggerResponse)
OnTriggerReceived.Invoke();
}