- Edited
I want to declare a global EnemyType
enum, have a class that exports a field of this type and reference the enum in different scripts. This is the best I could come up with, does this look right?
1. Add an autoload script enums.gd
to eventually hold all enums that should be globally available.
# enums.gd
extends Node
enum EnemyType {
unknown,
Baiter,
Bomber,
Lander,
Mutant,
Pod,
Swarmer,
}
2. Autoload this script as Enums
.
3. In script enemy.gd
declare an enemy base class with an export value type
of type Enums.EnemyType
.
# enemy.gd
class_name Enemy extends Node2D
@export var type := Enums.EnemyType.unknown
# [...]
4. Create subclasses of Enemy
and set their type
in the editor. For example class Mutant
:
5. Now somewhere in another script I can write something like this:
func _on_enemy_destroyed(enemy: Enemy):
if enemy.type == Enums.EnemyType.Mutant:
# do something
This all seems to work fine, I just wonder if this is the best way to use global enums? Or am I missing something?
Because in other languages I would declare the EnemyType
enum directly in enemy.gd
but if I do that then I am only able to use the enum in this script and not in other scripts, unless I am mistaken.