← All Workshops

MudEngine Part 8: Inventory & Chat

Step 12 / 14

Click to take, click to drop

Typing take loaf of bread works, but a MUD should feel immediate: click the item on the ground to pick it up, and click an inventory item to drop it back.

Room items live in Game's RSX, so their onclick just needs the same send the command input uses. The inventory is a separate component, so it gets a Callback prop — the same pattern DirectionPad.onmove already uses — and Game wires it up to ClientMessage::Drop.

mud-engine/src/components/game.rs
if !rd.items.is_empty() {
    div { class: "room-items",
        h3 { "📦 Items here" }
        for item in rd.items.clone() {
            div {
                class: "room-item",
                onclick: move |_| send(ClientMessage::Take { item: item.clone() }),
                "{item}",
            }
        }
    }
}
mud-engine/src/components/game.rs
#[component]
fn InventoryPanel(
    players: ReadOnlySignal<HashMap<String, PlayerInfo>>,
    my_id: ReadOnlySignal<String>,
    ondrop: Callback<String>,
) -> Element {
    let my_info = {
        let p = players.read();
        let id = my_id.read();
        id.as_ref().and_then(|id| p.get(id).cloned())
    };

    rsx! {
        div { class: "inventory-panel",
            h3 { class: "inv-title", "🎒 Inventory" }
            if let Some(ref info) = my_info {
                if info.items.is_empty() {
                    div { class: "inv-empty", "Empty" }
                } else {
                    for item in info.items.clone() {
                        div {
                            key: "{item}",
                            class: "inv-item",
                            onclick: move |_| ondrop.call(item.clone()),
                            "{item}",
                        }
                    }
                }
            }
        }
    }
}
mud-engine/src/components/game.rs
// Inventory panel
InventoryPanel {
    players: players.into(),
    my_id: my_id.into(),
    ondrop: move |item: String| send(ClientMessage::Drop { item }),
}
💡 Callback props keep components simple

InventoryPanel never touches the socket or the message types. The parent (Game) hands it an ondrop: Callback<String> — just a callable — and the panel decides when to call it (on a click). The server stays the source of truth: it validates the request, moves the item, and broadcasts the new state back to every client, which is why both lists update.

One Rust detail worth knowing: onclick closures are 'static, so they can only capture owned values. That's why both loops iterate rd.items.clone() / info.items.clone() instead of &rd.items — a move closure grabbing a &String that points into a temporary won't live long enough. The per-click clone is cheap insurance; a MUD's item lists stay tiny.

🧪 Verify it
  1. In Town Square, click loaf of bread under "📦 Items here" — it disappears from the room list and appears in Inventory.
  2. Click the bread in Inventory — it lands back on the ground with a "⚡ drops" system message.
  3. Check that take bread / drop bread typed in the command input still work too.
Step 12 / 14