Crafter

Scripting with the Crafter class.

The Crafter class is used to craft an item. This class can craft any item as long as the correct item combinations are provided.

Fields & Properties

Name
Description
Type
Access

combineItems

A list of items to use for crafting. A single stack of each item will be used.

List<ItemStack>

public

Creating a Crafter

Crafter myCrafter = new Crafter();

Adding Items

To add items, use the TryAddItem method. This method will return true if an ItemStack was successfully added. It will only add a single stack on an ItemStack. It may fail if the ItemStack is already added.

bool result = myCrafter.TryAddItem(myItemStack);

Removing Items

You can use the RemoveItem method to remove an item.

myCrafter.RemoveItem(myItemStack);

It's also possible to remove an item at a specific index.

// Remove item at inedx 0
myCrafter.RemoveItem(0);

Crafting

To craft, use the TryCraft method. The Crafter looks at all Craft scriptable objects to find one that matches the specific ItemStack combination (the combineItems field is the combination). If one matches, the resultItem of that Craft will be crafted (added to the inventory) and true will be returned. Otherwise, false will be returned and no item will be crafted.

TryCraft accepts a bool parameter that enables or disables the crafting cost. Crafting may fail if the player does not have sufficient currency.

bool result = myCrafter.TryCraft(false);

You can use CanCraft to check if something can be crafted. This method accepts 2 bool parameters: if the cost should be considered and if the failure reason should be invoked.

bool result = myCrafter.CanCraft(false, true);

Last updated