-
-
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
No easy way to #[export]
custom types
#227
Comments
As the error message says, the trait TLDR: we should design a proper API for |
#[export]
custom types
Thank you for your response. If I understand it correctly, it currently is possible to export custom types, but only by implementing low-level / internal traits? I.e. implementing |
The thing is, we haven't designed for this feature yet. It may work or not, there's no documentation, stuff may suddenly break and it will be hard to find someone that can support in this case. If you can use other ways of exporting variables, e.g. through getters returning integers, I'd highly recommend to use those for the moment. |
The fact that specifying custom getter/setter that take & return values for the property as a supported type (e.g. Of course, things are subject to change, but I'm willing to work around that :) |
It is now possible to declare default export info for custom types (variant type, property hint, and property hint string). using the as an example: pub enum GenLayer {
Surface = 0,
Underground = 1,
}
impl Export for GenLayer {
fn export(&self) -> Self {
*self
}
fn default_export_info() -> godot::export::ExportInfo {
ExportInfo {
variant_type: VariantType::Int,
hint: PropertyHint::PROPERTY_HINT_ENUM,
hint_string: "Surface,Underground".into(),
}
}
}
#[derive(GodotClass)]
#[class(base=Resource)]
pub struct MapGenLayer {
#[base]
base: Base<Resource>,
#[export(get = get_kind, set = set_kind)]
kind: GenLayer,
} |
Small question for my understanding: this currently wraps and unwraps the value (e.g. |
well this uses a custom setter/getter that takes an i64 (since the type is |
I see. So the example above would actually need the following code, too? #[godot_api]
impl MapGenLayer {
#[func]
pub fn get_kind(&self) -> i64 { ... }
#[func]
pub fn set_kind(&mut self, v: i64) { ... }
} |
yeah |
#309 should fix this, with that PR you can now implement #[derive(Copy, Clone)]
#[repr(i32)]
pub enum Alignment {
Left = 0,
Center = 1,
Right = 2,
}
impl Property for Alignment {
type Intermediate = i32;
fn get_property(&self) -> Self::Intermediate {
self as i32
}
fn set_property(&mut self, value: Self::Intermediate) {
match value {
0 => *self = Self::Left,
1 => *self = Self::Center,
2 => *self = Self::Right,
other => panic!("incorrect value {other}")
}
}
}
impl Export for Alignment {
fn default_export_info() -> ExportInfo {
ExportInfo {
hint: PropertyHint::PROPERTY_HINT_ENUM,
hint_string: "Left,Center,Right".into(),
}
}
}
#[derive(GodotClass)]
#[class(base=Control)]
pub struct Foo {
#[var]
unexported_bar: Alignment,
#[export]
bar: Alignment,
}
#[godot_api]
impl Foo {} |
309: Distinguish between exported and non-exported properties r=Bromeon a=lilizoey Built on top of #311 ## Split `#[export]` into `#[var]` and `#[export]` `var` is now for creating properties, and `export` for exporting to the editor. `export` implies `var`, meaning that the following two field declarations are equivalent: ```rs #[var] #[export] field: SomeType // and #[export] field: SomeType ``` `FieldVar` and `FieldExport` are now the types used to store the information about the `var` and `export` attributes. ## Add support for the various export annotation things like ``@export_range`` and such ## Add a list parser to godot-macros `ListParser` is used to parse values from the `KvParser` into ordered lists of items. We could've reused `KvParser`, but that would require for instance ``@export_range(0.0,` 10.0, or_greater)` to look something like: `#[export(range = (min = 0.0, max = 10.0, or_greater))]` Which to me doesn't seem ideal. I dont think we want people to look up the attribute names of the fields of an export annotation to figure out how to rewrite it in rust. With the list parser the above can instead just become: `#[export(range = (0.0, 10.0, or_greater))]` ## Split up `Export` trait into `Property` and `Export` `Property` has a setter and getter function for generating setters/getters. In addition to an associated type for the intermediate value used to pass objects to and from godot. `Export` is now only for default export info, and functioning as a marker trait for exportable types. ## Add in functions to create `ExportInfo` structs based on each export annotation Used as a typed api to ensure that the user passes in the right values and can in the future be used by the builder api too to ensure compatibility between the two APIs. These are all placed in `godot-core::property::export_info_functions`. ## Add `ExportableObject` for exportable `GodotClass` objects. An object can be exported if it inherits from either `Node` or `Resource`. Unfortunately this means there is no really nice way to create a bound for that using the existing traits. So i made `ExportableObject`, which is automatically implemented by all objects inheriting from one of those two classes. fixes #227 Co-authored-by: Lili Zoey <lili.andersen@nrk.no>
I do not know if this is a bug, but at the very least it is very unintuitive.
This works:
However, this does not (compiler error attached):
The only difference is that the former uses
Array<Alignment>
, while the latter uses rawAlignment
.The error:
out.txt
The text was updated successfully, but these errors were encountered: