#Inventory and Crafting System

1 messages · Page 1 of 1 (latest)

feral ingot
#

If anyone can VC with me it would be so much clearer I don't know how to explain this well but I'm going to do my best.

I have a UI with on the right item slots for inventory. In the middle are three slots. When you click an item on the right it goes into one of the top two slots in the middle and when two items in a recipe make an item the item appears in the last slot.

That all works.

What I'm trying to do is when you click the middle bottom button with the crafted item the materials used to craft it leave the inventory and the crafted item enters the inventory.

System wise it is doing that. But the UI isn't updating that. The picture of the crafted item stays in the middle and while the first crafting material is replaced with the crafted item the second crafting material icon stays there even though it's no longer in the inventory

#

I can provide code as requested

fresh quarry
#

[]cb

empty obsidianBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ``` (3 backticks. Click here to see where the key is)

To use syntax highlighting, add the file extension of the language you wish to highlight (cs for C#, cpp for C++)

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

feral ingot
fresh quarry
#

yep

feral ingot
#
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class RecipeSlot : MonoBehaviour, IPointerClickHandler
{
    public Item item;
    public Image icon;  // The UI image to display the item sprite
    public ItemSlot slot;
    private Inventory inventory;

    private void Start()
    {
        inventory = Inventory.instance;
    }

    // Adds an item to the recipe slot
    public void AddItem(Item newItem)
    {
        item = newItem;
        icon.sprite = item.itemSprite;  // Set the icon to the item's sprite
        icon.enabled = true;  // Make sure the icon is visible
        inventory.RemoveItem(item);
    }

    // Clears the slot (removes the item)
    public void ClearSlot()
    {
        item = null;
        icon.sprite = null;
        icon.enabled = false;  // Hide the icon when no item is present
    }
    
    public void OnPointerClick(PointerEventData eventData)
    {
        // If right click, send item to crafting
        if (eventData.button == PointerEventData.InputButton.Right)
        {
            OnRightClick();
        }
        else
        {
            return;
        }
    }

    public void OnRightClick()
    {
        ClearSlot();
        inventory.AddItem(item);
    }
}
#
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CraftingManager : MonoBehaviour
{
    public static CraftingManager Instance; // Singleton pattern

    private Item currentItem;
    public RecipeSlot[] craftingSlots;
    public List<Item> itemList;
    public string[] recipes;
    public Item[] recipeResults;
    public RecipeSlot resultSlot;
    public Sprite slotSprite;

    public RecipeSlot[] recipeSlots;

    private Inventory inventory;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

    private void Start()
    {
        inventory = Inventory.instance;
    }

    public void MoveToRecipeSlot(Item item)
    {
        foreach (RecipeSlot slot in recipeSlots)
        {
            if (slot.item == null)  // Find the first available empty slot
            {
                slot.AddItem(item);  // Add item to the recipe slot
                CheckForCreatedRecipes(); // Check if a valid recipe was created
                break;  // Exit loop once the item is placed
            }
        }
}
#
public void CheckForCreatedRecipes()
    {
        resultSlot.item = null;

        string currentRecipeString = "";

        // Build recipe string from crafting slots
        foreach (RecipeSlot slot in recipeSlots)
        {
            if (slot.item != null)
            {
                currentRecipeString += slot.item.itemName;
            }
            else
            {
                currentRecipeString += "null"; // Add null for empty slots
            }
        }

        // Check if the recipe string matches any known recipe
        for (int i = 0; i < recipes.Length; i++)
        {
            if (recipes[i] == currentRecipeString)
            {
                resultSlot.gameObject.SetActive(true);
                resultSlot.GetComponent<Image>().sprite = recipeResults[i].itemSprite;
                resultSlot.item = recipeResults[i];
                return; // Exit once the correct recipe is found
            }
        }
    }
     public void CraftItem()
     {
         Debug.Log("Is null?");
         if (resultSlot.item == null) return;

         foreach(RecipeSlot slot in recipeSlots)
         {
            inventory.RemoveItem(slot.item);
            Debug.Log(slot + "clearing");
            slot.ClearSlot();
         }

         inventory.AddItem(resultSlot.item);
         Debug.Log("Adding to inventory " + resultSlot.item);
         resultSlot.ClearSlot();
         Debug.Log("result slot cleared");

         inventory.onItemChangedCallback.Invoke();
     }
}
#
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public static Inventory instance;

    void Awake()
    {
        if (instance != null)
        {
            Debug.LogWarning("More than one instance of Inventory found!");
            return;
        }
        instance = this;
    }

    public List<Item> allItems = new List<Item>();
    public List<Item> items = new List<Item>();

    public delegate void OnItemChanged();
    public OnItemChanged onItemChangedCallback;

    public int space = 18;

    public bool AddItem(Item item)
    {
        if (items.Count >= space)
        {
            Debug.Log("Not enough room for item");
            return false;
        }

        items.Add(item);

        if (onItemChangedCallback != null)
            onItemChangedCallback.Invoke();
        return true;
    }

    public void RemoveItem(Item item)
    {
        if (items.Contains(item))
        {
            items.Remove(item);
            if (onItemChangedCallback != null)
                onItemChangedCallback.Invoke();  
        }
    }

    public Item GetItemByName(string itemName)
    {
        Debug.Log("Calling GetItemByName");
        foreach (Item item in allItems)
        {
            if (item.itemName == itemName)
            {
                Debug.Log("Name being returned by GetItemByName" + itemName);
                return item;
            }
        }

        Debug.LogWarning($"Item with name {itemName} not found.");
        return null;
    }
}
#

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using TMPro;

public class ItemSlot : MonoBehaviour, IPointerClickHandler
{
public Item item;

public string itemName;
public Sprite itemSprite;
public bool isFull;
public string itemDescription;
public Sprite emptySprite;

public Image itemDescriptionImage;
public TMP_Text itemDescriptionNameText;
public TMP_Text itemDescriptionText;

public GameObject selectedShader;
public bool thisItemSelected;

public InventoryUI inventoryUI;
public CraftingManager craftingManager;  // Reference to CraftingManager

public void AddItem(Item newItem)
{
    item = newItem;

    this.itemName = item.itemName;
    this.itemSprite = item.itemSprite;
    this.itemDescription = item.itemDescription;
    isFull = true;

    itemDescriptionImage.sprite = itemSprite;
    itemDescriptionImage.gameObject.SetActive(true);
}

public void OnPointerClick(PointerEventData eventData)
{
    if (isFull)
    {
        // If left click, select the item
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            OnLeftClick();
        }

        // If right click, send item to crafting
        if (eventData.button == PointerEventData.InputButton.Right)
        {
            OnRightClick();
        }
    }
}

public void OnLeftClick()
{
    if (isFull)
    {
        if (thisItemSelected)
        {

}