Unit 3 / 11

NPC Behavior and Dialogue Systems: Artificial Intelligence That Brings the Character Alive

Gains:

  • Ability to distinguish between write time and runtime artificial intelligence usage and produce NPC dialogue with character card and negative constraints
  • Ability to draft NPC behavior structures such as state machine and behavior tree with artificial intelligence and verify them in the engine
  • Understanding why guardrail, content filtering and precautions against prompt injection are mandatory in live (runtime) artificial intelligence NPCs

What makes a game unforgettable is often not the mechanics, but the characters the player encounters. NPC (non-player character — character that is not controlled by the player and is managed by the system); It could be a quest-giving villager, an enemy guard, a traveling companion, or a shopkeeper. The NPC's believability depends on two things: its behavior (what it does — patrolling, attacking, running away) and its dialogue (what it says — its personality, tone, information it gives to the player). AI transforms both of these areas: it both speeds up and enriches dialogue writing and helps draft behavior design (state machines, decision trees).

In this unit you will learn how to use AI for NPC dialogue and behavior; You'll learn how to preserve character identity, write branching dialogue, and the risks of systems that generate creatures at runtime.

Two different uses: write time and run time

Make a critical distinction. Design-time use: You use AI during development to produce dialogue outlines, character backgrounds, and behavior design; you read the output, fix it, and embed it in the game. This is the safe and common way. Runtime usage: The AI ​​produces live dialogue while the player is playing (the NPC responds to whatever the player types). This is impressive but risky: NPC may say things that are out of control, out of character or inappropriate, incur costs and latency, cannot be played offline. In this unit, we will focus mainly on write time and finally on the rules of runtime.

Character ID: every NPC must have a "voice"

The most common mistake is that all NPCs speak with the same AI tone — all polite, balanced, generic. To avoid this, write a character bible for each important character: their name, background, purpose, fear, style of speaking (short or long, slang or formal), dislikes. Give this card as context every time you write dialogue to the AI. Thus, the guard speaks like a soldier, the witch speaks mysteriously, the child speaks curiously.

Step by step dialogue generation:

  1. Write your character card and give it to the AI.
  2. Identify the context of the scene (where, what is happening, what the actor did).
  3. State the purpose of the dialogue (give a task, offer a hint, threaten).
  4. Ask for branching (3 different responses to the player's 3 different responses).
  5. Authenticate: does the output match the character card?
Tip: Give each character a "never say" list. For example, a tough mercenary doesn't say "please" and "I'm sorry." These negative constraints prevent the AI ​​from flattening the character.

Behavior design: state machines and decision trees

NPC behavior is generally established through two structures. State machine (FSM) — finite state machine: The NPC transitions between specific states — “patrol,” “chase,” “attack,” “escape” — and has conditions for transitioning from each state to the next. Behavior tree: organizes more complex decisions in a branched tree structure. AI is powerful at generating outlines of these structures: tell it the NPC's purpose and possible states, have it list transition conditions and edge cases. But remember — the behavioral logic the AI ​​produces is a draft until it is tested in the engine and verified in gameplay.

Caution: A guardrail layer is essential when generating live dialogue with the AI ​​in runtime: limit the character with the system prompt, filter out inappropriate content, guard against the player corrupting the character with prompt injection — an attempt to trick the NPC into behaving illegally. An unprotected live NPC is a reputation risk for your brand.

Dialogue's connection with game logic

Dialogue is not just text; speaks to the game situation. A good NPC replica reflects what the player did: he speaks differently if he completed the quest, and another if he failed. Give these status variables (player level, mission status, reputation, item in inventory) as context when generating dialogue with the AI; so the dialogue feels "alive". But a technical caveat: the dialogue the AI ​​produces must match the actual variable states the game will present; A line that mentions a mission that doesn't exist or an item the player doesn't have makes the game less believable. Therefore, when generating conditional dialogue branches, clearly define under which game condition each branch will be triggered and test it in the engine.

Another subtlety is tempo and length. AI is prone to long and literal speeches unless asked; However, in the game, the actor usually wants short lines that do not interrupt the gameplay. Give constraints in the prompt, such as "no more than two sentences", "don't make the player wait", etc. Shortening the text while preserving the character's voice is the sign of good playwriting.

three mini cases

Case 1 — ID card difference. An RPG studio first printed 40 NPC dialogues without cards; "Everyone talks the same," the players said in the playtest. They added and reproduced 6-line character cards for each NPC; In the player survey, the "characters are memorable" score increased from 2.8 to 4.3 out of 5.

Case 2 — Richness of branching. One designer branched a single key scene with the AI ​​into 3 player attitudes (aggressive, diplomatic, evasive). My writing took 2 hours; If he wrote it by hand, it would take half a day. The designer read each branch, corrected the tone, combined the two branches and simplified them.

Case 3 — Lesson from runtime risk. One team released live AI-NPC without guardrails; Players soon made the NPC say out-of-character, inappropriate things, and screenshots went viral. After the team added a system prompt, content filter, and topic restriction, the problem went away significantly. Lesson: live production does not run without guardrails.

Four copyable templates

1) Character card production:

