Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make C# classes show up in the Create New Node dialogue #1944

Closed
Darxoon opened this issue Dec 5, 2020 · 3 comments
Closed

Make C# classes show up in the Create New Node dialogue #1944

Darxoon opened this issue Dec 5, 2020 · 3 comments

Comments

@Darxoon
Copy link

Darxoon commented Dec 5, 2020

Describe the project you are working on

I'm working on a custom Control node in C# that derives from the base Control node.

Describe the problem or limitation you are having in your project

When trying to instance this new node, I have to create a blank control node and attach the script. This isn't a huge problem, but a small slowdown because I have to find the script in the File tree. If I had done this in GDScript (which doesn't work for performance reasons), I could use the class_name keyword and it would show up.

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

Do it like GDScript. There, when you use the class_name keyword, the script automatically gets added to the Create New Node dialogue.

There would be 2 solutions for C#. Either, we could just add every C# class to the dialogue automatically, or create an attribute which exposes the node to the 'Create New Node' dialogue and optionally give it an icon.

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

Let's say we have this oversimplified C# class:

[Tool]
public class CustomLabel : Control
{
    [Export(PropertyHint.MultilineText)] public string text;
    [Export] public Font font;

    public override void _Process(float delta)
    {
        Update();
    }

    public override void _Draw()
    {
        // ...
    }
}

If we were to go for the first solution, it would already work like this (this is a mockup):

Mockup that shows the C# class in the Create New Node dialogue

If we went for the second solution, I would suggest adding this Attribute: [ExposeAsNode(string iconPath = null)]. Having this attribute on the class will expose it in the dialogue. The optional parameter is for the icon. If it's left as null, it will inherit the icon from the super class. If the path is specified, it will use that.

So now, the class would look like this if we want it exposed and with a custom icon:

[Tool]
[ExposeAsNode("res://custom_label.ico")]
public class CustomLabel : Control
{
    [Export(PropertyHint.MultilineText)] public string text;
    [Export] public Font font;

    public override void _Process(float delta)
    {
        Update();
    }

    public override void _Draw()
    {
        // ...
    }
}

which would result in this (this is a mockup):

image

I'm open for suggestions on how to improve this.

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

No, since it's not code related. However, it can be worked around by creating the base node and adding the script manually, which is tedious because you have to do the additional step of looking up the script in the file system and dragging it onto the new node.

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

C# support is still in its early stages, and this for consistency between GDScript and C#. It can probably be worked around with an editor plugin that registers a custom type, but that has been made obsolete by the superior class_name (at least that's what I think. Research has shown contradicting results about custom types in editor plugins being obsolete).

@Calinou
Copy link
Member

Calinou commented Dec 5, 2020

Related to #997.

@aaronfranke
Copy link
Member

This is a duplicate (or sub-issue) of #22. As a workaround, you can use add_custom_type in EditorPlugin, but this requires that you place your script inside of a plugin.

@MattParkerDev
Copy link

This can now be done with the [GlobalClass] attribute, thanks to 72619

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants