How do I reference an enum from an autoloaded script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By magicwings

I have a file called skill_move.gd that contains the following:

extends Node

enum Skill {
	Jump,
	Backflip,
	DoubleBackflip
	TripleBackflip,
	TooManyBackflips,
	GoodLand,
	GreatLand,
	PerfectLand
}

This file is autoloaded via the AutoLoad tab in Project Settings, with the name Skillmove and the Global Variable checkbox enabled.

In a scene that’s loaded later in the execution of the game, I have this line of code:

func _process(_delta):
	var backflip_enum = Skillmove.Skill.Backflip

which throws this error:

Invalid get index 'Skill' (on base: 'Node').

I have followed all the instructions for using player singletons here and code executed elsewhere that utilizes this enum appears to work fine; why is this happening?

That all seems correct to me. So, you’re only having an issue with this enum in a single scene? Is there something unique about that scene that could point to the problem?

jgodfrey | 2023-02-05 00:19

Nothing, except that it’s part of a scene that’s instanced later instead of loaded at launch.

I can make it work if I do the following:

const SkillMoveType = preload("res://skill_move.gd").Skill

func _process(_delta):
    var backflip_enum = SkillMoveType.Backflip

… however this defeats the purpose of preloading the script in the first place.

magicwings | 2023-02-05 00:31

Test run it by adding a dummy scene to the tree and

func _ready(): 
    var test = Skillmove.Backflip     

Also double check SkillMove vs Skillmove in the name of the autoload symbol.

LeslieS | 2023-02-05 00:42

:bust_in_silhouette: Reply From: Inces

It is pointless to use autoload for this. You can use any form of static typing.
Option nr 1:
Keep all individual enums inside corresponding classes
Option nr 2:
Create enums class, that will only contain all constant data for other nodes to use or for autocomplecion purposes.

Either way, all You have to do is :

extends Node
class_name skillmove
enum Skill {}

Static node doesn’t even need to be loaded as singleton, it simply has to exist anywhere in your filesystem. You refer to it by its class, like var test = skillmove.Skill.Jump