A talking NPC with a quest (top-down)
Needs a game with an interact input action and the topDownController behavior (the top-down template has both).
Four pieces of data: the NPC prefab, its brain, its dialogue and the quest. List the files in game.json
(brains, dialogues, quests) and the item in items.
The NPC walks around, stops to chat when talked to (talked:<name> is emitted with the entity’s name), and offers a
quest. Its Interactable opens the dialogue when the player presses interact nearby:
{ "schemaVersion": "0.1", "id": "baker", "components": { "Transform": {}, "Sprite": { "texture": "sprite.villager", "frame": 0, "z": 20 }, "SpriteAnimation": { "animation": "idle-down" }, "TopDownAnimator": {}, "Body": { "gravityScale": 0 }, "Collider": { "w": 10, "h": 6, "offsetX": 3, "offsetY": 10, "layer": "npc", "mask": ["world"] }, "Brain": { "brain": "baker" }, "Interactable": { "dialogue": "baker", "prompt": "@prompt.talk" } }}The brain (game/ai/baker.json): a state machine. States run an activity (idle, wander, moveTo, follow,
flee, wait, say, do) or a behaviour tree; transitions fire on an event (on), a condition (if), time
(after) or the activity finishing (done):
{ "schemaVersion": "0.1", "id": "baker", "speed": 30, "machine": { "initial": "wander", "states": { "wander": { "activity": { "act": "wander", "radius": 40, "pause": 2 }, "transitions": [{ "to": "chat", "on": "talked:baker" }] }, "chat": { "activity": { "act": "idle" }, "transitions": [{ "to": "wander", "after": 4 }] } } }}A shy animal is a behaviour tree: flee when the player is near, else wander.
{ "schemaVersion": "0.1", "id": "cat", "tree": { "type": "selector", "children": [ { "type": "sequence", "children": [ { "type": "condition", "if": { "near": "player", "within": 48 } }, { "type": "action", "action": { "act": "flee", "from": "player", "distance": 96 } } ] }, { "type": "action", "action": { "act": "wander", "radius": 40 } } ] }}The dialogue (game/dialogues/baker.json). branches jump elsewhere when a condition holds, so the same NPC says
the right thing at each stage of the quest; choices can have conditions and actions:
{ "schemaVersion": "0.1", "id": "baker", "start": "hi", "speakers": { "baker": { "name": "Baker" } }, "nodes": { "hi": { "speaker": "baker", "text": "Could you bring me some flour?", "branches": [ { "if": { "quest": "flour", "is": "done" }, "next": "thanks" }, { "if": { "hasItem": "flour" }, "next": "give" } ], "choices": [ { "text": "Sure.", "do": [{ "action": "startQuest", "quest": "flour" }] }, { "text": "Not now." } ] }, "give": { "speaker": "baker", "text": "Perfect, thank you!", "do": [ { "action": "takeItem", "item": "flour" }, { "action": "emit", "event": "gave:flour" } ] }, "thanks": { "speaker": "baker", "text": "Fresh bread soon!" } }}The quest (game/quests/flour.json): steps complete in order, on an event or when a condition holds. The quest
HUD widget shows the current step.
{ "schemaVersion": "0.1", "id": "flour", "title": "Flour for the baker", "steps": [ { "id": "find", "text": "Find a bag of flour", "when": { "if": { "hasItem": "flour" } } }, { "id": "bring", "text": "Bring it to the baker", "when": { "event": "gave:flour" } } ], "rewards": [{ "action": "giveItem", "item": "bread" }]}The flour itself is a prefab with "Pickup": { "kind": "flour", "item": "flour" }. Check everything with validate,
then run_headless.