Typing an enum-like param

So let’s say I have this methid

class_name TransitionManager extends Node
...
func transition(type: String, etc...)

I don’t want to use an enum for the type, I want to leave it as String, but I want that autocompletion that you get when you use something like Input.is_action_just_pressed(). So if my types are fade and cutscene, I want to be able to just write:

TransitionManager.transition(" #<- and here I get the intellisense

How could I achieve something like that?

Is having Strings integral to your design? My initial thought is make a Transition class, and the have every transition inherit from that. then change your declaration to func transition(type: Transition, etc...). Then you should get your autocomplete.

True, I’m not really that attached to the idea of strings there, but there are other places too where just a simple string is all that is needed, and I’d wanna how how to achieve this.

I’m coming from web, so in Typescript that would be:

type TransitionType = "fade" | "cutscene" | etc.
...
const transition = (type: TransitionType, etc...)

I’m asking because I see it is possible for something like Input.is_action_just_pressed("move_down"), but I’m not sure how that’s achieved, if it’s something I can do as well, or just that the godot editor provides that for some built in stuff.

String autofill is only for built-in cases. Files, Nodepaths, @rpc, and Actions as far as I know.

In GDScript you can use enum

enum TransitionType {
    fade, cutscene
}

func transition(type: TransitionType) -> void:
    pass

I see.
What I find absolutely annoying about enums is the fact that I have to type TransitionManager.TransitionType.fade

maybe without the name will reduce specifiers

enum {
    fade, cutscene
}

Based on what @gertkeno said, you could write something in C++ and compile your own version of the editor to make this happen. It might also be possible in C#. Because with C# you have to use an external editor like VS Code, and you might be able to get VSCode to do that autocompleting for you.

I also hate the multiple dots for autocomplete. What I do is based on two things. The first is a YouTube video lecture on C++ where the speaker made a really good case for eliminating the word “Manager” from naming conventions. (I tried to find it to link it for you, but Google failed me.) What he said made sense. Either the manager is the thing (i.e. GameManager becomes Game), or it’s a collection of things (i.e. TransitionManager becomes Transitions). This makes you think hard about what to name things he said, and often makes your object hierarchies clearer.

The second is I put enums in their own class. This is something I learned from an experienced Java developer I worked with years ago. Put all your constants in their own constants directory. If you ever need to edit them, they are easy for anyone to find. So you could put your Enum in a TRANSITION class, then put an enum in it and nothing else, and don’t name don’t name the enum. Then you have TRANSITION.WHATEVER.

This is more advice than a solution, but I hope it helps.

1 Like