Derive `GodotClass` for enums
- Dominant language
- Rust
- Stars
- 5.2k
- Forks
- 312
- Avg merge
- 12h 59m
- Merged PRs (30d)
- 9
Description
It should be possible to derive `GodotClass` for rust enums by having it create an abstract base class, and make each enum variant a subclass of that abstract class.
This would be very useful to model things like. You want to export a resource to the editor, and the user can pick between 3 different variants that export should be, which each have different exports.
One example of what this may look like:
```rs
#[derive(GodotClass)]
// Creature inherits from `Resource`
// Every variant has an automatically generated `init` function
#[class(init, base = Resource)]
pub enum Creature {
Enemy {
#[base]
base: Base,
#[export]
damage: f64,
},
Villager {
#[export]
happiness: f64,
},
}
#[godot_api]
impl Creature {
// function defined on the base class of `Creature`
#[func]
fn some_function(&self) {
..
}
}
```
I've found myself wanting something like this several times before. There isn't really a good workaround for it, especially in the case where we want this to inherit from `Resource`. Since we'd want some way to specify that an exported field should be one of a limited set of types.
## Some questions
What would happen if someone makes a new subclass of that abstract base class, is that possible? and if so how should we handle it?
Should we add some way to add exported fields/variants to the base class that will be shared among all variants?
Should there be a way to automatically add a base field to all variants, or should that be explicit?
In GDScript, we could specify that a function can only take/returns one of the variants of the enum, does this cause any issues?
Contributor guide
Assessment
This issue has not been assessed yet.