ExamplesIntermediate

Items that remember

Adapt examples for Item charges, durability, changing images, personal Boop streaks, and Tamagotchi-style needs.

Item with limited charges

Make the Item non-stackable and add named images amulet_active and amulet_empty. The remembered variable and action form one Code module:

mem var charges = 5;
chip chargeStatus {
  config highlighted => true;
  format("Charges: {charges}")
}

action useAmulet -> "Use amulet" {
  config {
    participants: 1,
    consumes: 0,
  }

  let empty = charges <= 0;

  if empty {
    Item::setImage(&amulet_empty);
    show chip "No charges left";
  } else {
    charges--;
    if charges == 0 {
      Item::setImage(&amulet_empty);
    } else {
      Item::setImage(&amulet_active);
    }
    show variables charges;
  }

  text if empty {
    "The amulet is silent."
  } else {
    "The amulet flashes for @1."
  }
}

Each Item instance remembers its own number of charges.

Recharge action

Add the second action below useAmulet in the same Code module:

action recharge {
  config {
    participants: 1,
    consumes: 0,
  }

  charges = clamp(charges + 2, 0, 5);
  Item::setImage(&amulet_active);
  show variables charges;

  text "@1 restores the amulet's power.";
}

Durability with a calculated status

mem var durability = 100;
const condition = if durability > 70 then "PRISTINE" else if durability > 30 then "WORN" else "CRITICAL";

action inspectCondition -> "Inspect condition" {
  durability = clamp(durability - Rand::pick(3..=8), 0, 100);
  show variables durability, condition;

  text "{HOST.name} is now {}." with lower(condition);
}

condition is a constant, so Sugar recalculates it after durability changes.

Personal Boop streak

mem var streak = 0;

text {
  streak++;
  show chip format("Streak: {streak}");
  "@1 completed another round!"
}

Every JOI using the Boop receives a separate remembered streak.

Tamagotchi-style Item

A Tamagotchi-style creation combines several Memory variables, conditional actions, changing media, status embeds, and optionally Host Automations.

Build the complete Tamagotchi-style Item