+1 vote

So, currently I feel like this code for the keypad is too... much... Any ideas how to make the code just a little bit shorter?

The node order:

extends Control

func _on_Nine_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "9"


func _on_Eight_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "8"


func _on_Seven_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "7"


func _on_Six_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "6"


func _on_Five_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "5"


func _on_Four_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "4"


func _on_Three_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "3"


func _on_Two_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "2"


func _on_One_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text.length() == 4:
        return
    if $KeyHolder/TextHolder/InputText.text.length() > 4:
        $KeyHolder/TextHolder/InputText.text = ""
    $KeyHolder/TextHolder/InputText.text += "1"


func _on_Enter_button_up() -> void:
    if $KeyHolder/TextHolder/InputText.text == "6969":
        $KeyHolder/TextHolder/InputText.text = ""
        $KeyHolder/TextHolder/InputText.text = "Success!"
    else:
        $KeyHolder/TextHolder/InputText.text = ""
        $KeyHolder/TextHolder/InputText.text = "Failed!"


func _on_Clear_button_up() -> void:
    $KeyHolder/TextHolder/InputText.text = ""
Godot version 3.2.3.stable
in Engine by (64 points)

1 Answer

0 votes

You've got a lot of repeated code here. gdsad That's a sign that you can just make one function and reuse the code from it. Additionally, you can connect all of your buttons to the that same function and pass what you add to the InputText as an argument. Also, you can store a reference to the InputText node in a variable so you don't have to get it with $ every time.

by (7,190 points)

I don't know any way to pass an argument to a signal.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.