Godot 4.3
My code always shows “not enough ingredient for drink” even when i select the correct ingredients, how can i fix this?
extends Control
@onready var Boiling = %Boiling
@onready var Pouring = %Pouring
@onready var Drink = %Drink
@onready var drink_images = {
"Coffee": preload("res://Drinks/coffee.png"),
"Tea": preload("res://Drinks/tea.png"),
"Matcha_Latte": preload("res://Drinks/matcha.png"),
"Hot_Chocolate": preload("res://Drinks/hot_chocolate.png") # Fixed file name
}
var ingredients = {
"sugar": 0,
"milk": 0,
"matcha": 0,
"coffee_beans": 0,
"chocolate": 0
}
const Recipes = {
"Coffee": ["Coffee_Beans", "Milk", "Sugar"],
"Tea": ["Matcha", "Sugar"],
"Matcha_Latte": ["Matcha", "Milk"],
"Hot_Chocolate": ["Chocolate", "Milk", "Sugar"]
}
func _on_sugar_pressed() -> void:
ingredients["sugar"] += 1
func _on_milk_pressed() -> void:
ingredients["milk"] += 1
func _on_matcha_pressed() -> void:
ingredients["matcha"] += 1
func _on_coffee_beans_pressed() -> void:
ingredients["coffee_beans"] += 1
func _on_chocolate_pressed() -> void:
ingredients["chocolate"] += 1
func _on_drink_machine_pressed() -> void:
# Loop through the recipes to find a match
for drink_name in Recipes.keys():
var recipe = Recipes[drink_name]
var valid = true
var temp_ingredients = ingredients.duplicate() # Copy of the current ingredients
# Check if the recipe can be made with the selected ingredients
for ingredient in recipe:
if temp_ingredients.get(ingredient, 0) > 0:
temp_ingredients[ingredient] -= 1
else:
valid = false
break
# If the recipe is valid, brew the drink
if valid:
print("Brewing ", drink_name)
# Play boiling and pouring animations
Boiling.play(0.0)
await get_tree().create_timer(4.0).timeout # Wait for boiling sound
Pouring.play(0.0)
await get_tree().create_timer(1.0).timeout # Wait for pouring sound
# Show the brewed drink image
$Drink.texture = drink_images[drink_name] # Display the image of the brewed drink
# Reduce the ingredients used in the recipe
for ingredient in recipe:
ingredients[ingredient] -= 1
break # Exit the loop after brewing one drink
else:
print("Not enough ingredients for ", drink_name)