Skip to content
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
20 changes: 12 additions & 8 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# The godot-rust book

This book is a user guide for **godot-rust**, the Rust bindings to Godot 4.
It covers a large part of the concepts and complements [the API docs][gdext-docs].
There is also [gdnative-book] for Godot 3.
This book is a user guide for **godot-rust**, the Rust bindings to Godot 4.[^gdnative-book]
It covers a large part of the concepts and complements [the API docs][api-docs].

> [!Tip]
> The book is deployed at **[godot-rust.github.io/book][book-web]**.


## Local setup

The book is built with [mdBook] and the plugins [mdbook-toc] and [mdbook-admonish]. To install them and build the book locally, you can run:
The book is built with [mdBook] and the plugins [mdbook-toc] and [mdbook-admonish].[^mdbook]
To install them and build the book locally, you can run:

```bash
cargo install mdbook mdbook-toc mdbook-admonish
cargo install mdbook@^0.4 mdbook-toc@^0.14 mdbook-admonish@1.18.0
mdbook build
```

Expand Down Expand Up @@ -51,7 +51,7 @@ oxipng --strip safe --alpha -r src
## Contributing

This repository is for documentation only. For changes in the library itself, please open pull requests and issues in the [main repo][gdext],
and read the [contributing guidelines][gdext-contribute].
and read the [contributing guidelines][contribute].


## License
Expand All @@ -60,8 +60,8 @@ Like godot-rust itself, the godot-rust book is licensed under [MPL 2.0][mpl].

[book-web]: https://godot-rust.github.io/book
[gdext]: https://github.com/godot-rust/gdext
[gdext-docs]: https://godot-rust.github.io/docs/gdext/master/godot
[gdext-contribute]: https://github.com/godot-rust/gdext/blob/master/Contributing.md
[api-docs]: https://godot-rust.github.io/docs/gdext/master/godot
[contribute]: https://github.com/godot-rust/gdext/blob/master/Contributing.md
[gdnative-book]: https://github.com/godot-rust/gdnative-book
[markdownlint]: https://github.com/DavidAnson/markdownlint
[mdbook-admonish]: https://github.com/tommilligan/mdbook-admonish
Expand All @@ -70,3 +70,7 @@ Like godot-rust itself, the godot-rust book is licensed under [MPL 2.0][mpl].
[mpl]: https://www.mozilla.org/en-US/MPL
[oxipng]: https://github.com/shssoichiro/oxipng

<br>

[^gdnative-book]: For Godot 3, there is the older [gdnative-book].
[^mdbook]: mdBook 0.5 contains breaking changes, and we can't support it until plugins catch up. Right now it offers no features that would benefit us.
23 changes: 17 additions & 6 deletions src/register/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,31 @@ You can also declare both attributes on the same field. This is in fact necessar

## Enums

You can export Rust enums as properties. An exported enum appears as a drop-down field in the editor, with all available options.
You can export Rust enums as properties, as long as they are C style enums (no associated data per variant).
An exported enum appears as a drop-down field in the editor, with all available options.
In order to do that, you need to derive three traits:

- `GodotConvert` to define how the type is converted from/to Godot.
- `Var` to allow using it as a `#[var]` property, so it can be accessed from Godot.
- `Export` to allow using it as a `#[export]` property, so it appears in the editor UI.
- [`GodotConvert`][api-godotconvert] to define how the type is converted from/to Godot.
- [`Var`][api-var] to allow using it as a `#[var]` property, so it can be accessed from Godot.
- [`Export`][api-export] to allow using it as a `#[export]` property, so it appears in the editor UI.

Additionally, it can make sense to derive following standard traits:

- `Clone`: it is possible without, but then you need to implement `Var` manually. The reason is that `Var` also provides Rust getters when
used with `#[var(pub)]`, which require cloning the value.
- `Default`: if there is a meaningful default value. If not, use `#[init(val = ...)]` on the field declarations to provide an initial value.

Godot does not have dedicated enum types, so you can map them either as integers (e.g. `i64`) or strings (`GString`). This can be
configured using the `via` key of the `#[godot]` attribute.

Exporting an enum can be done as follows:

```rust
#[derive(GodotConvert, Var, Export)]
#[derive(GodotConvert, Var, Export, Default, Clone)]
#[godot(via = GString)]
pub enum Planet {
Earth, // first enumerator is default.
#[default] // Rust standard attribute, not godot-rust.
Earth,
Mars,
Venus,
}
Expand Down Expand Up @@ -210,6 +218,9 @@ As a general rule, try to stay close to Godot's own types, e.g. `Array`, `Dictio
```


[api-godotconvert]: https://godot-rust.github.io/docs/gdext/master/godot/meta/trait.GodotConvert.html
[api-var]: https://godot-rust.github.io/docs/gdext/master/godot/register/property/trait.Var.html
[api-export]: https://godot-rust.github.io/docs/gdext/master/godot/register/property/trait.Export.html
[api-derive-export]: https://godot-rust.github.io/docs/gdext/master/godot/register/derive.Export.html
[api-derive-var]: https://godot-rust.github.io/docs/gdext/master/godot/register/derive.Var.html
[api-var-export]: https://godot-rust.github.io/docs/gdext/master/godot/register/derive.GodotClass.html#properties-and-exports
Expand Down
Loading