Closed 31 closed 3 years ago
Asked for people to look at this on Discord, and it came up that you can use field initializers for the default:
[Export] public PackedScene Scene2 { get; set; }
= Engine.EditorHint ? GD.Load<PackedScene>("res://ExportPackedScene/ExternalScene.tscn") : null;
public override void _Ready() { ... }
However, field initializers only work for auto properties, so we can't track bool _sceneSet
, so intentionally setting the property to null doesn't work (the default will load anyway) with this method.
Actually, it is better to use a field initializer on the backing property. This way the generator doesn't have to generate a constructor, and the node script can implement its own constructor without interference.
[Export] public PackedScene Scene
{
get => _scene;
set
{
_scene = value;
_sceneSet = true;
}
}
private const string _sceneDefault = "res://ExportPackedScene/ExternalScene.tscn";
private PackedScene _scene = Engine.EditorHint ? GD.Load<PackedScene>(_sceneDefault) : null;
private bool _sceneSet;
public override void _Ready()
{
if (!Engine.EditorHint && !_sceneSet)
{
Scene = GD.Load<PackedScene>(_sceneDefault);
}
GD.Print($"End of ready: {Scene}");
}
GDScript lets you do this:
which shows up like this:
The default shows up nicely when you freshly attach it to a new node, and you can set it to something else.
This C# code does the same thing as far as I can tell: