Crafting with Scriptable Objects

I’m wondering if anyones got some documentation / resources for this? I’ve looked over Scriptable Objects and it’s inventory systems capabilities but I want to take a shot at a crafting system with it. Nothing too complicated just simple a+b=c.

Would appreciate any resources!

Do you mean a lookup matrix? Something like this maybe :

public class CCrafting : ScriptableObject
{
    public CCrafting( )
    {
        craftMatrix = new Dictionary<string, string>()
        {   { MakeOrderedKey( "This", "That" ), "Other" }
        ,   { MakeOrderedKey( "Wood", "Iron" ), "Sword" }
        ,   { MakeOrderedKey( "Iron", "Leather" ), "Shield" }
        };
    }

    public string TheseMake( string s1, string s2 )
    {
        var thisKey = MakeOrderedKey(s1, s2);
        return (craftMatrix.ContainsKey(thisKey)) ? craftMatrix[ thisKey ] : "";
    }

    string MakeOrderedKey(string s1, string s2)
    {
        var b = (String.Compare(s1, s2) < 0);
        return (  (b ? s1 : s2)
                + "/"
                + (b ? s2 : s1));
    }

    Dictionary<string, string> craftMatrix;
}

I don’t think this is quite what im looking for, a break down of what im attempting to do would be.

3 Gameobjects (lets say ore, coal and iron)
1 Gameobject to serve as a “recipe”, it takes in 3 game objects (ore, coal, and iron [iron would be the result in this case])

I’m looking for a way to run something probably in the update to check if these 2 items are true do x (in this case, destroy ore and coal and create Iron)

I made a basic drag drop UI so I’m hoping to tie that into it. This is where i got the idea from (
https://www.youtube.com/watch?v=QkLXhdWq_0A
). I’m challenging myself to try and learn how to do a system like this.

I’ve done a few other micro-projects like currency, stores, basic controllers etc. and this is my next one a step more advanced for sure.

With the ‘recipe’ approach, it’s kinda nice n easy. :slight_smile:

  1. if the slots have to take a type of item, this is the most straight forward way. I mean, in your example, if the first item has to be in slot 1 and the second in slot 2, then when an item is added, (or even before fully added), just check that it’s the right one.
    Only ever-so-slightly different if an item could be in either of the two slots… then, all you have to do is ensure it’s one or the other. :slight_smile:

  2. If the recipe requires x number of ingredients to produce its output, then when they have been validated, either the item is available right away, or there is a start button , a timer, etc… extra details.

My example here differs from the video, somewhat, in that the video is more likely searching for valid recipes than providing the ‘blueprint’ (so to speak), which doesn’t require searching anywhere but itself?

This sounds pretty much like what I want to do. Im trying to figure out how to tell it where to search (since I don’t want it to just search the entire inventory) and how to check if any of the recipes would be true

In that case, you could use the Chain of Responsibility design pattern.

Perhaps we were discussing different things (or different in some parts).

I was imagining that you have a “window” that is there, and says:
Insert : 1) wood and 2) string → Get: Bow.

Literally has no where to search but its own recipe self, and the 2 ‘input boxes’.

Perhaps you had something else in mind. :wink: I’m sure there are a number of variations for crafting.

That’s what I was thinking with the CCrafting class above. The ‘recipe shelf’ being provided by the dictionary acting as a lookup matrix. The ordered key in the dictionary was to handle the problem you identified above with the items being placed in the input boxes in any order. :slight_smile:

I subsequently wondered if I had misunderstood the requirements and over simplified the request. So I mentioned the more involved design pattern alternative.

Absolutely, I agree. :smile:

@Doug_B : For sure, will wait to see how the OP responds. :slight_smile:

1 Like