• Guides
  • Scripting
  • Visual Scripting
  • Communication with NPC

Communication with NPC


An NPC is a non-player character that is not controlled by the players in the game. Generally speaking, NPCs are the characters controlled by the game itself, rather than the players. They are generally friendly, or at least not hostile toward the players. Interacting with NPCs is a widely used game mechanic. In this part, you will achieve the effect of talking to the NPCs.

The player will be able to approach or click to have a dialogue with NPCs. The player can also close the dialogue and interact with other NPCs at any time. This part is introduction of using ECA, the lua can be known at Lua API – Communication with NPC.

UI content reading and display

The first part is UI content reading and display. First of all, you need to set all the NPCs' chat content in Object Editor. You can add a custom attribute "Talk" and store the content for each NPC.


We also need to design the chat UI in UI Editor. It should at least include a background, an icon of the NPC, text, and an Exit button.


Then, the chat UI needs to be able to read and display the correct information. You can custom a function "Write unit information" with a parameter "NPC" to achieve this. Set two variables to get the icon image ID and the dialogue content "Talk" you preset. Then, set the UI widget "Icon" and "Talk text" using the two variables.

In this way, when you call this function later and specify the NPC, the corresponding icon and dialogue content of this NPC will be obtained and displayed on the chat UI.


Unified entrance for NPC chat

The second part is unified entrance for NPC chat. This means when players approach or click any NPCs, the program will be notified and run in the same path. So, you can custom a function "Trigger NPC Chat" to pack the path and instruct the hero to talk to the NPC.

Then, call the function "Write unit information" to write the NPC information to the chat UI, and display it to the player.

You also need to set the Exit button. Create a boolean variable "Switch" to record the state of the button. When the UI Event Exit is triggered, set "Switch" to be True, which represents the button is pressed.


Then, in function "Trigger NPC Chat", you need to check the distance between the hero and the NPC every second, using a looped timer. If the distance is greater than 500, it means the player moves away. Prompt a message "Too far away" to the player, remove the timer, and turn off the chat UI.


If the player is still near the NPC, also need to check the state of the Exit button. If the boolean variable "Switch" equals True, it means the Exit button is pressed. The player wants to exit the chat. Remove the timer, turn off the chat UI and reset the the state of the button, that is, setting the variable "Switch" to be False.


The function is almost done. Now, you need to set the triggers of the approach event and click event.

When the hero unit controlled by the player enters the area of one NPC, they call the function "Trigger NPC event" to talk to the NPC.

When the player selects a unit on the map, you need to compare the unit first. The unit in the event should not be the hero unit controlled by the player. If the condition is met, you need to make the player re-select the hero unit first.


Then, program would be better that interaction happens only when the hero unit is in a certain range of the NPCs. So, check the distance between the event unit, that is the NPC, and the hero unit. If the distance is less than 400, use the function to instruct the hero unit to talk to the NPC. Otherwise, can prompt "It's too far away!"