Imran's personal blog

February 20, 2024

Godot 4 Pinned Notes

Filed under: Uncategorized — ipeerbhai @ 4:19 pm
Tags: , , , ,

These notes are to either handle Godot 4’s language changes, or to correct wrong knowledge.

Godot 4.2 language changes

  • Scene unique variables.
    Variables can be set unique in a scene to refer to Godot controls. This is done by using the % sign. So, if I make a any control in the editor and set the unique name property, I can then access that node by prefixing % to the name in GDScript. Example:
    – set it: a VBoxControl unique name to %fubar in the editor.
    – use it: %fubar.add_child(checkbox)
  • .connect API change.
    The node.connect API has changed. Previously, the 3 argument API was .connect(signal_name, object, handler_name) where both signal_name and handler_name were strings representing the signal to subscribe to and the handler, respectively. Now, the API has 2 arguments, the signal_name, and the function in the object instance to call. Example:
    old: http_request.connect(“request_completed”, self, “_on_request_completed”)
    new: http_request.connect(“request_completed”, self._on_request_completed)
  • parse_json is deprecated. Use the JSON class instead. Example:
    var my_json = JSON.new()
    var foo = my_json.parse_string{json_string)
  • Reference base class renamed to RefCounted
  • rect_min_size control property is deprecated.

GDScript Error Corrections:

  • Signals expose a connect function with parameter binding to the receiver. Here’s an example with checkbox:
    # Create a checkbox, connect the pressed signal to a receiver, and send a parameter to the receiver instance
    var receiver_object = Receiver.new()
    var send_parameter = 1
    var checkbox = CheckBox.new()
    checkbox.pressed.connect(reciever_object.function.bind(send_parameter))
  • Callbacks must be of type Callable, and called via .call.
    Other call syntax is incorrect. If I want to bind a single function callback, I have to do it like this:
    func use_callback(callback: Callable):
    var my_param = 1
    var output = callback.call(my_param)


Blog at WordPress.com.