Вот объявление enum, которое я использую в GDScript:
enum Motion = { IDLE, STEPPING, DASHING }
Хочу случайно выбирать вариант enum, не перечисляя все возможные варианты, чтобы позже добавлять новые, не редактируя код случайного выбора.
Вот один из опробованных подходов:
Motion.get(randi() % Motion.size())
И другой:
Motion[randi() % Motion.size()]
Оба не работают. Один возвращает Null, другой сообщает "Invalid get index '1' (on base: 'Dictionary')."
Как добиться результата так, чтобы это действительно работало?
Переведено с английского·Показать оригиналHere's the enum declaration I'm using in GDScript:
enum Motion = { IDLE, STEPPING, DASHING }
I'd like to choose an enum variant randomly without listing every possible variant, allowing me to introduce additional variants later without needing to edit the code that makes the random selection.
Here's one approach I've tried:
Motion.get(randi() % Motion.size())
And another:
Motion[randi() % Motion.size()]
Both fail. One returns Null, while the other reports "Invalid get index '1' (on base: 'Dictionary')."
What's a way of achieving this that will actually work?