Best practices for @onready and @export setters

I had the same issue in a tool script with a setter and found a proposal with a discussion of this problem. I used this workaround for Godot 4 from the discussion. Your get/set function would become:

@export var color: Color:
  get:
    return polygon2D.color
  set(new_color):
    if not is_node_ready():
        await ready
    polygon2D.color = new_color

Seems to work here, there’s no error anymore when loading the scene. The error before was: Invalid set index ‘color’ (on base: ‘Nil’) with value of type ‘Color’.

7 Likes