Recipe detecting system

Hello everyone, I am making a recipe system for my game but the problem is that I don’t know how to approach it.

Basically what I’m trying to do is I would have a table called “Recipe” containing other tables which are food containing ingredients, such as

Recipe = [Hamburger = {Bread, Meat, Lettuce}, --Other recipes–]

And then when the player puts ingredients in another table, it would run and check through all the recipe tables and if there’s a food table that match the ingredients table or not kinda like this
For example

{Bread, Meat} or {Bread, Meat, Meat} It would return nothing
But if it was {Bread, Meat, Lettuce} or {Lettuce, Bread, Meat} It would return hamburger

I have searched through the Devforum and found some stuff but it still didn’t get me clear.
I’m sorry if I am asking too much here, I just need a way to approach it, if you guys have any ideas or any useful tips please comment it below, Thanks in advance!

  1. Do you need it to return a list of all possible recipes, given a set of ingredients on the table?
  2. Do you want to include only exact matches, or do you want to also include recipes that require some of the ingredients on the table? For instance, if you had Hotdog = {Bread, Meat} and your table had Meat, Lettuce, Bread, should you return just { "Hamburger" } or { "Hamburger", "Hotdog" }?

this could possibly be what u need

local recipes = { --// this can be in a modulescript
	Hamburger = {'Bread', 'Meat', 'Lettuce';};
	Test = {'Meat', 'Bread';};
};

local ingredients = { --// my ingrendients
	'Bread', 'Meat', 'Lettuce';
};

local function compare_recipes(r1, r2)
	if #r1 == #r2 then --// hey. this is only for exact recipe.
		local f = 0;
		for _, v in next, r1 do
			if table.find(r2, v) then
				f += 1;
			end;
		end;
		return f == #r1;
	end;
end;

local function search_recipe(t)
	for name, data in next, recipes do
		local recipe = compare_recipes(t, data);
		if recipe then
			return name;
		end;
	end;
end;

print(search_recipe(ingredients))

note: this doesn’t work well for recipes that require 2 of the same thing

1 Like