Skip to content

Add use_item / can_use_item; item effects system#13

Merged
narayan24 merged 1 commit into
masterfrom
feature/use-item
May 14, 2026
Merged

Add use_item / can_use_item; item effects system#13
narayan24 merged 1 commit into
masterfrom
feature/use-item

Conversation

@narayan24
Copy link
Copy Markdown
Owner

Summary

Consumable items (food, drinks) previously had no actual mechanic — they sat in inventory with a description saying "eat to restore hunger" but nothing happened. This PR makes them real.

items.json additions (story-side)

{ "id": "groceries", "type": "food", "effects": { "hunger": 35, "mood": 3 } }
{ "id": "energy_drink", "type": "food", "effects": { "energy": 25, "mood": -3 } }

New script functions

{{ use_item "groceries" }}

Consumes 1 unit, applies all effects to mc via the existing AttrChange pipeline, returns an HTML stat summary (e.g. +35 Hunger | +3 Mood). Returns a red error string if not owned or no effects defined.

{{ if can_use_item "groceries" }}[[Eat groceries, ...]]{{ end }}

Guard: true only when player owns ≥1 of the item and effects are defined. Use to show/hide the "use" link.

Implementation details

  • ItemDefinition.Effects: Dictionary<string,int> — parsed from JSON effects field
  • MergeItemsFromJson: iterates item.effects JObject properties
  • UseItem: delegates to AttrChange("mc", ...) for consistency with the existing attr pipeline; builds coloured HTML summary
  • CanUseItem: lightweight check, no side effects

Test plan

  • Buying groceries at supermarket adds groceries item to inventory
  • {{ if can_use_item "groceries" }} shows link only when owned
  • {{ use_item "groceries" }} reduces count by 1, raises Hunger by 35, shows summary
  • {{ use_item "groceries" }} when not owned returns error string (no crash)
  • energy_drink raises Energy +25, lowers Mood -3

🤖 Generated with Claude Code

Items can now define an effects dict in items.json:

  { "id": "groceries", "type": "food", "effects": { "hunger": 35, "mood": 3 } }

New script functions (snake_case via Scriban auto-rename):

  {{ use_item "groceries" }}
    Consumes 1 unit, applies all effects to mc, returns HTML stat summary.
    Returns an error string if not owned or no effects defined.

  {{ if can_use_item "groceries" }}
    Guard: true only when player owns >=1 and effects are defined.

Implementation:
- ItemDefinition.Effects: Dictionary<string,int> parsed from JSON
- MergeItemsFromJson: parses effects field via dynamic JObject iteration
- UseItem: delegates attr changes to existing AttrChange() for consistency;
  builds a coloured HTML summary of applied changes
- CanUseItem: lightweight ownership + effects-defined check

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@narayan24 narayan24 merged commit 230ff32 into master May 14, 2026
@narayan24 narayan24 deleted the feature/use-item branch May 14, 2026 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant