How would I check if the player has the required materials for a recipe?

I know all I need to do is just check if the player has the items and enough of them. but I’m unsure how to (using a dictionary at least)

as an example lets say these are the recipes

local crafting_recipes = {
	Engineering = {
		sharpstone = {
			Tool = gameItems["Sharp Stone"], 
			Recipe = {Stone = 2}
		},
		
		stonepickaxe = {
			Tool = gameItems["Stone Pickaxe"], 
			Recipe = {Sharp Stone = 2, Stick = 1}
		}
	},
}

how would I check if the player has two of Stone? I know I could check their inventory for it, but how would I know I’m looking for stone? I thought of maybe getting the name of the key, but it seems like that isn’t possible, and I feel like giving every material it’s own name is wayyy to excessive

sorry if it doesnt make any sense

What’s the alternative? Usually games will have a unique ID for every item in the game so that it’s easy to identify and reference them.

From the code you posted, if the key values in the Recipe table are the unique IDs, you could just use those when checking the player’s inventory.

local RequiredItems = crafting_recipes.Engineering.sharpstone.Recipe
local Inventory = ... -- Whatever inventory system you're using

for ItemId, RequiredAmount in RequiredItems do
    if not Inventory:Contains(ItemId, RequiredAmount) then
        -- Player doesn't have all the items for the recipe
    end
end
2 Likes

my problem is I don’t know how I would make sure the player has the item, and the right amount

and I don’t know how I would check this without going through every id in the game to see if it’s apart of the recipe

It sounds like you need to implement some type of inventory system for the player because this could be done with some simple calls to that system. Something like the following

local PlayerInventory = Inventories[Player] -- Some way to grab the player's inventory

-- If this returns 0, they don't have the item in their inventory at all. 
-- If it's anything less than the recipe requires, they don't have the right amount
local ItemCount = PlayerInventory:GetCount(RequiredItem)

You can design the interface for that however you feel is best.

You put the item ID in the recipe itself so you immediately know which IDs are part of the recipe

Recipe = {["Sharp Stone"] = 2, Stick = 1}

Looking at this, we know that Sharp Stone and Stick are part of the recipe. We don’t need to go through every ID in the game and cross check it with this recipe.

1 Like

I do already have one made but I’m guessing it’s not enough for this

I know that, but last time I checked you can’t easily get the name of a key, and I dont want to do unnecessary checks

is the best way just to go through the player’s inventory until the required item is found?

Just loop through the dictionary for the recipe.

for itemName, requiredAmount in pairs(recipeDict) do
    print(itemName, requiredAmount)
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.