Code not working correctly SOLVED

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)

Please update your post to include proper indentation and use preformatted text for code snippets.

Yeah, without proper formatting, this is difficult to read.

As an aside, this problem is probably simple to solve on your own by setting a breakpoint in your code, then stepping through each line using F11. You can set a breakpoint by clicking the empty space to the left of a line number (it will make a red dot appear. Click the red dot again to remove that breakpoint.)

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"]
}

The ingredients in your Recipes begin with a capital letter, but everywhere else you use lowercase. Therefore you always get zero ingredients present in your first loop.

If you used squared brackets it may even throw and error :thinking: but you supply a default zero with the .get(ingredient, 0) :slight_smile:

1 Like