B
Bamboo47

Stuck on ToDo List Practice

Hi, I can't seem to get it working for the "on Button_pressed" part. I've added the "add_todo" function but the practice says it's not adding a new todo list.


extends VBoxContainer
onready var line_edit := $HBoxContainer/LineEdit
func add_todo(text: String) -> void:
	# Create an instance of the TodoItem scene, add it as a child, and set its text.
	var todo_item := preload("res://Scoreboard/TodoList/TodoItem.tscn").instance()
	add_child(todo_item)
	# Call the todo_item's set_text() function to change the displayed text.
	todo_item.set_text(text)
func _on_Button_pressed(text) -> void:
	if line_edit.text:
		# Add a todo item with the correct text.
		add_todo(text)
	line_edit.text = ""
  • Xananax replied
    Solution

    It's almost correct!

    One misunderstanding of how signals work though:

    When you press the button, it runs a function you specify. In this case, the function _on_Button_pressed().

    But the button does not pass any value to the function! Yet, you declared the function with a parameter called text.

    func _on_Button_pressed(text) -> void:

    When the button is pressed, Godot looks at the function, sees it expects one parameter, and since it has none (the Button being pressed needs a function with 0 parameters) refuses to run the function.

    Maybe you can figure things out from here. See if that's enough! If not, keep reading.

    If you look at the debugger while the scene is running, you should see this:



    emit_signal: Error calling method from signal 'pressed': 'VBoxContainer(TodoList.gd)::_on_Button_pressed': Method expected 1 arguments, but called with 0.

    Errors may look daunting, but if you read it, you'll see it says the same thing I was explaining. The function ("Method") expects 1 argument (the parameter "text"), but was called with no argument.

    Your solution is simple: remove text from the parameter list, and restore it to:

    func _on_Button_pressed() -> void:

    You will need to pass the text to add_todo() differently.

    Try to figure it out! Continue reading if you can't.

    You know where to find the text. It's written in the function itself! Right before the add_todo(text) line, you read the text from line_edit.

    This is where the text is. line_edit.text is the text you need.

    Call the function, and give it the text of the line edit:

    func _on_Button_pressed() -> void:
        if line_edit.text:
            add_todo(line_edit.text)
        line_edit.text = ""

    And you should be good to go.

    1 love
  • B
    Bamboo47 replied
    ... Oh you've gotta kidding me. LOL. Thanks for pointing it out.