Skip to reading
GDScript Programming & Godot

16. Read player input from an authored GUI

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView illustrations
Code example
func run() -> bool:
    var entered: String = str(GetGUIElementProperty(
        "name_entry", "name_box", "text", ""
    )).strip_edges()
    if entered.is_empty():
        SetGUIElementProperty("name_entry", "feedback", "text", "Enter a name.")
        return false
    Variable.player_name.value = entered
    SetGUIElementProperty("name_entry", "feedback", "text", "Hello, " + entered + ".")
    return true

1. Create a GUI called name_entry with a Text Box named name_box, a Label named feedback, and a Confirm button.

2. Create a text variable player_name. Put the example in a native script record called accept_name.

3. On Confirm → Events → Click, choose Run Visual Script / Run Script and target accept_name. Leave the event’s Verb blank.

4. Validate and apply the script, save the GUI, show it during play, type a name and click Confirm.

Figure 4. The authored interface collects the name; its Confirm event calls the native GDScript record.

Page 24 of 378Permalink
Go to page