How to Make a TextEdit Button Game

Godot Version

4

Question

I'm making a simple game but my code isn't working. The principle is quite simple, i want it to exit the game when i type apple and press the button. How can i do this? Here's my code:

@export var input_field: TextEdit

func _ready():
	connect("pressed", Callable(self, "_on_button_pressed"))

func _on_button_pressed():
	var user_input = input_field.text
if user_input == "apple":
		get_tree().quit()

I used this quick setup and works as expected:

extends Control

@onready var text_edit: TextEdit = $TextEdit
@onready var button: Button = $Button

func _ready() -> void:
	button.pressed.connect(_on_button_pressed)

func _on_button_pressed() -> void:
	var text = text_edit.text
	# Both lines work
	# if (text == "apple"):
	if (text && text.find("apple") == 0):
		get_tree().quit()
1 Like