Skip to content

Commit 7aa9e05

Browse files
committed
Documentation
1 parent 94edf15 commit 7aa9e05

File tree

5 files changed

+158
-4
lines changed

5 files changed

+158
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ For this, a manual implementation of the `godot::init::ExtensionLibrary` trait i
4646
be achieved via two macro calls. The `init!(...)` macro requires the name / path to a module in your library, which represents the root module
4747
of all available scripts.
4848

49-
```rs
49+
```rust
5050
struct Lib;
5151

5252
#[gdextension]
@@ -75,7 +75,7 @@ unsafe impl ExtensionLibrary for Lib {
7575

7676
Rust scripts require a root module. All rust modules under this module will be considered as potential scripts.
7777

78-
```rs
78+
```rust
7979
mod example_script;
8080

8181
godot_rust_script::define_script_root!();
@@ -92,7 +92,7 @@ Scripts are then composed of a `struct` definition and an `impl` block. Public f
9292
other scripting languages and the engine, so they must use Godot compatible types. The same applies to struct fields.
9393
Struct fields can additionally be exported via the `#[export]` attribute, so they show up in the editor inspector.
9494

95-
```rs
95+
```rust
9696
use godot_rust_script::{
9797
godot::prelude::{godot_print, Gd, GodotString, Node3D, Object},
9898
godot_script_impl, GodotScript,

derive/src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,60 @@ use type_paths::{godot_types, property_hints, string_name_ty, variant_ty};
1919

2020
use crate::attribute_ops::{FieldExportOps, PropertyOpts};
2121

22+
/// Use this derive macro to create new rust scripts for your projects.
23+
///
24+
/// The macro is desinged to closely align with both godot-rusts [`GodotClass`](https://docs.rs/godot/latest/godot/prelude/derive.GodotClass.html) macro and the GDScript
25+
/// annotations.
26+
///
27+
/// # Top Level Attribute
28+
/// On the struct level the `#[script]` attribute can be used to configure base details of the script.
29+
///
30+
/// ## `#[script(base)]`
31+
/// ```
32+
/// # use ::godot_rust_script::{GodotScript, godot_script_impl};
33+
/// # use ::godot::classes::Node3D;
34+
/// #
35+
/// #[derive(GodotScript, Debug)]
36+
/// #[script(base = Node3D)]
37+
/// struct MyScript {}
38+
///
39+
/// # #[godot_script_impl]
40+
/// # impl MyScript {}
41+
/// ```
42+
///
43+
/// Set the `base` field to specify a base class your script should inherit from. By default all scripts inherit from [`RefCounted`](https://docs.rs/godot/latest/godot/classes/struct.RefCounted.html).
44+
///
45+
/// # Field Level Attributes
46+
/// On the field level you can specify customizations for your script properties. Fields that are private will not be exposed to the engine.
47+
/// Public field on the other hand are exposed to the engine and can be annotated with attributes.
48+
///
49+
/// ## `#[prop]`
50+
/// Use the `#[prop]` attribute to set up getter and setter functions for your properties.
51+
///
52+
/// ```
53+
/// # use godot_rust_script::{GodotScript, godot_script_impl};
54+
/// # use godot::builtin::GString;
55+
/// #
56+
/// #[derive(GodotScript, Debug)]
57+
/// struct MyScript {
58+
/// #[prop(set = Self::set_my_prop, get = Self::get_my_prop)]
59+
/// my_prop: GString,
60+
/// }
61+
///
62+
/// #[godot_script_impl]
63+
/// impl MyScript {
64+
/// fn set_my_prop(&mut self, value: GString) {
65+
/// self.my_prop = value;
66+
/// }
67+
///
68+
/// fn get_my_prop(&self) -> GString {
69+
/// self.my_prop.clone()
70+
/// }
71+
/// }
72+
/// ```
73+
///
74+
/// This attribute optionally accepts a `get` and a `set` field. If these fields are defined they have to be set to a function pointer
75+
/// expression. The expression can contain the `Self` keyword.
2276
#[proc_macro_derive(GodotScript, attributes(export, script, prop, signal))]
2377
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
2478
let input = parse_macro_input!(input as DeriveInput);

rust-script/src/interface.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ pub use crate::runtime::Context;
2020
pub use export::GodotScriptExport;
2121
pub use signals::{ScriptSignal, Signal};
2222

23+
/// The primary trait of this library. This trait must be implemented by a struct to create a new rust script.
24+
///
25+
/// While it is possible, it's not intended that this trait is implemented by hand. Use the [derive macro](derive@crate::GodotScript) to
26+
/// implement this trait.
2327
pub trait GodotScript: Debug + GodotScriptImpl<ImplBase = Self::Base> {
2428
type Base: Inherits<Object>;
2529

@@ -171,6 +175,21 @@ impl<T: GodotScript, B: Inherits<T::Base> + Inherits<Object>> CastToScript<T> fo
171175
}
172176
}
173177

178+
/// Defines the root module for rust scripts. All scripts must be in submodules of the root module.
179+
///
180+
/// There must be a script root module in your project for Godot Rust Script to work. Using multiple root modules is currently not supported.
181+
///
182+
/// # Example
183+
/// ```ignore
184+
/// # use godot_rust_script::define_script_root;
185+
/// // Example script root: src/scripts/mod.rs
186+
///
187+
/// // define your script modules that contain `RustScript` structs.
188+
/// mod player;
189+
/// mod mob;
190+
///
191+
/// define_script_root!();
192+
/// ```
174193
#[macro_export]
175194
macro_rules! define_script_root {
176195
() => {
@@ -202,6 +221,7 @@ macro_rules! define_script_root {
202221
};
203222
}
204223

224+
/// DEPRECATED: This macro has been renamed to [define_script_root].
205225
#[deprecated = "Has been renamed to define_script_root!()"]
206226
#[macro_export]
207227
macro_rules! setup_library {
@@ -212,6 +232,43 @@ macro_rules! setup_library {
212232

213233
pub trait GodotScriptEnum: GodotConvert + FromGodot + ToGodot {}
214234

235+
/// Initialize the rust script runtime. This should be part of your `ExtensionLibrary::on_level_init` function.
236+
///
237+
/// # Example
238+
/// ```
239+
/// # use godot::init::{gdextension, InitLevel, ExtensionLibrary};
240+
/// #
241+
/// # mod scripts {
242+
/// # pub const __GODOT_RUST_SCRIPT_SRC_ROOT: &str = "/dummy/root";
243+
/// #
244+
/// # pub fn __godot_rust_script_init() -> Vec<godot_rust_script::private_export::RustScriptMetaData> {
245+
/// # unimplemented!()
246+
/// # }
247+
/// # }
248+
/// #
249+
/// struct Lib;
250+
///
251+
/// #[gdextension]
252+
/// unsafe impl ExtensionLibrary for Lib {
253+
/// fn on_level_init(level: InitLevel) {
254+
/// match level {
255+
/// InitLevel::Core => (),
256+
/// InitLevel::Servers => (),
257+
/// InitLevel::Scene => godot_rust_script::init!(scripts),
258+
/// InitLevel::Editor => (),
259+
/// }
260+
/// }
261+
///
262+
/// # fn on_level_deinit(level: InitLevel) {
263+
/// # match level {
264+
/// # InitLevel::Editor => (),
265+
/// # InitLevel::Scene => godot_rust_script::deinit!(),
266+
/// # InitLevel::Servers => (),
267+
/// # InitLevel::Core => (),
268+
/// # }
269+
/// # }
270+
/// }
271+
/// ````
215272
#[macro_export]
216273
macro_rules! init {
217274
($scripts_module:tt) => {
@@ -222,6 +279,43 @@ macro_rules! init {
222279
};
223280
}
224281

282+
/// Deinitialize the rust script runtime. This should be part of your `ExtensionLibrary::on_level_deinit` function.
283+
///
284+
/// # Example
285+
/// ```
286+
/// # use godot::init::{gdextension, InitLevel, ExtensionLibrary};
287+
/// #
288+
/// # mod scripts {
289+
/// # pub const __GODOT_RUST_SCRIPT_SRC_ROOT: &str = "/dummy/root";
290+
/// #
291+
/// # pub fn __godot_rust_script_init() -> Vec<godot_rust_script::private_export::RustScriptMetaData> {
292+
/// # unimplemented!()
293+
/// # }
294+
/// # }
295+
/// #
296+
/// struct Lib;
297+
///
298+
/// #[gdextension]
299+
/// unsafe impl ExtensionLibrary for Lib {
300+
/// # fn on_level_init(level: InitLevel) {
301+
/// # match level {
302+
/// # InitLevel::Core => (),
303+
/// # InitLevel::Servers => (),
304+
/// # InitLevel::Scene => godot_rust_script::init!(scripts),
305+
/// # InitLevel::Editor => (),
306+
/// # }
307+
/// # }
308+
/// #
309+
/// fn on_level_deinit(level: InitLevel) {
310+
/// match level {
311+
/// InitLevel::Editor => (),
312+
/// InitLevel::Scene => godot_rust_script::deinit!(),
313+
/// InitLevel::Servers => (),
314+
/// InitLevel::Core => (),
315+
/// }
316+
/// }
317+
/// }
318+
/// ````
225319
#[macro_export]
226320
macro_rules! deinit {
227321
() => {

rust-script/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
*/
66

7+
#![doc = include_str!("../../README.md")]
8+
79
mod apply;
810

911
mod editor_ui_hacks;
1012
mod interface;
1113
mod runtime;
1214
mod static_script_registry;
1315

14-
pub use godot_rust_script_derive::{godot_script_impl, GodotScript, GodotScriptEnum};
16+
pub use godot_rust_script_derive::GodotScript;
17+
pub use godot_rust_script_derive::{godot_script_impl, GodotScriptEnum};
1518
pub use interface::*;
1619
pub use runtime::RustScriptExtensionLayer;
1720

rust-script/src/static_script_registry.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::runtime::GodotScriptObject;
2121

2222
godot::sys::plugin_registry!(pub SCRIPT_REGISTRY: RegistryItem);
2323

24+
#[doc(hidden)]
2425
#[macro_export]
2526
#[cfg(before_api = "4.4")]
2627
macro_rules! register_script_class {
@@ -44,6 +45,7 @@ macro_rules! register_script_class {
4445
};
4546
}
4647

48+
#[doc(hidden)]
4749
#[macro_export]
4850
#[cfg(since_api = "4.4")]
4951
macro_rules! register_script_class {
@@ -66,6 +68,7 @@ macro_rules! register_script_class {
6668
};
6769
}
6870

71+
#[doc(hidden)]
6972
#[macro_export]
7073
macro_rules! register_script_methods {
7174
($class_name:ty, $methods:expr) => {

0 commit comments

Comments
 (0)