Sitemap

A Basic Dialog System

5 min readJul 22, 2024

--

Just getting started…

The other day I was working on a dialog system to add gameplay interest.

The idea is to have an npc that can respond to a player when the player enters the npc’s ‘zone’.

The essence of the conversation would contain an initial dialog to get the player’s attention, a secondary conversation that would explain a need that the npc has, and a tertiary conversation that would thank the player for supplying the npc with an item as well as give the player a reward.

With this in mind I set out to create four scripts that would handle the npc’s dialog, the player’s inventory, the ui text display and the ability to pick up an item as well as give it to the npc.

Setting Up The Dialog Script

The first step was to outline the various dialog parts.

The entry dialog is a single string while the rest of the dialog uses arrays to allow for greater content. While my dialog segments are short and sweet, it would be possible to have a series of dialog elements that could be advanced in conjunction with a coroutine to advance them.

Press enter or click to view image in full size

The addition of a string to hold the name of the required item as well as the bool to determine if the item/tool has been given to the player help determine what is to be received by the npc as well as what is to be given to the player.

The reference to the UIManager allows for the dialog to be displayed on-screen and the AudioSource will play the AudioClip when the npc’s ‘demands’ have been met.

Press enter or click to view image in full size

Our Start() & Update() Methods

The Start() method assigns the audio source located on the npc while the Update() checks to see if the player is ‘in the zone’, if the player has given the tool/item to the npc and to see if the ‘E’ key has been pressed.

If all of these conditions have been met then the dialog that reflects that the conditions have been met is run

Press enter or click to view image in full size

The Dialog Methods

Our dialog methods, when called, will send the various dialog elements to the UIManager for display in a message panel on the UI.

The Trigger Methods

We will use all three of the built-in trigger methods to assist in our dialog system.

If the player enters the trigger then he is ‘in the zone’ and the player is ‘out of the zone’ when exiting the trigger.

Additionally, if the item has not yet been picked up and ‘given to the npc’ then the initial dialog will play.

Press enter or click to view image in full size

The OnTriggerStay Method

This is the main logic behind the dialog system.

Get Eric Young’s stories in your inbox

Join Medium for free to get updates from this writer.

We check to see if the player is in the zone, that the player has an inventory script and whether the tool has been given to the npc.

Next, we check to see if the player has the item in inventory and, if so, the dialog reflecting that the condition has been met will now play.

Further, we want the audio clip to play once and that the player receives the promised reward once.

— That wraps up the Dialog Script — — — — — — —

Next, we will deal with the UIManager to display the dialog text as is appropriate.

Press enter or click to view image in full size

The UIManager Script

The UIManger class has access to the text field for dialog display.

When the UpdateDialogText() function has been called:

  • all coroutines are stopped
  • the DialogTimer is started
  • the dialog element is displayed on the screen
  • and finally, after a period of time, the dialog text is cleared.
Press enter or click to view image in full size

The ItemPickup Script

This script is fairly straightforward.

When the player makes contact with the tool/item a reference to the player’s inventory is made.

If it exists, the tool/item’s name is not null and the tool has not yet been picked up then a bool is tripped, an audio is played and the item is added to the player’s inventory and the item is destroyed.

Press enter or click to view image in full size

The Player Inventory Script

Our final script for this lesson provides a method to AddItemToInventory() and is called by the ToolPickup script.

That wraps up our Basic Dialog System. Here are some images of the result:

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

— EYE —

--

--