Crafter
🛠️ Crafter Class
A class that can craft any item.
Namespace:
Esper.Inventool.Crafting
Access:public
Type:class
📦 Fields
public
combineItems
List<ItemStack>
A list of items to use for crafting. A single stack of each item will be used.
public static
onCraftFailed
UnityEvent<string>
A callback for when a crafting fails. Accepts one argument: the error message.
public static
onCraftSucceeded
UnityEvent<ItemStack>
A callback for when a crafting succeeds. Accepts one argument: the crafted item.
🧰 Methods
public
TryAddItem(ItemStack itemStack)
bool
Tries to add an item to the list of items to craft. Returns false if the player does not have enough stacks.
public
RemoveItem(int index)
void
Removes a crafting item by index. Logs a warning if the index is out of range.
public
RemoveItem(ItemStack itemStack)
void
Removes a crafting item by reference.
public
IsInRange(int index)
bool
Checks if an index is within range of the combine items list.
public
GetCraft()
Craft
Gets the craft data object or null if the combined items don't result in a specific craft.
public
GetResultItem()
ItemStack
Gets the result of the craft. May be null if the item combination doesn't equal anything.
public
CanCraft(bool applyCost, bool invokeFailureReason)
bool
Checks if the item can be crafted.
public
CanCraft(Craft craft, bool applyCost, bool invokeFailureReason)
bool
Checks if the item can be crafted with the provided craft data.
public
TryCraft(bool applyCost)
bool
Attempts to craft the item. May fail if there's no space in the inventory or not enough currency.
public
Craft(Craft craft, bool applyCost)
void
Crafts the item without any checks. Removes required items, applies cost if needed, and adds the result to the inventory.
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