Writing NPCs and tools

An NPC is a persona (an FNpcPersona struct, or an NPC Persona asset that holds one) passed to Run NPC Turn with the player's line. The model reads the persona and the line, then replies in words, calls one of the persona's tools, or both. Your game decides what a call does.

The persona

Create an asset with Content Browser → Add → NpcAct → NPC Persona, or fill the struct in a Blueprint. The plugin's three examples (PA_Vendor, PA_DoorKeeper, PA_QuestGiver, in NpcAct Content → Examples → Personas) are a good start.

Field What it does
System Prompt Who the NPC is, what it knows and what it may do. The model reads it on every turn.
Tools What the NPC can ask the game to do. Each tool has a Name, a Description and Params.
Max Reply Tokens The longest reply, 16–1,024 (default 128). With speculative decoding on, a reply can run 2–3 tokens over (Performance).
Output Guard and its words Withholds a reply that names the AI behind the NPC (Keeping the AI out of sight). On by default.
Rescue Narrated Calls Makes the call when the NPC says it acts but doesn't call (below). On by default.

A tool parameter has a Name, a Type (String, Integer or Boolean), a Description, Allowed Values (the only strings it may take), an optional range (Has Range, Min, Max) and Required. The model is told a range as "(from Min to Max)" at the end of the description, because the model's chat template drops a schema's minimum and maximum. A call that breaks the schema (an unknown tool, a missing required argument, a value not allowed, a number out of range) is rejected: it never reaches On Tool Call, and the turn's Stats count it in Rejected Calls.

Write tools the model will call

The model's usual failure is not a broken call. It is saying the action ("Here's your potion.") without calling the tool. In the plugin's test turns on the developer's Mac (2026-11-22), 0 calls were malformed, and 6–16 of 44 turns made no call where one was expected (Performance). These rules reduce it:

  • Put every figure in the persona. Prices, stock, door names, quest names: write them into the System Prompt, and into Allowed Values where you can. The model can't look anything up, and a figure it doesn't have, it invents. The example project's trader adds its price list from the game's own price table to the prompt on every turn, so the prompt and the game's check read the same numbers.
  • Give the schema a way to say what the player will ask for. If a player can ask for two of something, the tool needs a quantity. With a sell_item(item, price_gold) tool and no quantity, "Sell me two repair kits." got "Here are your two repair kits for 16 gold." and no call at all (CPU, 2026-11-22). Let the model name the item and the count, and let your game do the arithmetic.
  • Say when to call. One plain rule per tool in the System Prompt: "When the player agrees to take a quest, start it with start_quest. Start a quest only then." Recorded in the example project (developer's Mac GPU, 2026-11-22, one run per line):
    • The door keeper opened the forge gate when asked "What is behind the forge gate?". With "You open the forge_gate and the back_room for anyone who asks you to open them. Answer questions about the doors in words." added, the same question got an answer in words.
    • The innkeeper, asked "I will find your lost ring.", asked back whether the player wanted the quest. With the rule above and the line "Yes, I'll take the lost ring quest.", it called start_quest.
    • The trader named the price of an iron sword and sold nothing, both before and after a rule for when to sell and a clearer player line ("I'll buy one iron sword, please."). After the change, the rescue below made the sale from the player's words.
  • Name tools with a plain verb first (give_item, open_door, sell_item, move_to), so the rescue below can recognise them.
  • Keep Descriptions short and literal. They are part of what the model reads.

Re-test after every edit

Any change to the text the model reads moves some turns from correct to no-call and others the other way. A change in how a number's range was described moved 4 of 25 test turns on the GPU and 3 on the CPU, in both directions (developer's Mac, 2026-11-22). Keep a list of player lines with the call each should make, run it after every persona or tool edit, and compare which call each line made, not the exact words.

The rescue: when the NPC says it but doesn't call

With Rescue Narrated Calls on (the default), when a reply says the NPC acts ("Here you go", "I'll open the gate") and no tool was called, the plugin makes that one call from the player's own words. It only does this when:

  • the tool's family is known from the first word of the tool name (up to _, -, a space or a capital letter, so give_item and GiveItem both start with "give"):

    Family First words
    give give, hand, gift, grant, bring
    sell sell, trade, vend
    open open, unlock
    move move, walk, go, goto, travel, follow, lead, meet
  • and the item, door or place is named in the player's line.

A rescued call is checked like any other and arrives on On Tool Call; the turn's Stats have Rescued set. A tool whose name starts with any other word (start_quest, heal_player) is never rescued. The rescue never guesses a count: in the repair-kit example above, the quantity was only in words, so no call was made.

