Talking to NPCs is a fun activity in any game – let’s take a look on how to get a little bit of variation into it.
We’ve already created dialogue events – but this time we’ll use Object Game Variables to cycle through different dialogues when talking to the NPC multiple times. You can learn more about game variables in general in this how-to.
Setting up the NPC
We’ve already added some NPCs, so let’s get on with it. Open the town scene (1 Town, can be found in Assets/Tutorial Resources/Scenes/) and add the NPC_purple prefab (can be found in Assets/Tutorial Resources/NPCs/). I’ve added mine on the right side of the tree.
Use the ORK Scene Wizard to add a Place On Ground component (Add Component > Place On Ground). Now, since we’re using Object Game Variables, we need to add an Object Variables component to our NPC, again, using the scene wizard: Add Component > Object Variables
The object variables component will automatically generate an Object ID for us – this ID is used to keep track of the game variables bound to this object. Similar to shops, all game objects sharing the same object ID will also share the same game variables.
Finally, we’ll add an Event Interaction component (Add Component > Event Interaction). Click on Create New Event in the object’s inspector to open the ORK Framework editor and create a new event for our NPC.
The changing dialogue
This event will, similar to our quest event, display different dialogues depending on a Game Variable – but this time, it’s an Object Game Variable.
We’ll use a float variable named counter to cycle through the different dialogues, the variable is increased by 1 every time a dialogue has been displayed. After the last dialogue, we’ll reset the variable.
Event Settings
- Blocking Event
Enable this setting. - Block Player Control
Enable this setting. - Block Camera Control
Enable this setting.
We’ll need one actor, so click on Add Actor.
- Type
Select Object. - Event Object
Enable this setting. - Set Name
Enable this setting. - Name (English)
Set to Purple Pants.
That’s it for the event settings.
Check Game Variables
Add > Value > Variable > Check Game Variables
First, we’ll check if counter is set to 0 – by default, all float variables (also those who haven’t been used) are 0.
- Variable Origin
Select Object. - Use Object
Enable this setting.
We’ll use an object of the event, else we could define the Object ID of the object variables. - Object
Select Actor. - Actor
Select Purple Pants.
Click on Add Game Variable to add a variable condition.
- Value Type (Variable Key)
Select Value. - Value (Variable Key)
Set to counter. - Is Valid
Enable this setting. - Type
Select Float. - Check Type
Select Is Equal. - Value Type (Check Value)
Select Value. - Value (Check Value)
Set to 0.
Show Dialogue
Add > UI > Dialogue > Show Dialogue
Our first dialogue – connected to the Success slot of the counter is 0 check.
- Dialogue Type
Select Message. - GUI Box
Select Bottom Dialogue. - Use Speaker
Enable this setting. - Actor
Select Purple Pants. - Show Name
Enable this setting. - Message
Set to: Hi there!
Change Game Variables
Add > Value > Variable > Change Game Variables
This step will increase our counter by 1 – we’ll use this step multiple times.
- Variable Origin
Select Object. - Use Object
Enable this setting. - Object
Select Actor. - Actor
Select Purple Pants.
Click on Add Game Variable.
- Value Type (Variable Key)
Select Value. - Value (Variable Key)
Set to counter. - Type
Select Float. - Operator
Select Add. - Value Type (Float Value)
Select Value. - Value (Float Value)
Set to 1.
Check Game Variables
Add > Value > Variable > Check Game Variables
The 2nd variable check will check if counter is set to 1. This step is connected to the Failed slot of the counter is 0 check.
- Variable Origin
Select Object. - Use Object
Enable this setting.
We’ll use an object of the event, else we could define the Object ID of the object variables. - Object
Select Actor. - Actor
Select Purple Pants.
Click on Add Game Variable to add a variable condition.
- Value Type (Variable Key)
Select Value. - Value (Variable Key)
Set to counter. - Is Valid
Enable this setting. - Type
Select Float. - Check Type
Select Is Equal. - Value Type (Check Value)
Select Value. - Value (Check Value)
Set to 1.
Show Dialogue
Add > UI > Dialogue > Show Dialogue
The 2nd dialogue is connected to the Success slot of the counter is 1 check.
- Dialogue Type
Select Message. - GUI Box
Select Bottom Dialogue. - Use Speaker
Enable this setting. - Actor
Select Purple Pants. - Show Name
Enable this setting. - Message
Set to: Can I help you?
Connect the Next slot of this step to the Change Game Variable step we created earlier.
Check Game Variables
Add > Value > Variable > Check Game Variables
The last variable check will check if counter is set to 2. This step is connected to the Failed slot of the counter is 1 check.
- Variable Origin
Select Object. - Use Object
Enable this setting.
We’ll use an object of the event, else we could define the Object ID of the object variables. - Object
Select Actor. - Actor
Select Purple Pants.
Click on Add Game Variable to add a variable condition.
- Value Type (Variable Key)
Select Value. - Value (Variable Key)
Set to counter. - Is Valid
Enable this setting. - Type
Select Float. - Check Type
Select Is Equal. - Value Type (Check Value)
Select Value. - Value (Check Value)
Set to 2.
Show Dialogue
Add > UI > Dialogue > Show Dialogue
The 3rd dialogue is connected to the Success slot of the counter is 2 check.
- Dialogue Type
Select Message. - GUI Box
Select Bottom Dialogue. - Use Speaker
Enable this setting. - Actor
Select Purple Pants. - Show Name
Enable this setting. - Message
Set to: Why aren’t you saying anything?
Connect the Next slot of this step to the Change Game Variable step we created earlier.
Show Dialogue
Add > UI > Dialogue > Show Dialogue
The last dialogue is connected to the Failed slot of the counter is 2 check.
- Dialogue Type
Select Message. - GUI Box
Select Bottom Dialogue. - Use Speaker
Enable this setting. - Actor
Select Purple Pants. - Show Name
Enable this setting. - Message
Set to: Please go away …
Change Game Variables
Add > Value > Variable > Change Game Variables
Finally, we’ll reset the counter to 1 – i.e. the first dialogue will only appear once.
- Variable Origin
Select Object. - Use Object
Enable this setting. - Object
Select Actor. - Actor
Select Purple Pants.
Click on Add Game Variable.
- Value Type (Variable Key)
Select Value. - Value (Variable Key)
Set to counter. - Type
Select Float. - Operator
Select Set. - Value Type (Float Value)
Select Value. - Value (Float Value)
Set to 1.
And that’s it for the event – click on Save Event and save it as changingDialogue in Assets/Events/Town/. Close the ORK Framework editor.
Back to town
Now, back in the town scene, let’s add the event to our NPC. Change the following in the Event Interaction inspector.
- Event Asset
Select changingDialogue.
Click on Ok to accept the selected event.
- Start Type
Select Interact. - Turn Player to Event
Enable this setting. - Turn Event to Player
Enable this setting.
And that’s it – save the scene.
Testing
Open the main menu scene (0 Main Menu) and hit play. Go and talk to Purple Pants multiple times.
Since we’re using an Object Game Variable, we can use the same variable key (counter) in other events as well, without interfering with the dialogues of Purple Pants.
When saving, the object game variables will also be saved (when set up that way in the Save Game Menu).
And that’s it for now – the next lesson will cover item collectors.