Make object serialization/exporting by interface a first class citizen #7971
Replies: 7 comments 8 replies
-
I tried to fake this by doing the following but without luck.
It seems that the type GodotObject can't be used to serialize anything from the inspector. |
Beta Was this translation helpful? Give feedback.
-
Something similar can be achieved by overriding If you're just dealing with Resources, inheriting from a common base class and marking the export variable as common base class is enough I think. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I am 100% in favor of this. As a workaround, I wonder if this can be achieved with a C# addon/library using source generators. My idea is that we can use an attribute to generate a class that exports a plain Let's say we use an attribute like this: public partial class MyNode : Node2D
{
[ExportInterface]
public IComponent Component { set; get; }
} It would generate this: // MyNode.generated.cs
public partial class MyNode
{
[Export]
private Node _component;
// Call this in _Ready()
// Not sure if this can be automated
public void WireComponents()
{
Component = _component;
}
} And to enforce the interface, we can do something like this: [Tool]
public partial class InterfaceExportInspectorPlugin : EditorInspectorPlugin
{
public override bool _ParseProperty(GodotObject @object, Variant.Type type, string name, PropertyHint hintType, string hintString, PropertyUsageFlags usageFlags, bool wide)
{
// This service will use reflection to figure out if it's an interface export
if (_attributeAnalyzerService.IsExportInterface(@object, name))
{
// Add a custom Node picker that highlights only valid implementations
AddPropertyEditor(new CustomNodePicker(_attributeAnalyzerService, @object, name));
return true;
}
return false;
}
} This last part might be a bit difficult because of godotengine/godot#80693, so we will have to use reflection to figure out what attribute and interface belong to a property. I tried to implement this and was able to get the attribute reflection working, but got stuck on the generator so I kinda gave up. Curious if anyone else might be able to implement this or have ideas for simpler ways to achieve this. |
Beta Was this translation helpful? Give feedback.
-
Coming back to this post, I realised that Godot via gdscript doesn't have this feature but it does attempt to use it with functions like has_method being used to hack the concept of contracts / interfaces into the language. Having a gd interface could be a case of defining a gd script that has all the functions you care about but nothing |
Beta Was this translation helpful? Give feedback.
-
FYI I've made a proposal for this: #8722. After a few weeks of working on an addon I think I have a pretty good idea of how this can be implemented in core. Let me know if there is anything I've missed in the proposal. |
Beta Was this translation helpful? Give feedback.
-
Came back as I saw this and it seemed like a good add to the bonfire |
Beta Was this translation helpful? Give feedback.
-
Could not find any open discussion on this topic. There is however an issue from 2019.
When dealing with the implementation and referencing other objects, Godot4 introduced a great feature where Exports reference Nodes by their type directly instead of needing to get them by path.
This would be a step after that.
With the engine supporting this cleanly, game code could be way cleaner and have more of a plugin architecture where they just need to know the implementation of the other types.
Something similar can be implemented with abstract classes, but then things become harder. Not to mention that there is no way then to have the interface be either something located inside the same scene or on a shared resource.
The idea behind this is that you could for example have something similar to this
And then similar to what this project, and what godot already does, have a dropdown where you can easily create a new instance or reference an existing one from the Project.
Relevant image from the implementation we have in Unity for this (https://github.com/Guillemsc/ImplementationSelector)
Godot's way of handling references is even better than unity as it could allow for scene embedded/referenced asset, something which unity can't easily do in their current serialization strategy.
Beta Was this translation helpful? Give feedback.
All reactions