Tutorial: Meadow Quest (2D RPG)
11. Inventory & potions
A five-slot item bar on screen that shows what the hero carries, and a potion to pick up and drink.

How it works
The inventory is the game state you already have, shown on screen. Each slot's icon is a
sprite that the Inventory script places on the bar when the matching global says you own
it, and parks otherwise:
| Slot | Shows when |
|---|---|
| 1: sword | hasSword = 1 |
| 2–4: coins | coins ≥ 1, 2, 3 |
| 5: potion | potions ≥ 1 |
How to build the bar
- A 2D Sprite
Inventory, imagemq-invbar, Scale5,1(80×16 pixels: five tiles), Z-Order70, parked at (200, 280). - Icon sprites, Z-Order
71, parked at (200, 280):SwordIcon(imagemq-sword),CoinIcon1,CoinIcon2,CoinIcon3(imagemq-coin), andPotionIcon(imagemq-potion). - Script
Inventoryon theInventorysprite:
tgscript
//!tgscript v1
//! script name=Inventory
//! region graph
//! node 1 OnUpdate pos=(60,80)
export function onUpdate(ctx) {
//! node 2 FindEntity pos=(60,240) entity=Camera
const n2_value = ctx.findEntity("Camera");
//! node 3 GetEntityPosition2D pos=(260,240)
const n3_value = ctx.getEntityPosition2D(n2_value);
//! node 4 GetGlobal pos=(60,340) global=started
const n4_value = ctx.getGlobal("started");
//! node 5 ConstNumber pos=(60,440)
const n5_value = 0.50;
//! node 6 Greater pos=(260,340)
const n6_out = (n4_value > n5_value);
//! node 7 ConstVec2 pos=(260,440)
const n7_value = ctx.vec2(200.00, 280.00);
//! node 8 ConstVec2 pos=(260,540)
const n8_value = ctx.vec2(0.00, -3.55);
//! node 9 AddVec2 pos=(460,500)
const n9_out = ctx.addVec2(n3_value, n8_value);
//! node 10 Branch pos=(660,80)
if (n6_out) {
//! node 11 SetPosition2D pos=(860,80)
ctx.setPosition2D(n9_out);
} else {
//! node 12 SetPosition2D pos=(860,240)
ctx.setPosition2D(n7_value);
}
//! node 21 FindEntity pos=(60,700) entity=SwordIcon
const n21_value = ctx.findEntity("SwordIcon");
//! node 22 GetGlobal pos=(60,800) global=hasSword
const n22_value = ctx.getGlobal("hasSword");
//! node 23 ConstNumber pos=(60,900)
const n23_value = 0.50;
//! node 24 Greater pos=(260,800)
const n24_out = (n22_value > n23_value);
//! node 25 And pos=(460,800)
const n25_out = (n24_out && n6_out);
//! node 26 ConstVec2 pos=(260,1000)
const n26_value = ctx.vec2(-2.00, -3.55);
//! node 27 AddVec2 pos=(460,1000)
const n27_out = ctx.addVec2(n3_value, n26_value);
//! node 28 Branch pos=(1060,80)
if (n25_out) {
//! node 29 SetEntityPosition2D pos=(1260,80)
ctx.setEntityPosition2D(n21_value, n27_out);
} else {
//! node 30 SetEntityPosition2D pos=(1260,240)
ctx.setEntityPosition2D(n21_value, n7_value);
}
//! node 31 FindEntity pos=(60,1120) entity=CoinIcon1
const n31_value = ctx.findEntity("CoinIcon1");
//! node 32 GetGlobal pos=(60,1220) global=coins
const n32_value = ctx.getGlobal("coins");
//! node 33 ConstNumber pos=(60,1320)
const n33_value = 0.50;
//! node 34 Greater pos=(260,1220)
const n34_out = (n32_value > n33_value);
//! node 35 And pos=(460,1220)
const n35_out = (n34_out && n6_out);
//! node 36 ConstVec2 pos=(260,1420)
const n36_value = ctx.vec2(-1.00, -3.55);
//! node 37 AddVec2 pos=(460,1420)
const n37_out = ctx.addVec2(n3_value, n36_value);
//! node 38 Branch pos=(1560,80)
if (n35_out) {
//! node 39 SetEntityPosition2D pos=(1760,80)
ctx.setEntityPosition2D(n31_value, n37_out);
} else {
//! node 40 SetEntityPosition2D pos=(1760,240)
ctx.setEntityPosition2D(n31_value, n7_value);
}
//! node 41 FindEntity pos=(60,1540) entity=CoinIcon2
const n41_value = ctx.findEntity("CoinIcon2");
//! node 42 GetGlobal pos=(60,1640) global=coins
const n42_value = ctx.getGlobal("coins");
//! node 43 ConstNumber pos=(60,1740)
const n43_value = 1.50;
//! node 44 Greater pos=(260,1640)
const n44_out = (n42_value > n43_value);
//! node 45 And pos=(460,1640)
const n45_out = (n44_out && n6_out);
//! node 46 ConstVec2 pos=(260,1840)
const n46_value = ctx.vec2(0.00, -3.55);
//! node 47 AddVec2 pos=(460,1840)
const n47_out = ctx.addVec2(n3_value, n46_value);
//! node 48 Branch pos=(2060,80)
if (n45_out) {
//! node 49 SetEntityPosition2D pos=(2260,80)
ctx.setEntityPosition2D(n41_value, n47_out);
} else {
//! node 50 SetEntityPosition2D pos=(2260,240)
ctx.setEntityPosition2D(n41_value, n7_value);
}
//! node 51 FindEntity pos=(60,1960) entity=CoinIcon3
const n51_value = ctx.findEntity("CoinIcon3");
//! node 52 GetGlobal pos=(60,2060) global=coins
const n52_value = ctx.getGlobal("coins");
//! node 53 ConstNumber pos=(60,2160)
const n53_value = 2.50;
//! node 54 Greater pos=(260,2060)
const n54_out = (n52_value > n53_value);
//! node 55 And pos=(460,2060)
const n55_out = (n54_out && n6_out);
//! node 56 ConstVec2 pos=(260,2260)
const n56_value = ctx.vec2(1.00, -3.55);
//! node 57 AddVec2 pos=(460,2260)
const n57_out = ctx.addVec2(n3_value, n56_value);
//! node 58 Branch pos=(2560,80)
if (n55_out) {
//! node 59 SetEntityPosition2D pos=(2760,80)
ctx.setEntityPosition2D(n51_value, n57_out);
} else {
//! node 60 SetEntityPosition2D pos=(2760,240)
ctx.setEntityPosition2D(n51_value, n7_value);
}
//! node 61 FindEntity pos=(60,2380) entity=PotionIcon
const n61_value = ctx.findEntity("PotionIcon");
//! node 62 GetGlobal pos=(60,2480) global=potions
const n62_value = ctx.getGlobal("potions");
//! node 63 ConstNumber pos=(60,2580)
const n63_value = 0.50;
//! node 64 Greater pos=(260,2480)
const n64_out = (n62_value > n63_value);
//! node 65 And pos=(460,2480)
const n65_out = (n64_out && n6_out);
//! node 66 ConstVec2 pos=(260,2680)
const n66_value = ctx.vec2(2.00, -3.55);
//! node 67 AddVec2 pos=(460,2680)
const n67_out = ctx.addVec2(n3_value, n66_value);
//! node 68 Branch pos=(3060,80)
if (n65_out) {
//! node 69 SetEntityPosition2D pos=(3260,80)
ctx.setEntityPosition2D(n61_value, n67_out);
} else {
//! node 70 SetEntityPosition2D pos=(3260,240)
ctx.setEntityPosition2D(n61_value, n7_value);
}
}
//! endregionHow to add a potion
- A 2D Sprite
Potion(imagemq-potion, Z-Order8) at (7, -1). - Script
Potion: a pickup like the coin, counting intopotions. (Part 12 turns it into a trigger with a sound, like the coins.)
tgscript
//!tgscript v1
//! script name=Potion
//! region graph
//! node 1 OnUpdate pos=(60,80)
export function onUpdate(ctx) {
//! node 2 FindEntity pos=(60,220) entity=Hero
const n2_value = ctx.findEntity("Hero");
//! node 3 GetEntityPosition2D pos=(260,220)
const n3_value = ctx.getEntityPosition2D(n2_value);
//! node 4 GetPosition2D pos=(260,340)
const n4_value = ctx.getPosition2D();
//! node 5 Vec2Distance pos=(460,260)
const n5_out = ctx.vec2Distance(n3_value, n4_value);
//! node 6 ConstNumber pos=(460,380)
const n6_value = 0.80;
//! node 7 Less pos=(660,260)
const n7_out = (n5_out < n6_value);
//! node 8 Branch pos=(860,80)
if (n7_out) {
//! node 9 GetGlobal pos=(860,240) global=potions
const n9_value = ctx.getGlobal("potions");
//! node 10 ConstNumber pos=(860,340)
const n10_value = 1.00;
//! node 11 AddNumber pos=(1060,280)
const n11_out = (n9_value + n10_value);
//! node 12 SetGlobal pos=(1260,80) global=potions
ctx.setGlobal("potions", n11_out);
//! node 13 ConstVec2 pos=(1260,240)
const n13_value = ctx.vec2(200.00, 270.00);
//! node 14 SetPosition2D pos=(1460,80)
ctx.setPosition2D(n13_value);
}
}
//! endregionDrinking is already in the Health script from Part 10: when
you hold Q, have a potion and fewer than three hearts, hp goes back to 3 and
potions goes down by one.
Good practices
- The inventory is data, the bar is a view. Items are numbers in globals; the bar only displays them. Saving, trading or a shop later only need to change the numbers.
- HUD follows the camera. Every slot is placed at "camera + offset", so it stays put on the screen however the camera moves.
- Keep the HUD inside the view. With a 9-unit-tall camera the edges are at ±4.5; the bar at -3.55 and the hearts at 3.7 stay clear of them.
- Show the HUD only while playing. Every slot is parked until
startedis set, so the title screen stays clean. - Grow it with the game. Five slots fit three kinds of item; add a slot when you add an item, and a global to count it.
This part's assets
Inventory.tgscript ·
Potion.tgscript ·
mq-invbar.png ·
mq-potion.png (each image with its .mt).