Demo: Goal and Actions
1. Opening the Sample Scene
In the Project Panel
, click Inworld
> Inworld.Samples.RPM
> Scenes
> SampleGoals_Actions
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.
Select Advanced
> Goals
3. Workflow in Unity
The script TransformCanvas
attached to this scene will immediately send its first trigger, initconvo, which, upon the character's registration on the server during runtime (detailed below in Sending Triggers), will activate the init goal from the previous YAML.
- name: "init"
repeatable: false
actions:
- instruction: Tell {player} that {character} has been cursed by Wizard Dotcom and turned into a stone because one day he expressed hatred towards browsers and during a conversation with Wizard Dotcom, which angered Wizard Dotcom. Wizard Dotcom says only a spell to turn {character} back into a human.
activation:
trigger: "initconvo"
As a result, instead of you initiating the conversation, the character will start the conversation with you first.
If you typed or said the correct answer "WWW", it will be recoginized in the intent www we just defined. Here we added several ambiguous training phrases for "WWW" to improve the systems ability at recognizing this intent.
intents:
- name: "www"
training_phrases:
- "www"
- "the cure is www"
- "the cure spell is www"
- "w.w.w"
- "ww.w"
- "w.ww"
- "ww.w."
- "w w w"
This intent will then activate the the goal turnback.
- name: "turnback"
actions:
- instruction: Thank {player} for saving {character} from being trapped as a stone and wish them good fortune
emotion_change: JOY
activation:
intent: "www"
In Unity, if the received goal name matches the one we defined in the Check Trigger
field (m_CheckTrigger
) of Transform Canvas
(in this case, turnback), we carry out the following actions: hide the cube, reactivate the human avatar, and restart the lip animation.
public void OnGoalComplete(string trigger)
{
if (trigger != m_CheckTrigger)
return;
if (!m_CurrentCharacter)
return;
m_Stone.SetActive(false);
m_Avatar.SetActive(true);
m_LipAnimation.Init();
}
4. Done
Let's check the result in Unity.
5. Sending Triggers.
The InworldCharacter script contains a SendTrigger
method, which you can use to send a trigger to any character. When you want to send a trigger you have several options. By default, you just need to specify the name of the trigger when you call SendTrigger
:
public virtual void SendTrigger(string trigger, bool needCancelResponse = false, Dictionary<string, string> parameters = null)
{
// 1. Interrupt current speaking.
if (needCancelResponse)
CancelResponse();
// 2. Send Text. YAN: Now all trigger has to be lower cases.
InworldController.Instance.SendTrigger(trigger.ToLower(), ID, parameters);
}
If you wish to immediately interrupt the character using the trigger, you can set needCancelResponse
to true (by default, it's set to false). In this case, the character will halt its speech and immediately process the trigger, similar to what we did in the sample scene. Otherwise, the trigger will be sent without halting the current speech and the response will be processed after the current interaction completes.
6. Sending Triggers with Parameters
You can also send triggers with parameters, which must be defined in the Inworld Studio YAML for your character, as shown below:
- name: "introduction"
actions:
- instruction: Say he likes {{p.item}}
activation:
trigger: "saylike"
In YAML, you can use a special syntax, {{p.YOUR_PARAM_NAME}}
. In the example above, we defined our parameter name as item.
Then, in Unity, we define a dictionary with item as the parameter name and strawberry as the parameter value, like this:
void SayLike()
{
Dictionary<string, string> param = new Dictionary<string, string>
{
["item"] = "strawberry"
};
m_CurrentCharacter.SendTrigger("saylike", true, param);
}
Now, let's see what happens in Unity when we call this function.
7. Full YAML Reference
Here's the sample YAML for the character:
goals:
- name: "turnback"
repeatable: true
actions:
- instruction: Thank {player} for saving {character} from being trapped as a stone and wish them good fortune
emotion_change: JOY
activation:
intent: "www"
- name: "init"
repeatable: false
actions:
- instruction: Tell {player} that {character} has been cursed by Wizard Dotcom and turned into a stone because one day he expressed hatred towards browsers and during a conversation with Wizard Dotcom, which angered Wizard Dotcom. Wizard Dotcom says only a spell to turn {character} back into a human.
activation:
trigger: "initconvo"
- name: "introduction"
actions:
- instruction: Say he likes {{p.item}}
activation:
trigger: "saylike"
intents:
- name: "www"
training_phrases:
- "www"
- "the cure is www"
- "the cure spell is www"
- "w.w.w"
- "ww.w"
- "w.ww"
- "ww.w."
- "w w w"