Topic was automatically imported from the old Question2Answer platform.
Asked By
Eric Ellingson
Is there a way to define a class property with some sort of “onready” functionality, similar to how it’s done in gdscript? Could it be something similar to how exports are defined? (API differences to GDScript — Godot Engine (3.1) documentation in English) I haven’t been able to see any mention of this anywhere.
Something like this maybe?
public class Game : Node
{
[OnReady]
public Sprite sprite = GetNode("Sprite");
}
public Sprite sprite;
public override void _Ready()
{
sprite = GetNode<Sprite>("Sprite");
}
Thanks for the response. I’m aware of that approach, but was hoping there was something a little more terse available.
Eric Ellingson | 2020-01-08 23:04
They could build a Attribute system to do what you want,
The problem is it’s a lot of work, and pretty bad performance, and (arguably) makes your code harder to read. So very much not surprised this isn’t supported.
Jason Swearingen | 2020-01-09 14:46
thanks I came here looking for onready var timer: Timer = $Timer alternative in C#.
It generates _Ready() based on [OnReadyGet("Sprite")] private Sprite _sprite;. (It also optionally exports a NodePath property so you can change "Sprite" for specific nodes in the editor.) It doesn’t use reflection, so it doesn’t have that performance cost.