Skip to content

Commit f8d8204

Browse files
committed
Improve #[var] + #[export] docs
1 parent edfdc61 commit f8d8204

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

godot-codegen/src/special_cases/special_cases.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,8 @@ pub fn maybe_rename_virtual_method<'m>(
758758
// TODO method-level extra docs, for:
759759
// - Node::rpc_config() -> link to RpcConfig.
760760
// - Node::process/physics_process -> mention `f32`/`f64` duality.
761+
// - Node::duplicate -> to copy #[var] fields, needs STORAGE property usage, or #[export],
762+
// or #[export(storage)] which is #[export] without editor UI.
761763

762764
pub fn get_class_extra_docs(class_name: &TyName) -> Option<&'static str> {
763765
match class_name.godot_ty.as_str() {

godot-macros/src/class/data_models/field_export.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ impl ExportType {
548548
}
549549
}
550550

551+
/// Returns a `PropertyUsageFlags` identifier if this export type has a _usage_.
551552
pub fn to_export_usage(&self) -> Option<Ident> {
552553
match self {
553554
Self::Storage => Some(ident("STORAGE")),

godot-macros/src/lib.rs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ use crate::util::{bail, ident, KvParser};
139139
/// (fields annotated with `@export`). In the gdext API, these two concepts are represented with `#[var]` and `#[export]` attributes respectively,
140140
/// which in turn are backed by the [`Var`](../register/property/trait.Var.html) and [`Export`](../register/property/trait.Export.html) traits.
141141
///
142-
/// ## Property registration
142+
/// ## Register properties -- `#[var]`
143143
///
144-
/// To create a property, you can use the `#[var]` annotation:
144+
/// To create a property, you can use the `#[var]` annotation, which supports types implementing [`Var`](../register/property/trait.Var.html).
145145
///
146146
/// ```
147147
/// # use godot::prelude::*;
@@ -207,9 +207,10 @@ use crate::util::{bail, ident, KvParser};
207207
/// }
208208
/// ```
209209
///
210-
/// ## Property exports
210+
/// ## Export properties -- `#[export]`
211211
///
212-
/// For exporting properties to the editor, you can use the `#[export]` attribute:
212+
/// To export properties to the editor, you can use the `#[export]` attribute, which supports types implementing
213+
/// [`Export`](../register/property/trait.Export.html):
213214
///
214215
/// ```
215216
/// # use godot::prelude::*;
@@ -221,19 +222,21 @@ use crate::util::{bail, ident, KvParser};
221222
/// }
222223
/// ```
223224
///
224-
/// If you don't also include a `#[var]` attribute, then a default one will be generated.
225-
/// `#[export]` also supports all of GDScript's annotations, in a slightly different format. The format is
225+
/// If you don't include an additional `#[var]` attribute, then a default one will be generated.
226+
///
227+
/// `#[export]` also supports all of [GDScript's annotations][gdscript-annotations], in a slightly different format. The format is
226228
/// translated from an annotation by following these four rules:
227229
///
228-
/// - `@export` becomes `#[export]`
229-
/// - `@export_{name}` becomes `#[export(name)]`
230-
/// - `@export_{name}(elem1, ...)` becomes `#[export(name = (elem1, ...))]`
231-
/// - `@export_{flags/enum}("elem1", "elem2:key2", ...)`
232-
/// becomes
233-
/// `#[export(flags/enum = (elem1, elem2 = key2, ...))]`
230+
/// [gdscript-annotations]: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_exports.html
234231
///
232+
/// | GDScript annotation | Rust attribute |
233+
/// |---------------------------------------------|-------------------------------------------------|
234+
/// | `@export` | `#[export]` |
235+
/// | `@export_key` | `#[export(key)]` |
236+
/// | `@export_key(elem1, ...)` | `#[export(key = (elem1, ...))]` |
237+
/// | `@export_flags("elem1", "elem2:val2", ...)`<br>`@export_enum("elem1", "elem2:val2", ...)` | `#[export(flags = (elem1, elem2 = val2, ...))]`<br>`#[export(enum = (elem1, elem2 = val2, ...))]` |
235238
///
236-
/// As an example of some different export attributes:
239+
/// As an example of different export attributes:
237240
///
238241
/// ```
239242
/// # use godot::prelude::*;
@@ -279,9 +282,8 @@ use crate::util::{bail, ident, KvParser};
279282
///
280283
/// ```
281284
///
282-
/// Most values in expressions like `key = value`, can be an arbitrary expression that evaluates to the
283-
/// right value. Meaning you can use constants or variables, as well as any other rust syntax you'd like in
284-
/// the export attributes.
285+
/// Most values in syntax such as `key = value` can be arbitrary expressions. For example, you can use constants, function calls or
286+
/// other Rust expressions that are valid in that context.
285287
///
286288
/// ```
287289
/// # use godot::prelude::*;
@@ -298,18 +300,24 @@ use crate::util::{bail, ident, KvParser};
298300
/// }
299301
/// ```
300302
///
301-
/// You can specify custom property hints, hint strings, and usage flags in a `#[var]` attribute using the
302-
/// `hint`, `hint_string`, and `usage_flags` keys in the attribute. These are constants in the `PropertyHint`
303-
/// and `PropertyUsageFlags` enums, respectively.
303+
/// ## Low-level property hints and usage
304304
///
305-
/// ```
305+
/// You can specify custom property hints, hint strings, and usage flags in a `#[var]` attribute using the `hint`, `hint_string`
306+
/// and `usage_flags` keys in the attribute. Hint and usage flags are constants in the [`PropertyHint`] and [`PropertyUsageFlags`] enums,
307+
/// while hint strings are dependent on the hint, property type and context. Using these low-level keys is rarely necessary, as most common
308+
/// combinations are covered by `#[var]` and `#[export]` already.
309+
///
310+
/// [`PropertyHint`]: ../global/struct.PropertyHint.html
311+
/// [`PropertyUsageFlags`]: ../global/struct.PropertyUsageFlags.html
312+
///
313+
/// ```no_run
306314
/// # use godot::prelude::*;
307315
/// #[derive(GodotClass)]
308316
/// # #[class(init)]
309317
/// struct MyStruct {
310-
/// // Treated as an enum with two values: "One" and "Two"
311-
/// // Displayed in the editor
312-
/// // Treated as read-only by the editor
318+
/// // Treated as an enum with two values: "One" and "Two",
319+
/// // displayed in the editor,
320+
/// // treated as read-only by the editor.
313321
/// #[var(
314322
/// hint = ENUM,
315323
/// hint_string = "One,Two",
@@ -319,9 +327,10 @@ use crate::util::{bail, ident, KvParser};
319327
/// }
320328
/// ```
321329
///
330+
///
322331
/// # Further class customization
323332
///
324-
/// ## Running code in the editor
333+
/// ## Running code in the editor (tool)
325334
///
326335
/// If you annotate a class with `#[class(tool)]`, its lifecycle methods (`ready()`, `process()` etc.) will be invoked in the editor. This
327336
/// is useful for writing custom editor plugins, as opposed to classes running simply in-game.

0 commit comments

Comments
 (0)