Skip to content

Make onready work for nested onready fields #23

Description

@rawsp33d

Right now @[gd.onready] works for fields that are predefined Godot types, and it also works for custom structs. However it does not properly initialize the onready fields on those types. This is really only relevant for custom classes.

IE if you have this:

struct Main {
	gd.Node
mut:
	death_sound gd.AudioStreamPlayer @[gd.onready]
	hud            HUD       @[gd.onready]
}

struct HUD {
	gd.CanvasLayer
mut:
	start_button  gd.Button @[gd.onready:]
}

The Main instance will indeed have instance.hud set on ready, but instance.hud.start_button will be empty.

To work around this for now you can just remove the onready from custom structs, and initialize them manually in ready_ of the parent:

fn (mut s Main) ready_() {
	// TEMP: workaround onready not working great with nested onready nodes
	node := s.get_node_v('hud')
	if mut hud := node.try_cast_to_v[HUD]() {
		s.hud = hud
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions