In my game, you can combine a variety of ingredients to make a product. For example, you could combine water and dirt to make mud.

What I am currently doing to achieve this is by listing each combination possibility that a particular ingredient has. The one thing I managed to do to make it a little more efficient is that I managed to remove duplicates (So for example, if I have coded ingredientA + IngredientB = Cheese, there isn't another entry for IngredientB + Ingredient A = Cheese).

The problem I have with my method is that it is very annoying adding new ingredients. If I do so, I have to go back to every previous ingredient I have coded, and add it's interaction with the ingredient(s) I want to add.

The amount of ingredients I have means this is becoming increasingly time consuming.

Is there a better way to do this?

  • \$\begingroup\$ Maybe it would help you if you just build a better editor for your recipes. Unity editor scripting is a very powerful tool. You can use it to create whole new UIs for creating and maintaining custom assets inside the Unity editor. \$\endgroup\$
    – Philipp
    May 15, 2017 at 15:02
  • \$\begingroup\$ I was considering this also, I already have a rudimentary custom interface setup already, but the problem still remains that every new ingredient takes longer to implement. I'm not very experience with the editor scripting, so my implementation clearly could be better. Thanks for your help though! \$\endgroup\$ May 15, 2017 at 15:27

2 Answers 2


Each product that exists in your world could be listed in an array or table in a database.

Each product can then have a collection of Recipes.

Instead of each Recipe actually containing a Product, the can simply reference the Products necessary to produce another Product.

For example, let's say Cheese is made from Milk and Chemicals. :) All these things will exist on the same level wherever you decide to store them. Cheese will have a Recipe. This Recipe will reference Milk and Chemicals. Assuming Cheese has another Recipe (say, Milk and Fart), this other Recipe will also reference the necessary products.

I believe this way, there's no need to worry about backward compatibility.

  • \$\begingroup\$ Thanks Alot! I'm fairly sure you've solved my problem. One small thing I'm struggling to wrap my head around in what you outlined above, is how I should code the recognition of the ingredients. Currently, I take the itemID of ingredient A, and then search an ingredientA array with ingredientB's itemID. This yields me the itemID of the product. It's not immediately obvious to me how I would search for the correct recipe in your example. \$\endgroup\$ May 15, 2017 at 15:07
  • \$\begingroup\$ I stopped being lazy and wrote out some pseudo code and figured it out (it was obvious). Thanks alot for your help. On the off chance someone was similarly confused: I just take both the ID's in an if statement. if (ID1 == 5 && ID2 == 23) { spawn product X } \$\endgroup\$ May 15, 2017 at 15:23

Following what Didia said, what you want sounds like a Composite pattern (https://en.wikipedia.org/wiki/Composite_pattern).

A resulting product is the same thing as it's parts. In your case, a cheese would be a product just as much as milk and rennet. Each product could have an array of "product required" to create it, but in the end they're all the same conceptually. So when you add a new product, you just need to specify it's required products to create it:

                 cheese <
               /          rennet
mac n' cheese <
               \         flour
                 pasta <

In this case, you'd have

List<Product> list_products = new List<Product>();

list_products.add(new Product("milk",null));
list_products.add(new Product("rennet",null));
list_products.add(new Product("flour",null));
list_products.add(new Product("egg",null));
list_products.add(new Product("cheese",new Tuple<string,string>("milk","rennet")));
list_products.add(new Product("pasta",new Tuple<string,string>("flour","egg")));

To add mac n' cheese, simply push a new value in the list

list_products.add(new Product("mac n' cheese",new Tuple<string,string>("cheese","pasta")));

I used Tuple wince you said that 2 product create another, but Lists or even arrays could do the job here.

Of course the code is probably not working (or working awfully with string comparison, problems with localization, etc.), so see it as pseudocode, but you should get the gist of it. The cool thing is that you could (and should IMO) put your products in a nice JSON or XML format that is easily parsable and scalable with this kind of data. Also, a hashtable of ("ID",Product) would probably be easier to manage.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .