Turn both the recipe and the player's ingredients into a Dictionary of ingredient to count, and compare the two with ==. In Godot 4 that compares keys and counts and ignores the order. In our Godot 4.7 test, comparing arrays missed a Healing Pill put in as Moon Dew, Spirit Grass, Spirit Grass, and checking that every recipe item was present with has() gave the wrong answer for 4 of the 6 mixes. The script got all 6 right.
The code
Create one, add each recipe once with add_recipe(), then call find_recipe() with whatever the player put in the pot, crafting grid or forge. It returns the recipe's name, or an empty string when the mix makes nothing. Ingredients can be anything that works as a Dictionary key, such as names or item IDs.
extends RefCounted
## Finds the recipe a set of ingredients makes: any order, and the counts must match exactly.
var recipes := {} # recipe name -> {ingredient: count}
func add_recipe(recipe_name: String, ingredients: Array) -> void:
recipes[recipe_name] = count(ingredients)
## Returns the recipe name, or "" when the ingredients make nothing.
func find_recipe(ingredients: Array) -> String:
var have := count(ingredients)
for r in recipes:
if recipes[r] == have: # same keys and counts; the order of the keys does not matter
return r
return ""
static func count(items: Array) -> Dictionary:
var d := {}
for it in items:
d[it] = d.get(it, 0) + 1
return d
What we measured
Two recipes: Healing Pill is 2 Spirit Grass and 1 Moon Dew, Qi Pill is 1 Spirit Grass, 1 Moon Dew and 1 Ember Lotus. Six mixes. Two runs gave the same answers.
- Healing Pill in the recipe's order: all four ways found it.
- Healing Pill in another order: comparing arrays found nothing.
has(), sorting and the script found it. - 1 Spirit Grass and 1 Moon Dew, one grass short:
has()said Healing Pill. Sorting and the script: nothing. - 2 Moon Dew and 1 Spirit Grass, the counts swapped:
has()said Healing Pill. Sorting and the script: nothing. - Qi Pill plus an extra Iron Root:
has()said Healing Pill. Sorting and the script: nothing. - Qi Pill in another order:
has()said Healing Pill, the wrong recipe, because every Healing ingredient was in the pot. Sorting and the script: Qi Pill. - Two Dictionaries with the same keys written in a different order:
==returned true.
Why simple checks go wrong
Two arrays are only equal when every item is in the same place, so the player has to guess the order. has() only asks whether an item is there at all, so it cannot see counts or extra items, and a small recipe matches any bigger mix that contains it. Sorting both lists before comparing does work. It needs a copy first, because sort() changes the array itself: in our test, sorting the player's array without a copy reordered their pot. Counting into a Dictionary skips that.
What to change
- To let the player throw in extra ingredients that do nothing, remove those from the count before comparing.
- For a hint like "one ingredient away", compare the counts key by key and report what is missing.
A whole pill refining system: Xianxia Alchemy & Pill Refining System for Godot 4 has 9 herbs, 6 recipes, a furnace heat minigame, experimenting to discover recipes, alchemist levels and save and load. Its recipes match herbs and counts in any order the same way.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Xianxia Alchemy & Pill Refining System for Godot 4 has herbs, recipes, a furnace heat minigame, experimenting to discover recipes, levels and save/load.
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene