Hey,
I was going through the crafting guide posted by one of the users RPG Crafting System, and I wanted to add more control to it.
For starters, I wanted to add a+ and - button, that would let the user control the output number of the item that they would receive. So if you would click + you would get +1 added to the outputNumber field.
For this, I created the public int outputNumber = 1; field in the CraftingRecipe.cs, under the Recipes class:
using UnityEngine;
using RPG.Inventories;
namespace RPG.Crafting
{
[CreateAssetMenu(menuName = "RPG/Crafting/Crafting Recipe")]
public class CraftingRecipe : ScriptableObject
{
[SerializeField] Recipes[] recipes;
CraftingRecipe.Recipes recipe;
[System.Serializable]
public class Recipes
{
public int outputNumber = 1;
public InventoryItem item;
public Ingredients[] ingredients;
}
[System.Serializable]
public class Ingredients
{
public InventoryItem item;
public int number;
}
public Recipes[] GetCraftingRecipes()
{
return recipes;
}
}
}
Now that I can specify the number of items I want to craft, I had to go and modify the Craft.cs file, more specifically the CraftItem method AddToFirstEmptySlot line, to reference the outputNumber:
inventory.AddToFirstEmptySlot(inventoryItem, recipe.outputNumber);
One issue that I encountered here is that, if you have a non stackable item, you can still output more than 1 item. The problem that I ran into was that non stackable item would appear in your inventory as a stacked item, and not one individual item per inventory slot. To get around this, I said that when I would create a recipe, I would make sure that non stackable items would only yield 1 item, and only stackable items could yield more. (Since I couldn’t figure out how to make it so that the non stackable items appear as one individual item per available slot)
So far so good.
Next, I went and created the + and - buttons, serialized them in the CraftingUI.cs and also made sure to create them in the CreateRecipeObjects method as well as add function to the buttons.
private void CreateRecipeObjects(InventoryItem inventoryItem, Transform recipeHolder, CraftingRecipe.Recipes recipe)
{
// Create the arrow image UI element and make it a child under the recipeHolder transform.
var arrow = Instantiate(recipeArrow, recipeHolder);
// Instantiate the itemSlot prefab and make it a child under the recipeHolder transform.
var item = Instantiate(itemSlot, recipeHolder);
// Set up the item’s (item) icon and number amount.
item.Setup(inventoryItem, recipe.outputNumber);
// Create the crafting button UI element and make it a child under the recipeHolder transform.
var button = Instantiate(craftButton, recipeHolder);
// Get the Craft script component from the button gameobject and store it in a variable.
var craft = button.GetComponent<Craft>();
// Add a listener to the button onClick event using a lambda expression.
button.onClick.AddListener(() => craft.CraftItem(inventory, inventoryItem, recipe));
var minus= Instantiate(minusButton, recipeHolder);
var plus = Instantiate(plusButton, recipeHolder)
if (inventoryItem.IsStackable())
{
minus.onClick.AddListener(() => recipe.outputNumber -= 1);
plus.onClick.AddListener(() => recipe.outputNumber += 1);
}
Now when I click the + button, the output will increase by 1, and - will decrease by 1.
But there are a couple of issues now:
- When I either click the + or - button, the UI does not update immediately, meaning that I can only see the increased output number only after I close and then open the UI again. I didn’t know how to make it show it immediately. The Redraw method in it is a bit strange to me.
- If I click the - button and I am already at 0, it will go below to negative values. I would like to make it so that if you click - and you are at 1, it should stop there and not let you go lower. The minimum output number should always be 1.
- Now, if you increase or decrease the number of the the output, the number of the materials that it cost to craft should also increase or decrease accordingly. So if 1 box takes 2 wood to make, 2 boxes would need 4 wood, or however many materials you are using, their required number should increase accordingly. I tried to make this work but I couldn’t really since I did not know where exactly to do this.
I saw that there is a CreateRecipeIngredients method in CraftingUI.cs that takes care of creating and setting up the materials you need to craft the item alongside their number.
I tried to somehow make the change there since there is the following line:
ingredientItem.Setup(recipe.ingredients[ingredient].item, recipe.ingredients[ingredient].number);
At first, I wanted to test and see if it works and I added:
if (outputNumber > 1)
{
ingredientItem.Setup(recipe.ingredients[ingredient].item, recipe.ingredients[ingredient].number += recipe.ingredients[ingredient].number);
}
else
{
ingredientItem.Setup(recipe.ingredients[ingredient].item, recipe.ingredients[ingredient].number);
}
For testing purposes, it worked, once I clicked + and the output of the item was greater than 1, the required material number went up by + however many there were initially.
I know this is not the right way to do it, because the problem was that every time that I would close and open the UI, the Redraw() method would kick in, and the material cost would constantly increase itself until… however many times you would open and close the UI.
So the problem is that I have no idea how to make it so that the material cost would increase / decrease only once after you click + or -, and to also increase the material cost accordingly. So it should have a base cost that would increase accordingly: 1item = 2cost, 2items = 4cost, etc;.