Apply System

The apply system handles the "Apply" button in inventories. This powers crafting (combining items into new items) and custom interactions (puzzle mechanics, item insertion, etc.).


How It Works

When a player clicks the Apply button:

StepWhat happens
1inventory_apply event fires
2If event returns false, stop
3Otherwise, run crafting logic

The Apply button appears when an inventory has either:

  • Recipes assigned to it
  • A custom interactive value set

The Apply Button

The interactive field on inventory templates controls the Apply button:

The value also becomes a CSS class on the button, allowing custom styling per inventory.

Styling the Apply button:

/* Base styling for "combine" button */
.apply-button.combine {
  background-color: #4a90d9;
}

.apply-button.combine:hover {
  background-color: #3a70b9;
}

/* Set button text using ::before */
.apply-button.combine::before {
  content: "Combine";
}

Recipes

Recipes define how items transform into other items.

Recipe Template Fields

FieldDescription
idRecipe identifier
nameDisplay name
descriptionRecipe description (supports rich text)
input_itemsRequired ingredients
output_itemsItems produced
tagsFor categorizing recipes

Input/Output Format

FieldDescription
item_idThe item template ID
quantityHow many (default: 1)

Learning Recipes

Recipes must be learned before they can be used.

ActionDescription
{learn_recipe: "recipe_id"}Learn a single recipe
{learn_recipe: "recipe1, recipe2"}Learn multiple recipes (comma-separated)

Example - Learning from a book:

FieldValue
TriggerPlayer uses "Blacksmith Manual" item
Action{learn_recipe: "iron_sword_recipe, steel_sword_recipe"}

Crafting Flow

When the player clicks Apply with a selected recipe:

StepWhat happens
1Check if recipe is learned
2Check if ingredients exist (unequipped items only)
3Check if inventory has space for outputs
4Remove input items
5Create output items

If any check fails, an appropriate notification is shown.


Recipe Selection UI

When an inventory has recipes:

UI ElementBehavior
Recipe listShows only learned recipes
Recipe buttonDisabled if ingredients missing
Click recipeTransfers needed ingredients from party inventory
Click ApplyCrafts using selected recipe

Clicking a recipe automatically moves the required ingredients from party inventory to the crafting inventory.


Stacking Rules

Crafting respects item stacking:

max_stack ValueBehavior
0 or 1Each item takes one slot
99Up to 99 items per slot
-1Unlimited stacking

The system calculates available space by considering:

  • Slots freed by consuming ingredients
  • Existing partial stacks that can be filled
  • Max stack values from item templates

Custom Apply Logic

Use the inventory_apply event for custom behavior:

game.on("inventory_apply", (inventory) => {
  if (inventory.id === "puzzle_altar") {
    // Check if correct items are placed
    const gem = inventory.getFirstItemById("red_gem");
    const key = inventory.getFirstItemById("ancient_key");

    if (gem && key) {
      // Consume items
      inventory.removeItem(gem);
      inventory.removeItem(key);

      // Store result and notify
      game.getStore("puzzle").set("altar_activated", true);
      game.showNotification("The altar glows with ancient power!");
    }

    // Prevent other calls, including default crafting(if the inventory has any recipes assigned to it)
    return false; 
  }
});

Assigning Recipes to Inventories

In the inventory template:

FieldDescription
recipesList of recipe IDs available at this inventory

Example - Blacksmith inventory:

FieldValue
idblacksmith_forge
name"Blacksmith's Forge"
recipesiron_sword_recipe, steel_sword_recipe, repair_armor

The Apply button defaults to "craft" when recipes are assigned.


Events

EventWhen it firesParameters
inventory_applyApply button clicked(inventory)
recipe_learnedRecipe is learned(recipeId)

Methods

MethodDescription
inventory.getApplyButton()Get the apply button label
inventory.addRecipe(id)Add recipe to inventory
inventory.getRecipes()Get all recipe IDs
inventory.craft()Execute crafting
game.addLearnedRecipe(id)Learn a recipe
game.getLearnedRecipes()Get all learned recipes

Quick Reference

I want to...Do this
Create a crafting stationSet recipes on inventory template
Learn a recipe{learn_recipe: "recipe_id"} action
Custom apply behaviorListen to inventory_apply event
Change button labelSet interactive on inventory template
Check if recipe learnedgame.getLearnedRecipes().has(id)

Next Steps