godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.08k stars 69 forks source link

Change or allow customization of [Export] behavior in C# #3966

Open GlitchedCode opened 2 years ago

GlitchedCode commented 2 years ago

Describe the project you are working on

Simulation sandbox with a detailed UI. May also apply to a much wider class of scripts that interact with nodes in the scene tree.

Describe the problem or limitation you are having in your project

As it is currently implemented, the Export attribute in Godot's C# API, used to mirror the export keyword in GDScript, presents an annoying limitation when it comes to exporting C# properties with customized set and get accessors: exported values are assigned to nodes before even entering the scene tree, which makes writing clean code harder, as the following example causes a crash when the property is exported and modified through the editor.

[Export]
public string ComponentName
{
    get => GetNode<Label>("NameLabel").Text;
    set => GetNode<Label>("NameLabel") = value;
}

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Stricly in regards to scene initialization, the Export attribute's behavior should either be customizable or modified to assign the exported values after the Node instance enters the scene tree.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

The Export attribute can be modified to allow one more parameter that is used to determine at which point the exported value will be assigned: this could be an enumerative type or a string that allows the user to choose between object creation, after entering the tree or after the ready callback. Since exports should also support other types, such as Resource, reflection should be used to determine if the newly added parameter should be ignored.

If this enhancement will not be used often, can it be worked around with a few lines of script?

This issue can be worked around by using additional private fields, null checks and/or checks to ascertain the node is inside the tree, which can make for cumbersome code when frequently combining properties and nodes.

Is there a reason why this should be core and not an add-on in the asset library?

This feature can only exist as part of the GDMono API.

raulsntos commented 2 years ago

Related: #325