Access child nodes in _Set function in C#

Godot Version

4.3-dev5

Question

i’m trying to write a tool script that exposes some of the properties of its child nodes, using _GetPropertyList and _Get/_Set overrides. the problem is that _Set is called before it or any of its children enter the tree, so it errors out when trying to access any of them.

from what i’ve found in GDScript working around this is trivial, by awaiting the ready signal inside the _set function. but to my knowledge, this isn’t possible in c# since await only works in async methods, which _Set can’t be because it has a bool return value.

is there any other simple way to work around it, that works in c#?

While I’m not 100% certain of this, I believe you can use CallDeferred() to achieve this.

1 Like

that seems to do the trick, thanks!

in case anyone stumbles upon this in the future, here’s pretty much what i did

public override bool _Set(StringName property, Variant value) {
	if (property == "cool_property") {
		// could be !IsInsideTree() too
		if (!IsNodeReady()) {
			CallDeferred(MethodName.SetCoolProperty, (float)value);
		} else {
			SetCoolProperty((float)value);
		}
		return true;
	}
	return false;
}

public void SetCoolProperty(float value) {
	GetNode<CoolNode>("CoolNode").CoolProperty = value;
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.