这是我在 GDScript 中使用的枚举声明。
enum Motion = { IDLE, STEPPING, DASHING }
我想随机选择一个枚举变体,而不用列出所有可能的变体。这样以后增加变体时,就不必修改负责随机选择的代码。
我尝试过下面这种方法。
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?