Tutorial: Meadow Quest (2D RPG)

6. Coins & the gate

Coins that count themselves into a global variable, and a gate that opens on the third one.

The coin

Each coin checks how far away the hero is. Within 0.8 units it adds 1 to a global variable called coins (shared by every script), then moves itself far off-screen.

OnUpdate ─▶ Branch( Vec2Distance( hero position , my position ) < 0.8 )
               then ─▶ SetGlobal coins = GetGlobal coins + 1
                    ─▶ SetPosition2D( 200 , 200 )
  1. Script Editor > + New, then MeadowCoin. Keep OnUpdate and delete the rest.
  2. Build the graph above with these nodes: FindEntity (entity = Hero), GetEntityPosition2D, GetPosition2D, Vec2Distance, ConstNumber (0.8), Less, Branch, GetGlobal (global = coins), ConstNumber (1), AddNumber, SetGlobal (global = coins), ConstVec2 (200, 200), SetPosition2D.
    • Exec wires: OnUpdate → Branch, then Branch then → SetGlobal → SetPosition2D.
  3. Compile, Save.

The MeadowCoin graph: distance check, Branch, then add 1 to coins and move away

Or paste the text:

tgscript
//!tgscript v1
//! script name=MeadowCoin
//! region graph
//! node 1 OnUpdate pos=(60,80)
export function onUpdate(ctx) {
    //! node 2 FindEntity pos=(260,80) entity=Hero
    const n2_value = ctx.findEntity("Hero");
    //! node 3 GetEntityPosition2D pos=(460,80)
    const n3_value = ctx.getEntityPosition2D(n2_value);
    //! node 4 GetPosition2D pos=(460,200)
    const n4_value = ctx.getPosition2D();
    //! node 5 Vec2Distance pos=(660,120)
    const n5_out = ctx.vec2Distance(n3_value, n4_value);
    //! node 6 ConstNumber pos=(660,240)
    const n6_value = 0.80;
    //! node 7 Less pos=(860,120)
    const n7_out = (n5_out < n6_value);
    //! node 8 Branch pos=(1060,80)
    if (n7_out) {
        //! node 9 GetGlobal pos=(1060,240) global=coins
        const n9_value = ctx.getGlobal("coins");
        //! node 10 ConstNumber pos=(1060,340)
        const n10_value = 1.00;
        //! node 11 AddNumber pos=(1260,280)
        const n11_out = (n9_value + n10_value);
        //! node 12 SetGlobal pos=(1460,240) global=coins
        ctx.setGlobal("coins", n11_out);
        //! node 13 ConstVec2 pos=(1260,80)
        const n13_value = ctx.vec2(200.00, 200.00);
        //! node 14 SetPosition2D pos=(1460,80)
        ctx.setPosition2D(n13_value);
    }
}
//! endregion

Now place three coins, south of the fence: 2D Sprites named Coin1–Coin3 with image mq-coin, scale 1×1, Z-Order 8, at (0, −5), (−6, −3) and (6, −6), each with MeadowCoin.tgscript in its Script slot. One script serves all three.

The gate

The gate is a wall that moves out of the way once coins reaches 3.

  1. A 2D Sprite named Gate: image mq-gate, Position (0, 2), Scale (3, 1), Z-Order 5, plus Add Component > Collider 2D (Fit to sprite: half size 1.5 × 0.5). It fills the 3-tile gap in the fence. The image is 48×16, so scale 3×1 keeps it at 16 px per unit.
  2. Script Gate:
tgscript
//!tgscript v1
//! script name=Gate
//! region graph
//! node 1 OnUpdate pos=(60,80)
export function onUpdate(ctx) {
    //! node 2 GetGlobal pos=(260,120) global=coins
    const n2_value = ctx.getGlobal("coins");
    //! node 3 ConstNumber pos=(260,240)
    const n3_value = 3.00;
    //! node 4 GreaterEqual pos=(460,120)
    const n4_out = (n2_value >= n3_value);
    //! node 5 Branch pos=(660,80)
    if (n4_out) {
        //! node 6 ConstVec2 pos=(660,240)
        const n6_value = ctx.vec2(200.00, 210.00);
        //! node 7 SetPosition2D pos=(860,80)
        ctx.setPosition2D(n6_value);
    }
}
//! endregion

The Gate graph

Attach it to Gate.

Play-test

  • Walk north before collecting the coins: the closed gate blocks you, just like the fence.
  • Collect all three: on the third, the gate disappears, and its collider goes with it.
  • Stop resets everything, including the coins counter.

The gate gone after the third coin

Good practices

  • One script, many objects. All three coins share MeadowCoin.tgscript: fix it once, every coin gets the fix. Each coin acts on itself, because GetPosition2D and SetPosition2D without an entity mean "me".
  • Globals for game state. coins and later won are shared by every script and reset when you press Stop. Use clear names; a typo makes a second, empty variable.
  • Moving far away is today's way to remove something (spawn and destroy are coming). Pick a spot no camera can reach, like (200, 200).
  • Distance checks are fine to start with. Part 12 shows the better tool for pickups: a trigger.

This part's assets

MeadowCoin.tgscript · Gate.tgscript · mq-coin.png · mq-gate.png (each image with its .mt). The two scripts are the finished versions from Part 12, with triggers and sounds.

Next: pet, chest & victory →