# How to implement combining 2 ingredients to create a product?

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?

• 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. May 15, 2017 at 15:02
• 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! May 15, 2017 at 15:27

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.

• 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. May 15, 2017 at 15:07
• 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 } 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:

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


In this case, you'd have

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



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.