-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export enum property with custom int discriminants #764
Comments
Hello, I want to help solving this issue. |
Here's the current implementation for context: /// Hints that an integer, float or string property is an enumerated value to pick in a list.
///
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// use gdnative_core::nativescript::init::property::hint::EnumHint;
///
/// let hint = EnumHint::new(vec!["Foo".into(), "Bar".into(), "Baz".into()]);
/// ```
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct EnumHint {
values: Vec<String>,
}
impl EnumHint {
#[inline]
pub fn new(values: Vec<String>) -> Self {
EnumHint { values }
}
} To answer your question: yes, The internal representation is not that important, but maybe we could add a new method: /// Create a list of values to choose from, each with an associated numerical value.
#[inline]
pub fn with_numbers(values: Vec<(String, i32)>) -> Self {
EnumHint { values }
} |
@clarfonthey The code you posted: enum Dir { Top = -1, Bottom = 1 }
export(Dir) var dir = Dir.Bottom is only syntactic sugar for: const Dir: Dictionary = {
"Top": -1,
"Bottom": 1,
}
export(int, "Top", "Bottom") var dir = Dir["Bottom"] There are no real enums in GDScript. So what you suggest is possible today in godot-rust, however it needs a bit of manual work. See also discussion in this PR for details. So we would need to first design what we concretely want to achieve. Is it exporting Rust enums? |
I found that #546 had discussed very similar topic to this. If we can serialize/deserialize a Rust enum (at least as |
Yes, this is my primary goal -- I'm redesigning some code I wrote in GDScript in Rust, and as a result I'd like to be able to export an enum very similar to the one described. My main issue here is that while I want to internally reference these enum values using integers other than the standard indexed ones, I don't see an easy way to avoid duplicating this code when exporting -- one for the 0, 1 indexing and one for the -1, 1 indexing. I was under the impression that GDScript preserved the values of the variants when exporting but I guess I was wrong |
Yes, it would involve:
So it would be mostly automating the two. You're right, having enum convertible to
Does that mean you wouldn't want this in Rust: #[derive(...)]
enum Dir {
Top = -1,
Bottom = 1,
} but rather something like: #[derive(...)]
enum Dir {
#[export_as = -1]
Top,
#[export_as = 1]
Bottom,
} As written above, we could maybe start with a
GDScript only exports an int. The hint is what makes it look like an enum in the editor (string keys and limited set of values), but in theory you can assign any integer to it. |
Actually, funnily enough, I am using explicit discriminants in this case, but I think allowing for both explicit discriminants and an attribute would be best for extensibility. |
I found that no matter the enum's mapping value, godot editor only serialize enum's value according to the hint string. pub enum Num {
THREE = 3,
FOUR = 4,
} and export a Maybe we need to always serialize enum as string if editing it by godot editor is required? But deserialization may be broken if enum's member is renamed. If we actually need to serialize it as int, the only way I can think of now may be (de)serialize it according to its declaring order. (i.e. @Bromeon Do you have other ideas about how to implement this? or any information I should provide? |
We should probably look at the most minimal problem first, and take it from there. pub enum Num {
THREE = 3,
FOUR = 4,
}
// in a NativeClass
#[property]
my_num: Num A user would expect that the This was also discussed in #544. Concerns:
|
Sorry for the late reply, I have tried to implement that, and I think there are some points need to discuss.
In above example, users actually can see the drop-down list, but in GDScript side, they would see Update: I found that the enum hint can accept hint string like
Agree, I think
strum can be used to serialize string, but I am not sure should I handle the |
Basically, this simple gdscript example can't quite be replicated:
Since, when defining
IntHint::Enum
, you just provide an ordered list of strings, and there's no way to map strings to values. You can still get the result in code with custom setters, but I have a feeling that there's a more direct way to do this by changing theIntHint
struct.The text was updated successfully, but these errors were encountered: