Skip to content

Documentation #70

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ thiserror = "1"

godot-rust-script-derive = { path = "derive" }
tests-scripts-lib = { path = "tests-scripts-lib" }

[workspace.lints.rust]
missing_docs = "warn"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ For this, a manual implementation of the `godot::init::ExtensionLibrary` trait i
be achieved via two macro calls. The `init!(...)` macro requires the name / path to a module in your library, which represents the root module
of all available scripts.

```rs
```rust
struct Lib;

#[gdextension]
Expand Down Expand Up @@ -75,7 +75,7 @@ unsafe impl ExtensionLibrary for Lib {

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

```rs
```rust
mod example_script;

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

```rs
```rust
use godot_rust_script::{
godot::prelude::{godot_print, Gd, GodotString, Node3D, Object},
godot_script_impl, GodotScript,
Expand Down
3 changes: 3 additions & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ proc-macro2.workspace = true
quote.workspace = true
syn.workspace = true
itertools.workspace = true

[lints]
workspace = true
57 changes: 57 additions & 0 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#![doc = "Derive macros for the godot-rust-script crate."]

mod attribute_ops;
mod enums;
Expand All @@ -19,6 +20,60 @@ use type_paths::{godot_types, property_hints, string_name_ty, variant_ty};

use crate::attribute_ops::{FieldExportOps, FieldSignalOps, PropertyOpts};

/// Use this derive macro to create new rust scripts for your projects.
///
/// 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
/// annotations.
///
/// # Top Level Attribute
/// On the struct level the `#[script]` attribute can be used to configure base details of the script.
///
/// ## `#[script(base)]`
/// ```
/// # use ::godot_rust_script::{GodotScript, godot_script_impl};
/// # use ::godot::classes::Node3D;
/// #
/// #[derive(GodotScript, Debug)]
/// #[script(base = Node3D)]
/// struct MyScript {}
///
/// # #[godot_script_impl]
/// # impl MyScript {}
/// ```
///
/// 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).
///
/// # Field Level Attributes
/// On the field level you can specify customizations for your script properties. Fields that are private will not be exposed to the engine.
/// Public field on the other hand are exposed to the engine and can be annotated with attributes.
///
/// ## `#[prop]`
/// Use the `#[prop]` attribute to set up getter and setter functions for your properties.
///
/// ```
/// # use godot_rust_script::{GodotScript, godot_script_impl};
/// # use godot::builtin::GString;
/// #
/// #[derive(GodotScript, Debug)]
/// struct MyScript {
/// #[prop(set = Self::set_my_prop, get = Self::get_my_prop)]
/// my_prop: GString,
/// }
///
/// #[godot_script_impl]
/// impl MyScript {
/// fn set_my_prop(&mut self, value: GString) {
/// self.my_prop = value;
/// }
///
/// fn get_my_prop(&self) -> GString {
/// self.my_prop.clone()
/// }
/// }
/// ```
///
/// This attribute optionally accepts a `get` and a `set` field. If these fields are defined they have to be set to a function pointer
/// expression. The expression can contain the `Self` keyword.
#[proc_macro_derive(GodotScript, attributes(export, script, prop, signal))]
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
Expand Down Expand Up @@ -502,6 +557,7 @@ fn derive_signal_metadata(field: &SpannedValue<FieldOpts>) -> (TokenStream, Toke
(metadata, const_assert.unwrap_or_default())
}

/// Attribute for `GodotScript` impls to automatically implement the corret traits.
#[proc_macro_attribute]
pub fn godot_script_impl(
args: proc_macro::TokenStream,
Expand Down Expand Up @@ -544,6 +600,7 @@ fn extract_ident_from_type(impl_target: &syn::Type) -> Result<Ident, TokenStream
}
}

/// Derive macro to auto implement `GodotScriptEnum`
#[proc_macro_derive(GodotScriptEnum, attributes(script_enum))]
pub fn script_enum_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
enums::script_enum_derive(input)
Expand Down
3 changes: 3 additions & 0 deletions rust-script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ godot-bindings.workspace = true
default = ["runtime", "scripts"]
runtime = ["dep:itertools", "dep:rand"]
scripts = ["dep:godot-rust-script-derive"]

[lints]
workspace = true
1 change: 1 addition & 0 deletions rust-script/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#![doc = "Build script for the godot-rust-script crate."]

fn main() {
godot_bindings::emit_godot_version_cfg();
Expand Down
Loading
Loading