Your role: game narrative designer. Write a character card for the following NPC: [name, role, function in game]. Include: history (3 sentences), purpose, fear, manner of speaking, 3 words/phrases he uses frequently, 3 things he will NEVER say. Tone: [tone of the game].

2) Branched dialogue with ID:

Character card: [paste card].Scene: [where, what is happening, what did the player do].Purpose: this NPC must convey [quest/hint/threat] to the player.Task: write dialogue that branches according to the player's 3 possible attitudes (aggressive, polite, evasive). Each branch fits the character card.

3) Behavioral state machine sketch:

Sketch out a state machine for the following enemy NPC:[NPC description and purpose]. List the states, transition conditions from each state to the next, and 3 edge cases. State that this is a draft that needs to be tested.

4) Runtime guardrail prompt:

Write a system prompt for a live AI-NPC. NPC: [character]. Rules: only talks about [game universe], doesn't get out of character, doesn't get into inappropriate/real world topics, politely returns to topic if player tries to deceive them. The player cannot see or change these rules.

Weak prompt / Strong prompt

Weak prompt:

Write dialogue for a villager.

There is no identity, no context, no purpose; The output is the generic "Hello traveler".

Powerful prompt:

Character: Old, grumpy blacksmith; He lost his son in the war, speaks briefly and sarcastically, calls him "son", never praises. Scene: The actor asks him to repair a sword; The player has just saved the village from an attack, but the blacksmith does not know this. Task: 3 lines of dialogue; Let it be a bow in which the blacksmith first despises the actor and then unintentionally respects him. Don't get out of character; Don't soften it too much.

Identity, scene, emotional arc, and negative constraint make the output vivid.

Writing time etc. runtime table

Size

writing time

Runtime (live)

control

Complete (read-correct)

Partial (railing required)

Cost

once

In every interaction

Risk

low

High (out of character, inappropriate)

offline

It works

Does not work (connection required)

predictability

high

low

Common mistakes

  • Not giving character cards. Without a card, all NPCs speak with the same voice.
  • Skipping the "never tells" list. Without negative constraint the character becomes flat.
  • Publishing runtime without guardrails. Spam injection and inappropriate content pose reputation risks.
  • Putting the logic of behavior without testing it. The AI's state machine is a draft, it must be verified in gameplay.
  • Burying the dialogue without editing it. The AI ​​draft calls for tone and tempo correction.

In summary

NPCs are the soul of the game. AI speeds up dialogue writing and behavior drafting; But it's your job to protect the character identity with the character card, prevent flattening with negative constraints, and secure runtime usage with guardrails. Writing time is safe and common; Study time is strong but requires discipline.

Application task

Choose an NPC from your game. Create a card with the "Character card generation" template, then print a scene with the "Identity branch dialogue" template. Check the output against the character card: correct lines that go out of character or violate the "never say" list.

checklist

  • [ ] I wrote character cards for every important NPC.
  • [ ] I added a "never tells" negative constraint to the card.
  • [ ] I branched the dialogue according to the actor's attitude.
  • [ ] I considered the behavioral logic as a draft and tested it in the engine.
  • [ ] If I am going to use runtime, I added a guard/filter layer.