Crafting System

If we are talking about the class of the IngredientConfig (I’m a little baffled here on what I’m supposed to do…), this is it:

[System.Serializable]
        public struct IngredientConfig {

            public InventoryItem Ingredient;
            public int Amount;

        }

ingredients is an array IngredientConfig[], you’re using a singular element IngredientConfig. That’s what the

is trying to tell you. foreach has to be run on an array, list, or IEnumerable. (More to the point, foreach must be run on an object with an IEnumerable interface, which arrays and Lists just happen to implement.)

Oh wow… I did NOT notice that this was a singular value rather than an array… Thanks Brian

Now to investigate the next syntax error, this one:

var hasIngredient = playerInventory.HasItem(ingredientConfig.Ingredient, out var inventoryAmount);

This is the first line in the foreach loop in ‘CraftingTable.CanCraftRecipe()’, where my Inventory.HasItem() function does not take more than 2 arguments (P.S: I did not reach the end yet…)

Honestly speaking though, I’m surprised someone can code an entire crafting system in a single afternoon…

The answer is in Bixarrio’s tutorial…

Yup, found it. It’s in the end, I’m about 70% of the way there as we speak :slight_smile: (Excluding the UI Design, I’ll probably create that between classes tomorrow)

I recommend reading through the whole thing first… then beginning implementation…

I will slowly go through it all right now, and I’ll probably take notes too

Frankly speaking though, I’m more than grateful to meet the both of you, you both are helping me pull off miracles, somehow… :slight_smile: (thank you Brian, Bixarrio, and the rest of the amazing GameDevTV team and community for being here!)

I’m terribly sorry. I posted that and then got distracted and missed all this.

@Brian_Trotter got to all of it I see. The post isn’t organised very nicely. I actually forgot that I added the method to the Inventory until I was done with the post. That’s why it was at the end.

That’s absolutely fine. Frankly speaking, if it weren’t for you guys, I honestly never would’ve ever figured this system out on my own. I don’t know how you guys do it, but I’m glad you can. If anything, I’ll try implementing the new UI tomorrow (or tonight) and testing it out

I do have a question though, what’s a “recipe db”? You wrote that in the first sentence under your ‘CraftingTable.cs’ script, before the script itself

db is short for Database. As in the Crafting Table is serving as the recipe database. While @bixarrio wondered aloud if this was the best place for it, I’ll resolve all doubt, on balance it IS the best place for it. You could, however, add yet another ScriptableObject which would let you define all of the recipies (like a DropLibrary).

Not tonight please. For now, let’s just try make sure it works. I may or may not have more chaotic requests (hint: I do, but I won’t ask that now. I’ll go understand the new code first, step-by-step (and to make sure Brian doesn’t kill me))… :stuck_out_tongue_winking_eye:

Yes, it adds an extra layer of complexity to what is already a fairly complex system.

Didn’t see this question. I did this a long time ago but I believe the wind is just the built-in terrain wind stuff and the water is from the old standard assets - and it throws a lot of errors every time I go into 2D view to edit the UI

It’s all good. Frankly speaking though, the Synty water pack that comes with the Vikings assets are just a bit too low poly. It can absolutely use some help with a better water shader

As for the wind, I’ll search that up too

I thought about this after I posted and agree. In Valheim (from where I drew all inspiration) you have different types of crafting tables. Doing it this way, subclasses of the crafting table could filter the recipes to only hold the ones it can craft

Well since you guys have hugely contributed to me coding an entire crafting system out of thin air, I’d like to contribute back, even if it’s with something insanely little in return (it’s best I can do). If anyone needs a code that gets your character to run to the table and automatically open the crafting UI on his own when he arrives, here it is, integrated with @bixarrio 's ‘CraftingTable.cs’ script:

using System;
using GameDevTV.Inventories;
using GameDevTV.UI;
using RPG.Control;
using RPG.Movement;
using UnityEngine;

namespace RPG.Crafting {

public class CraftingTable : MonoBehaviour, IRaycastable
{

    [SerializeField] float acceptanceRadius;
    [SerializeField] ShowHideUI showHideUI;

    public static event Action<CraftingRecipe[]> OnCrafting;
    private CraftingRecipe[] allRecipes;

    private bool isMovingTowardsCraftingTable = true;   // this variable checks if the player is near the crafting table, or is 
    public bool isInCraftingUI = false; // this variable is a check that automatically shuts our Crafting UI down if we are interrupted

    private void Start() {

        allRecipes = Resources.LoadAll<CraftingRecipe>("");

    }

    public CursorType GetCursorType()
    {
        return CursorType.Crafting;
    }

    public void Awake() {

        acceptanceRadius = 3.0f;

    }

    public bool HandleRaycast(PlayerController callingController)
    {
        
        if (Input.GetMouseButtonDown(0)) {

            float distanceToCraftingTable = Vector3.Distance(transform.position, callingController.transform.position);

            if (distanceToCraftingTable > acceptanceRadius) {

                Debug.Log("Moving towards Crafting UI");
                callingController.GetComponent<Mover>().MoveTo(transform.position, 1.0f);
                isMovingTowardsCraftingTable = true;

                if (isMovingTowardsCraftingTable && distanceToCraftingTable <= acceptanceRadius) {

                    isMovingTowardsCraftingTable = false;
                    Debug.Log("Opening up the Crafting UI");
                    OnCrafting?.Invoke(allRecipes);

                }

                return true;

            }

            else if (distanceToCraftingTable <= acceptanceRadius) {

                Debug.Log("Opening up the Crafting UI");
                OnCrafting?.Invoke(allRecipes);
                isMovingTowardsCraftingTable = false;
                return true;

            }

        }

        if (isMovingTowardsCraftingTable && Vector3.Distance(transform.position, callingController.transform.position) <= acceptanceRadius) {

            Debug.Log("We are at the Crafting Table, opening up the UI now");
            OnCrafting?.Invoke(allRecipes);
            isMovingTowardsCraftingTable = false;
            return true;


        }

        return true;
    
    }

    public static bool CanCraftRecipe(CraftingRecipe recipe) {

        var playerInventory = Inventory.GetPlayerInventory();

        foreach (var ingredientConfig in recipe.GetIngredients()) {

            var hasIngredient = playerInventory.HasItem(ingredientConfig.Ingredient, out var inventoryAmount);
            if (!hasIngredient) return false;
            var hasEnough = inventoryAmount >= ingredientConfig.Amount;
            if (!hasEnough) return false;

        }

        return true;

    }

    }

}

I’m sorry about all the => bits (and there are a lot). I am so used to it that I don’t think about not doing it like that.

Alls’ good :slight_smile:

@bixarrio One last request please. If you have the spare time, would you be kind enough to create a video explaining the steps of creating the UI? I’m trying it on my own, but it’s a little complicated to guess what’s being created just based on the names in the heirarchy

Noooooooo, I do not do videos. If I did, the whole crating system would’ve been a video - it would’ve been much easier to put together. I would, however, attempt to break it down for you. You would just have to give me some time. Sorry

Privacy & Terms