Hands-On Challenge Overview
This challenge introduces you to the NPC gizmo and how to set up AI conversations between the NPC and players.
You’ll learn how to:
- Set up the NPC gizmo.
- Set up the NPCs TypeScript to allow for AI conversations between the NPC and players.
- Add context and environmental events to the NPC so they are aware of the world around them.
Estimated Time: 30m
What You'll Need:
- Desktop Editor
- Empty World
- VSCode or other IDE
Challenge Steps:
While this challenge may seem daunting and highly detailed the tasks are quite simple. Follow along at your own pace and you will be conversing with your NPC in no time.
1. Set up the NPC
- Place an NPC gizmo in the world.
- Give the NPC a name.
- Select the NPC in the Hierarchy.
- Under NPC in the Properties panel.
- Add a name in the Character Name field.
- NOTE: This is required in order for the Character Builder to be clickable.
- Build the NPC
- Click None next to Conversation under NPC in the Properties panel and select AI.
- Click the Open button next to Character Builder under NPC in the Properties panel.
- Generate your NPCs look using Meta Avatars or Meta AI.
- Select Character Description under Personality in the Character Builder window.
- Give the NPC a name and click the Create button.
- Add a backstory that fits the character and click the Save button.
- NOTE: The backstory is like all the info the NPC will have access to.
- Select Instructions under Personality in the Character Builder window.
- Instructions help build the personality of the NPC.
- NOTE: They are like guardrails that keep the NPC in check. Read the Example text in the Instructions text box. Once done click the Save button.
- Select Voice and Speech under Personality in the Character Builder window.
- Find a voice in the Voice Library or Generate a voice using the Generate Voices button.
- Assign the voice and click the Save button.
2. Add Player Conversation Mechanic
- Create and attach a script called
NPCPlayerConversation to the NPC gizmo. - Click the Attach script button in the NPCs Properties panel.
- Click New NPC conversation script.
- Name it
NPCPlayerConversation. - Click the Create and attach button.
- Place a Trigger Zone in front of the NPC gizmo.
- Build > Interactions > Trigger Zone.
- Edit the
NPCPlayerConversation script - Scripts > Scripts in the world >
NPCPlayerConversation
- Connect to the triggers events
- Create a props Definition for the trigger.
- Add the
preStart() method to the script. - Add the
connectCodeBlockEvent for the OnPlayerEnterTrigger event. - Add the
connectCodeBlockEvent for the OnPlayerExitTrigger event.
- Set up the conversation
- In the
OnPlayerEnterTrigger event register the player as a conversation participant with this.npc.conversation.registerParticipant(player). - In the
OnPlayerExitTrigger event unregister the player as a conversation participant with this.npc.conversation.unregisterParticipant(player).
- Test
- Save the script and return to the Desktop Editor.
- Wait for the script to compile then press play.
- Connect the trigger to the NPCs new trigger property.
- Ensure you are not muted and enter the trigger.
- NOTE: You may need to enter preview to get the NPC to register the player.
Level Up:
For these challenges we will be looking at Dynamic Context and Event Perception. Dynamic Context are things like environmental conditions or changing of states and can be undone. Event Perceptions are permanent and cannot be undone once added.
1. Add Dynamic Context to the NPC
- Open the script you created during the challenge above.
- When the player enters the trigger, set a dynamic context with
this.npc.conversation.setDynamicContext(). This takes a key value pair and allows you to set the same key multiple times or create new ones. For example the key could be power and the value could increase with the amount of players in the trigger or decrease when the player exits the trigger. - Now save and test. Ask the NPC about the key you used in the
setDynamicContext().
2. Add Event Perception to the NPC
- Open the script you created during the challenge above.
- When the player enters the trigger, add an event perception with
this.npc.conversation.addEventPerception(). This just takes a description of the event. For example a ship just blew up. - Now save and test. Ask the NPC about the event you used in the
addEventPerception().
Need Help?
Related Links