Skip to reading
GDScript Programming & Godot

38. A native Godot button inside a PCA game

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
var overlay: CanvasLayer
var native_status: Label
var click_count: int = 0
Code example
func run() -> void:
    if is_instance_valid(overlay):
        return
    overlay = CanvasLayer.new()
    overlay.layer = 120
    add_child(overlay)
    var panel := VBoxContainer.new()
    panel.position = Vector2(35, 100)
    overlay.add_child(panel)
    native_status = Label.new()
    native_status.text = "Native Godot control: 0 clicks"
    panel.add_child(native_status)
    var button := Button.new()
    button.text = "Test Godot signal"
    panel.add_child(button)
    button.pressed.connect(_on_native_pressed)
Code example
func _on_native_pressed() -> void:
    click_count += 1
    native_status.text = "Native Godot control: %d clicks" % click_count

Make this record global, then run it during play. It creates a CanvasLayer, a layout container, a label and a button. The pressed signal changes the label. All nodes are children of the script, so they go away with that instance.

This is a useful small custom-tool pattern. For interfaces you want to author, translate, theme and manage through PCA, the GUI Designer remains the easier route. A native Control is not automatically registered as a PCA GUI element.

Figure 12. A real runtime test of the native Godot button and its pressed signal.

Page 47 of 378Permalink
Go to page