Tutorial: Meadow Quest (2D RPG)
9. A farmer who talks
An NPC with speech bubbles that change with the story, and a gift the player gets by pressing Space.

The old farmer stands near the start. Walk up to him and a speech bubble appears. Press Space and he gives you his sword; after that his bubble says so. Walk away and the bubble disappears.
How it works
The bubbles are two sprites (text painted into the image) that the farmer's script moves
next to him or parks out of sight. Which one shows depends on a global, hasSword:
near = distance(hero, me) < 1.8
near and no sword: show "HELLO" bubble; if Space pressed: hasSword = 1
near and has sword: show "TAKE MY SWORD" bubble
otherwise: park both bubblesHow to build it
- A 2D Sprite
Farmer(imagemq-farmer, Z-Order9) at (-2.5, -8.5). - Two 2D Sprites
BubbleHelloandBubbleSword(imagesmq-bubble-helloandmq-bubble-sword), Scale6,2.5(the images are 96×40 pixels), Z-Order80, parked at (200, 240). - Script Editor > + New,
Farmer, paste this, Compile, Save, and put it in the Farmer's Script slot:
tgscript
//!tgscript v1
//! script name=Farmer
//! region graph
//! node 1 OnUpdate pos=(60,80)
export function onUpdate(ctx) {
//! node 2 FindEntity pos=(60,240) entity=Hero
const n2_value = ctx.findEntity("Hero");
//! node 3 GetEntityPosition2D pos=(260,240)
const n3_value = ctx.getEntityPosition2D(n2_value);
//! node 4 GetPosition2D pos=(260,360)
const n4_value = ctx.getPosition2D();
//! node 5 Vec2Distance pos=(460,300)
const n5_out = ctx.vec2Distance(n3_value, n4_value);
//! node 6 ConstNumber pos=(460,420)
const n6_value = 1.80;
//! node 7 Less pos=(660,300)
const n7_out = (n5_out < n6_value);
//! node 8 GetGlobal pos=(460,540) global=hasSword
const n8_value = ctx.getGlobal("hasSword");
//! node 9 ConstNumber pos=(460,640)
const n9_value = 0.50;
//! node 10 Less pos=(660,540)
const n10_out = (n8_value < n9_value);
//! node 11 And pos=(860,360)
const n11_out = (n7_out && n10_out);
//! node 12 Not pos=(860,540)
const n12_out = (!n10_out);
//! node 13 And pos=(1060,480)
const n13_out = (n7_out && n12_out);
//! node 14 FindEntity pos=(860,700) entity=BubbleHello
const n14_value = ctx.findEntity("BubbleHello");
//! node 15 FindEntity pos=(860,800) entity=BubbleSword
const n15_value = ctx.findEntity("BubbleSword");
//! node 16 ConstVec2 pos=(460,760)
const n16_value = ctx.vec2(2.20, 1.90);
//! node 17 AddVec2 pos=(660,700)
const n17_out = ctx.addVec2(n4_value, n16_value);
//! node 18 ConstVec2 pos=(660,860)
const n18_value = ctx.vec2(200.00, 240.00);
//! node 19 Branch pos=(1260,80)
if (n11_out) {
//! node 20 SetEntityPosition2D pos=(1460,80)
ctx.setEntityPosition2D(n14_value, n17_out);
//! node 21 ActionPressed pos=(1460,240) action=Jump
const n21_value = ctx.actionPressed("Jump");
//! node 22 Branch pos=(1660,80)
if (n21_value) {
//! node 23 ConstNumber pos=(1660,240)
const n23_value = 1.00;
//! node 24 SetGlobal pos=(1860,80) global=hasSword
ctx.setGlobal("hasSword", n23_value);
}
} else {
//! node 25 SetEntityPosition2D pos=(1460,320)
ctx.setEntityPosition2D(n14_value, n18_value);
}
//! node 26 Branch pos=(2060,80)
if (n13_out) {
//! node 27 SetEntityPosition2D pos=(2260,80)
ctx.setEntityPosition2D(n15_value, n17_out);
} else {
//! node 28 SetEntityPosition2D pos=(2260,240)
ctx.setEntityPosition2D(n15_value, n18_value);
}
}
//! endregionPlay, press Space to start, and walk left to the farmer.
Good practices
- State lives in globals; pictures follow the state. The farmer doesn't remember what he
said: he looks at
hasSwordevery frame and shows the bubble that matches. Nothing can get out of step. - Every branch has an
else. Each bubble is either placed or parked on every frame, so it can never be left floating where it was. - Offsets point the tail at the speaker. The bubble's tail is near its left edge, so the bubble sits 2.2 units to the right of the farmer.
- Text in sprites can't be translated. For a game in several languages, use UI Text with
localized keys instead; a script's SetText can show a key such as
@dlg.hello(Part 13 uses SetText for the quest line).
This part's assets
Farmer.tgscript ·
mq-farmer.png ·
mq-bubble-hello.png ·
mq-bubble-sword.png (each image with its .mt).