Skip to content

Support @export_storage attribute #1183

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

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions godot-core/src/registry/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ pub mod export_info_functions {
// right side are the corresponding property hint. Godot is not always consistent between the two, such
// as `export_multiline` being `PROPERTY_HINT_MULTILINE_TEXT`.
default_export_funcs!(
export_storage => NONE, // Storage exports don't display in the editor.
export_flags_2d_physics => LAYERS_2D_PHYSICS,
export_flags_2d_render => LAYERS_2D_RENDER,
export_flags_2d_navigation => LAYERS_2D_NAVIGATION,
Expand Down
37 changes: 36 additions & 1 deletion godot-macros/src/class/data_models/field_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use std::collections::{HashMap, HashSet};

use crate::util::{KvParser, ListParser};
use crate::util::{ident, KvParser, ListParser};
use crate::ParseResult;

pub struct FieldExport {
Expand All @@ -27,6 +27,10 @@ impl FieldExport {
pub fn to_export_hint(&self) -> Option<TokenStream> {
self.export_type.to_export_hint()
}

pub fn to_export_usage(&self) -> Option<Ident> {
self.export_type.to_export_usage()
}
}

/// Store info from `#[export]` attribute.
Expand All @@ -40,6 +44,20 @@ pub enum ExportType {
/// Can become other property hints, depends on context.
Default,

/// ### GDScript annotations
/// - `@export_storage`
///
/// ### Property hints
/// - `NONE`
///
/// ### Property usage
/// - `STORAGE`
///
/// This is used to indicate that the property should be exported
/// but should not be visible in the editor. Therefore, it does not
/// have a property hint, but uses the `STORAGE` property usage.
Storage,

/// ### GDScript annotations
/// - `@export_range`
///
Expand Down Expand Up @@ -150,6 +168,10 @@ impl ExportType {
/// becomes
/// `#[export(flags/enum = (elem1, elem2 = key2, ...))]`
pub(crate) fn new_from_kv(parser: &mut KvParser) -> ParseResult<Self> {
if parser.handle_alone("storage")? {
return Self::new_storage();
}

if let Some(list_parser) = parser.handle_list("range")? {
return Self::new_range_list(list_parser);
}
Expand Down Expand Up @@ -273,6 +295,10 @@ impl ExportType {
Ok(Self::Default)
}

fn new_storage() -> ParseResult<Self> {
Ok(Self::Storage)
}

fn new_range_list(mut parser: ListParser) -> ParseResult<Self> {
const FLAG_OPTIONS: [&str; 7] = [
"or_greater",
Expand Down Expand Up @@ -404,6 +430,8 @@ impl ExportType {
match self {
Self::Default => None,

Self::Storage => quote_export_func! { export_storage() },

Self::Range {
min,
max,
Expand Down Expand Up @@ -519,6 +547,13 @@ impl ExportType {
Self::ColorNoAlpha => quote_export_func! { export_color_no_alpha() },
}
}

pub fn to_export_usage(&self) -> Option<Ident> {
match self {
Self::Storage => Some(ident("STORAGE")),
_ => None,
}
}
}

/// The dimension of a `@export_flags_{dimension}_{layer}` annotation.
Expand Down
15 changes: 11 additions & 4 deletions godot-macros/src/class/data_models/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ pub fn make_property_impl(class_name: &Ident, fields: &Fields) -> TokenStream {

// Ensure we add a var if the user only provided a `#[export]`.
let var = match (export, var) {
(Some(_), None) => Some(FieldVar {
usage_flags: UsageFlags::InferredExport,
..Default::default()
}),
(Some(export), None) => {
let usage_flags = if let Some(usage) = export.to_export_usage() {
UsageFlags::Custom(vec![usage])
} else {
UsageFlags::InferredExport
};
Some(FieldVar {
usage_flags,
..Default::default()
})
}

(_, var) => var.clone(),
};
Expand Down
6 changes: 5 additions & 1 deletion godot-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ use crate::util::{bail, ident, KvParser};
/// // @export
/// #[export]
/// float: f64,
///
///
/// // @export_storage
/// #[export(storage)]
/// hidden_string: GString,
///
/// // @export_range(0.0, 10.0, or_greater)
/// #[export(range = (0.0, 10.0, or_greater))]
/// range_f64: f64,
Expand Down
4 changes: 4 additions & 0 deletions itest/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ fn generate_property_template(inputs: &[Input]) -> PropertyTests {
TokenStream::new()
} else {
quote! {
#[export(storage)]
export_storage: GString,

#[export(file)]
export_file_array: Array<GString>,
#[export(file)]
Expand Down Expand Up @@ -597,6 +600,7 @@ fn generate_property_template(inputs: &[Input]) -> PropertyTests {

// Only available in Godot 4.3+.
let advanced_exports_4_3 = r#"
@export_storage var export_storage: String
@export_file var export_file_array: Array[String]
@export_file var export_file_parray: PackedStringArray
@export_file("*.txt") var export_file_wildcard_array: Array[String]
Expand Down
22 changes: 12 additions & 10 deletions itest/rust/src/object_tests/property_template_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ fn property_template_test(ctx: &TestContext) {

let mut rust_usage = rust_prop.at("usage").to::<i64>();

// the GDSscript variables are script variables, and so have `PROPERTY_USAGE_SCRIPT_VARIABLE` set.
if rust_usage == PropertyUsageFlags::STORAGE.ord() as i64 {
// `PROPERTY_USAGE_SCRIPT_VARIABLE` does the same thing as `PROPERTY_USAGE_STORAGE` and so
// GDScript doesn't set both if it doesn't need to.
rust_usage = PropertyUsageFlags::SCRIPT_VARIABLE.ord() as i64
// The GDSscript variables are script variables, and so have `PROPERTY_USAGE_SCRIPT_VARIABLE` set.
// Before 4.3, `PROPERTY_USAGE_SCRIPT_VARIABLE` did the same thing as `PROPERTY_USAGE_STORAGE` and
// so GDScript didn't set both if it didn't need to.
if GdextBuild::before_api("4.3") {
if rust_usage == PropertyUsageFlags::STORAGE.ord() as i64 {
rust_usage = PropertyUsageFlags::SCRIPT_VARIABLE.ord() as i64
} else {
rust_usage |= PropertyUsageFlags::SCRIPT_VARIABLE.ord() as i64;
}
} else {
rust_usage |= PropertyUsageFlags::SCRIPT_VARIABLE.ord() as i64;
}
Expand Down Expand Up @@ -102,11 +106,11 @@ fn property_template_test(ctx: &TestContext) {
errors.push(format!(
"mismatch in property {name}:\n GDScript: {gdscript_prop:?}\n Rust: {rust_prop:?}"
));
} /*else {
println!("matching property {name}\n GDScript: {gdscript_prop:?}\n Rust: {rust_prop:?}");
}*/
}
}

rust_properties.free();

assert!(
properties.is_empty(),
"not all properties were matched, missing: {properties:?}"
Expand All @@ -118,6 +122,4 @@ fn property_template_test(ctx: &TestContext) {
errors.len(),
errors.join("\n")
);

rust_properties.free();
}
Loading