Check every call in your game

The plugin checks a call against the tool's schema. It doesn't know your game's rules. In the test turns the model opened a door its prompt said must stay shut, on both backends. So before a call changes anything:

  • Check it against the game's state: is the item on the price list, is the price the list's price, does the player have the gold, is the door one this NPC may open.
  • Make it safe to repeat. A second identical call, in the same turn or a later one, should do nothing: a door already open stays open and nothing is charged twice.
  • Say what really happened. If the game refuses a call, replace the NPC's words with a line of yours, so the NPC never says it sold something the game didn't sell. If the reply had no words, show what the game did.

The example project does all three in ANpcDemoGameMode::ExecuteCall.

Fallback lines

Write a line of your own for each case where the model can't answer, and show it instead of a reply:

When Why
The model file isn't on the device Is Model Present is false, or Load NPC Model failed with Model Missing or Model Invalid
The model is still loading the player talks before Load NPC Model's On Done
The game refused the call your check said no
The call was already done the door is already open
The turn failed On Failed with any code; Failure's Message is neutral but generic
The guard withheld the reply On Failed with Out Of Character

Clear what you showed on On Failed, before you show the fallback line. Text reaches On Text a word at a time as it is checked, so part of a reply may be on screen when the guard withholds the rest. In the example project, an innkeeper asked which model was answering had already streamed "I am a large language model, trained by " when the guard withheld the next word.

On iOS and Android the plugin frees the model when the app goes to the background (Drop Model In Background on the NpcAct subsystem, on by default), because a backgrounded app that holds it is among the first the system closes. Drop NPC Model does the same on request, for your own low-memory handling. Either way On Model Dropped fires when the memory is free, and the next turn fails with Not Loaded until you load the model again.

Measured on the Galaxy S26+ (2026-11-22), with a permission dialog and with Home, each with the model idle and during a turn: the running turn ended with Cancelled within 66 ms, the model's memory was freed while the game was in the background, and the next turn failed with Not Loaded. Nothing reloaded until the test loaded it. On Model Dropped fires when the game comes back, not when it leaves: the engine pauses the game thread in the background, and the event fired about 30 ms after the return. Reload the model from On Model Dropped, or when the player next needs an NPC, behind a loading screen.

History

Run NPC Turn's History pin (under the node's advanced pins) takes earlier lines of the conversation, oldest first. The model reads every line again on every turn, so a long history makes every turn slower to start. Keep only the lines the NPC needs.

On the Galaxy S26+ (2026-11-22), with lines of about 20 tokens each, every History line added about 57 ms before the first reply text on the CPU backend, and about 14 ms on GPU (Performance).

History can change whether the NPC calls a tool. In the same test, the player's line "I'll buy an iron sword." made the shop's sell call in every turn with no History. After 8 or 16 lines of unrelated small talk, no turn made it on either backend: the NPC named the price and asked for the coin instead. Test each NPC with the History your game really sends (re-test).

Cancel

A turn, Generate Text or Generate Struct can be stopped from C++ with Cancel() on its action object (UNpcTurnAsync, UNpcGenerateTextAsync, UNpcGenerateStructAsync). On Failed then fires once, right away, with Cancelled, and no other pin of that call fires after it. In version 1.0.0 the Blueprint nodes have no pin for the action object, so a Blueprint can't cancel a turn yet. Drop NPC Model stops the running turn (Cancelled) and fails the queued ones (Not Loaded).

Generate Text and Generate Struct

These nodes run the model without a persona, for text that isn't an NPC's reply.

  • Generate Text takes a System Prompt and a Prompt (and Max Tokens) and streams the reply.
  • Generate Struct fills a struct of your choosing: its fields become a JSON shape the reply is held to. Field names are the model's only description of each field, so name them plainly (RewardGold, not Rg). A reply that doesn't fit fails with Invalid Reply and is never repaired.

Both are covered by the output guard through a project setting, Guard Generate Text And Generate Struct (on by default), with its own extra words. They use the built-in stack names only, with no out-of-character list, because they have no character. A hit fails with Out Of Character and the withheld text reaches no pin.

Generate Struct uses the project's Speculative Decoding setting. On a Mac GPU this changes the JSON the model writes: 9 of 10 test replies were valid with Adaptive, against 10 of 10 with Off (Performance). If your game relies on Generate Struct on Mac, set Speculative Decoding to Off.