38. A native Godot button inside a PCA game
var overlay: CanvasLayer
var native_status: Label
var click_count: int = 0func 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)func _on_native_pressed() -> void:
click_count += 1
native_status.text = "Native Godot control: %d clicks" % click_countMake 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.
