In this tutorial we’ll add an NPC to speak to.
Speaking to an NPC, opening a chest … these are interactions with the game world.
For the player to be able to interact with something, we need an Interaction Controller.
We’ll set up
- interaction controller (prefab + setup)
- a dialogue schematic
- NPC in town
Let’s get to it!
Interaction Controller #
The Interaction Controller is used to interact with any interaction within it’s trigger when the player uses the Interact key.
You can even use multiple interaction controllers on the combatant to interact with things at different distances, e.g. speaking to an NPC when standing before it but collecting items all around. See this tutorial for an example setup.
We’ll create a prefab that’s automatically added to the player.
Prefab Setup #
We start with setting up the prefab – you can do this in any of the scenes.
As usual this is pretty easy using the scene hierarchy’s context menu: Makinom > Interaction Controller (2D)
Change the following settings in the inspector.
Box Collider 2D #
- Size
Set to X=1, Y=1.
Create Prefab #
Create a prefab out of the Interaction Controller (2D) game object.
Remove the game object from the scene.
Interaction Settings #
Open the Makinom editor and navigate to Base/Control > Game Controls.
We’ll now set up to automatically add the interaction controller to our player. It will be placed on a child object that rotates with the player’s movement direction.
Interaction Settings #
The default setup should already use an interaction controller and the Accept input key.
- Interaction Control
Select Interaction Controller. - Interact Key
Select Accept.
The Interaction Controller settings handle adding our prefab to the player.
- Add Automatically
Enable this setting. - IC Prefab
Select the Interaction Controller prefab you created. - On Child
Select Path.
We’ll define a path to a child object. - Find Child
Set to: Mount/Front
The interaction controller will be placed on this child object, which will automatically rotate into the direction the combatant moves in.
NPC Combatant #
NPCs don’t have to be combatants, but they can be to make use of certain combatant-related functionality. E.g. using the animation setup or move AI.
We’ll set up a simple combatant for our NPCs to make use or the animations – i.e. having the NPC turn to the player. This is only needed due to our 2D setup, where the directional sprite is based on the rotation. E.g. in a 3D game this would not be needed, as we can directly rotate the game object without worrying.
Navigate to Combatants > Combatants and add a new combatant.
- Name
Set to: NPC
That’s all we need, since the rest is already covered by the generail settings.
We also don’t need a prefab, because we’ll add the combatant to an NPC in the scene using an Add Combatant component.
Save Changes #
Save the changes before we set up the dialogue schematic.
Don’t forget to save your changes by clicking on Save Settings at the bottom of the editor.
Dialogue Schematic #
We’ll set up a dialogue that has changing messages each time we talk to the NPC.
This is done by using variables – object variables on our NPC’s game object to be precise. We’ll use an int variable with the variable key counter to count the dialogues.
Navigate to Schematics.
Settings #
- Block Player Control
Enable this setting.
We don’t want our player to run around while talking to the NPC.
Actors #
Dialogue speakers require actors, so let’s add one for the NPC.
Click on Add Actor.
- Actor Type
Select Machine Object.
The Interaction Machine we’ll use to start the dialogue will be placed on the NPC, so the machine object will be the NPC’s game object. - Set Name
Enable this setting.
We’ll define a name here – alternatively, otherwise it’ll use the combatant’s content information. - Default Content
Set to: Random Citizen
Variable Fork #
Add Node > Value > Variable > Variable Fork
This node is used to check a single variable for multiple values – each value check has it’s own next slot to continue from.
We’ll cycle through 3 different dialogues, so we’ll need 3 conditions.
- Variable Key
Set to counter. - Variable Origin
Select Object.
This’ll use object variables attached to a game object (via an Object Variables component). - Object
Select Machine Object.
There’s already a variable condition added for us, so let’s change Variable Condition 0 to our needs.
- Type
Select Int. - Check Type
Select Is Equal. - Check Value
Select Value > Value.
Set the value to 0.
0 is also the default value for any not yet set int variable (for bool variables it’d be false, for string an empty text, etc.).
Copy Variable Condition 0.
- Check Value
Select Value > Value.
Set the value to 1.
Copy Variable Condition 1.
- Check Value
Select Value > Value.
Set the value to 2.
Show Dialogue #
Add Node > UI > Dialogue > Show Dialogue
We’ll show the first dialogue.
This node is connected to the is Equal 0 slot of the Variable Fork node.
- Dialogue Type
Select Message.
We simply display a message text. - UI Box
Select Dialogue.
The Speaker Settings handle if a speaker is used, e.g. to display a name in the UI box’s title content.
- Use Speaker
Enable this setting. - Actor
Select Actor 0: Random Citizen. - Show Name
Enable this setting. - Text (Message Content)
Set to: Hello there!
Show Dialogue #
Copy the previous Show Dialogue node.
This node is connected to the is Equal 1 slot of the Variable Fork node.
We’ll just change the message.
- Text (Message Content)
Set to: Can I help you?
Show Dialogue #
Copy the previous Show Dialogue node.
This node is connected to the is Equal 2 slot of the Variable Fork node.
We’ll change the message again.
- Text (Message Content)
Set to: Please go away …
Change Variables #
Add Node > Value > Variable > Change Variables
We’ll now increase the counter variable by 1.
This node is connected the Next slots of all Show Dialogue nodes.
Click on Add Variable.
- Change Type
Select Variable. - Variable Key
Set to counter. - Variable Origin
Select Object. - Object
Select Machine Object. - Type
Select Int. - Operator
Select Add. - Float Value
Select Value > Value.
Set the value to 1.
Change Variables #
Copy the previous Change Variables node.
We’ll now set the counter variable to 0 in case the initial Variable Fork check failed.
This node is connected the Failed slot of the Variable Fork node.
Connect the node’s Next slot back to the Variable Fork node – i.e. if the fork failed, it resets the variable and checks again, restarting the dialogue.
- Operator
Select Set. - Float Value
Select Value > Value.
Set the value to 0.
And that’s it for the schematic – save it, e.g. as ChangingDialogue.
Adding the NPC #
Open the Town scene – we’ll now add the NPC to talk to.
Add the Villager4 prefab to the scene, you can find it in Assets/Tutorial Assets/NinjaAdventure/Actor/Characters/Villager4/.
I added mine beside the river:
Rigidbody #
We set the NPC to be kinematic, otherwise the player would be able to push it around.
- Body Type
Select Kinematic.
Object Variables Component #
Add an Object Variables component to the NPC’s game object using the component menu.
This component is used to bind object variables to a game object and allows the Object variable origin to find variables on a game object.
Object variables can either persist and be shared between multiple game objects by using the same Object ID to identiy them, or be used as local variables. We’ll use them as local variables, i.e. they’re only accessible for this game object, not shared and not saved with save games. Once the game object is destroyed (e.g. when we change scenes), they’ll be gone. That’s all we need for our changing dialogue.
- Local Variables
Enable this setting.
Interaction Machine #
Add an Interaction Machine component to the NPC’s game object using the component menu. Learn more about interaction machines in this Makinom documentation.
Start Settings #
- Interact
Enable this setting. - Mouse Up As Button
Enable this setting in case you also want to start the interaction by clicking on the NPC.
Machine Execution Settings #
- Schematic Asset
Select the ChangingDialogue schematic you created.
Expand the Object Turn Settings, we’ll let the player and NPC face each other. Our combatant animation setup will take care of displaying the correct sprite directions.
- Turn Starting Object
Enable this setting. - Turn Machine Object
Enable this setting.
Add Combatant #
Add an Add Combatant component to the NPC’s game object using the component menu.
This component will add a combatant instance to the game object instead of spawning a new combatant using it’s prefab.
Combatant Settings #
- Faction
Select Allies. - Combatant
Select NPC.
And that’s it – save the scene.
Testing #
Hit play and walk up to the NPC.
Using the accept input key (Space) lets us speak with the NPC and cycle through the dialogue.
Next, we’ll add treasure chests.