Skip to content

Commit c7ced77

Browse files
authored
README is quite outdated (#55)
Closes #49
1 parent 45ab022 commit c7ced77

File tree

3 files changed

+64
-57
lines changed

3 files changed

+64
-57
lines changed

README.md

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,96 @@
11
# Godot Rust Script
2-
An implementation of the rust programing language as a scripting language for the godot engine based on [godot-rust/gdext](https://github.com/godot-rust/gdext).
2+
An implementation of the rust programing language as a scripting language for the godot 4.x engine based on [godot-rust/gdext](https://github.com/godot-rust/gdext).
33

44
# Important Notice
55

6-
**godot-rust-script is still very experimental and unstable.**
6+
**godot-rust-script is still experimental and undergoes breaking changes from time to time.**
77

8-
This project also currently depends on a slightly modfied fork of [godot-rust/gdext](https://github.com/godot-rust/gdext) and should not be used in combination with any other version.
8+
# Why?
99

10-
# Featues
11-
- use rust as scripts similar to GDScript or CSharp
12-
- hot reload your rust scripts in development mode
13-
- use familiar godot annotations similar to GDScripts annotations for editor integration
10+
The question of why this project exists might arise, and it's a good question. The [godot-rust/gdext](https://github.com/godot-rust/gdext)
11+
project already implements excellent bindings with the engine and provides a good developer experience. If you are just looking to write code
12+
for your Godot project in rust, you most likely are already well served with gdext and definitely do not **need** this library.
1413

15-
# Setup
14+
## When would you want to use `godot-rust-script`?
1615

17-
godot-rust-script comes with two compontents. A script runtime and a library for writing godot script compatible rust structs. Both these components have to be set up.
16+
GDExtension works by allowing dynamic libraries to define their own Godot classes, which inherit directly from an existing class. These
17+
classes inherit all the functionality of their base classes. Nothing more, nothing less. Scripts, on the other hand, offer a bit more
18+
flexibility. While they also define a base class, this is more like a minimally required interface. Scripts are attached to an instance of
19+
an existing class. As long as the instance inherits the required base class, the script is compatible with it. This makes the scripts somewhat
20+
more flexible and provides more compossibility than using plain class-based inheritance. It is up to you to decide if you need this
21+
additional flexibility.
1822

19-
Two sepearte crates are required to make rust scripts work.
23+
# Setup
2024

21-
## Runtime library
25+
To use `godot-rust-script` first follow the basic setup instructions for `gdext`.
2226

23-
rs-runtime/Cargo.toml
27+
## Add Dependency
2428

25-
```toml
26-
[package]
27-
name = "rs-runtime"
28-
version = "0.1.0"
29-
edition = "2021"
29+
The project has to be added as a cargo dependency. At the moment, it is not available on crates.io since it is still under heavy development.
30+
This library currently re-exports the `godot` crate, but adding the `godot` dependency as well is recommended, as this most likely will change in the future.
3031

32+
```toml
3133
[lib]
3234
crate-type = ["cdylib"]
3335

3436
[dependencies]
35-
godot-rust-script = { git = "https://github.com/TitanNano/godot-rust-script.git", branch = "master", features = ["runtime"] }
36-
scripts = { path = "./scripts" }
37+
godot-rust-script = { git = "https://github.com/TitanNano/godot-rust-script.git", branch = "master" }
3738
```
3839

39-
rs-runtime/src/main.rs
40-
41-
```rs
42-
use std::cell::RefCell;
40+
## Bootstrap Script Runtime
4341

44-
use godot::prelude::{gdextension, ExtensionLibrary, InitLevel};
45-
use godot_rust_script::{self, RustScriptExtensionLayer};
42+
The script runtime has to be registered with the engine, as Godot does not know how scripts written in rust should be executed or even that
43+
it's available as a scripting language.
4644

47-
godot_rust_script::setup!(scripts);
45+
For this, a manual implementation of the `godot::init::ExtensionLibrary` trait is required. Initializing and deinitalizing the runtime can then
46+
be achieved via two macro calls. The `init!(...)` macro requires the name / path to a module in your library, which represents the root module
47+
of all available scripts.
4848

49-
struct NativeLib;
50-
51-
thread_local! {
52-
static RUST_SCRIPT_LAYER: RefCell<RustScriptExtensionLayer> = RefCell::new(godot_rust_script::init!());
53-
}
49+
```rs
50+
struct Lib;
5451

5552
#[gdextension]
56-
unsafe impl ExtensionLibrary for NativeLib {
53+
unsafe impl ExtensionLibrary for Lib {
5754
fn on_level_init(level: InitLevel) {
5855
match level {
5956
InitLevel::Core => (),
6057
InitLevel::Servers => (),
61-
InitLevel::Scene => RUST_SCRIPT_LAYER.with_borrow_mut(|layer| layer.initialize()),
58+
InitLevel::Scene => godot_rust_script::init!(scripts),
6259
InitLevel::Editor => (),
6360
}
6461
}
6562

6663
fn on_level_deinit(level: InitLevel) {
6764
match level {
6865
InitLevel::Editor => (),
69-
InitLevel::Scene => RUST_SCRIPT_LAYER.with_borrow_mut(|layer| layer.deinitialize()),
70-
InitLevel::Servers => {}
66+
InitLevel::Scene => godot_rust_script::deinit!(),
67+
InitLevel::Servers => (),
7168
InitLevel::Core => (),
7269
}
7370
}
7471
}
7572
```
7673

77-
## Scripts Library
78-
79-
scripts-lib/Cargo.toml
74+
## Define Scripts Root
8075

81-
```toml
82-
[package]
83-
name = "scripts"
84-
version = "0.1.0"
85-
edition = "2021"
76+
Rust scripts require a root module. All rust modules under this module will be considered as potential scripts.
8677

87-
[lib]
88-
crate-type = ["dylib", "rlib"]
78+
```rs
79+
mod example_script;
8980

90-
[dependencies]
91-
godot-rust-script = { git = "https://github.com/TitanNano/godot-rust-script.git", branch = "master", features = ["scripts"] }
81+
godot_rust_script::define_script_root!();
9282
```
9383

94-
scripts-lib/src/lib.rs
84+
## Write the first Script
9585

96-
```rs
97-
mod example_script;
86+
Godots script system is file-based, which means each of your rust scripts has to go into its own module file. Rust script then uses the name
87+
of your module file (e.g., `player_controller.rs`) to identify the script class / struct inside of it (e.g., `PlayerController`).
9888

99-
godot_rust_script::setup_library!();
100-
```
89+
Currently, all scripts are defined as global classes in the engine. This means each script must have a unique name.
10190

102-
scripts-lib/src/example_script.rs
91+
Scripts are then composed of a `struct` definition and an `impl` block. Public functions inside the impl block will be made available to
92+
other scripting languages and the engine, so they must use Godot compatible types. The same applies to struct fields.
93+
Struct fields can additionally be exported via the `#[export]` attribute, so they show up in the editor inspector.
10394

10495
```rs
10596
use godot_rust_script::{
@@ -109,9 +100,8 @@ use godot_rust_script::{
109100

110101
#[derive(Debug, GodotScript)]
111102
struct ExampleScript {
112-
#[export(exp_easing = ["positive_only"])]
103+
#[export]
113104
pub flag: bool,
114-
//#[export(dir_path, color_no_alpha, global_dir, global_file, multiline)]
115105
pub path: GodotString,
116106
property: Option<Gd<Object>>,
117107
base: Gd<Node3D>,
@@ -132,3 +122,12 @@ impl ExampleScript {
132122
}
133123
}
134124
```
125+
126+
# FAQ
127+
128+
## Can I write / edit scripts in the godot editor?
129+
No, it's currently neither supported nor planned. There are numerous good Rust editors and IDEs, so supporting the language inside
130+
the Godot code editor is not a goal of this project.
131+
132+
## Can I compile my scripts from inside the godot editor?
133+
This is currently not supported.

rust-script/src/interface.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<T: GodotScript, B: Inherits<T::Base> + Inherits<Object>> CastToScript<T> fo
172172
}
173173

174174
#[macro_export]
175-
macro_rules! setup_library {
175+
macro_rules! define_script_root {
176176
() => {
177177
#[no_mangle]
178178
pub fn __godot_rust_script_init(
@@ -202,6 +202,14 @@ macro_rules! setup_library {
202202
};
203203
}
204204

205+
#[deprecated = "Has been renamed to define_script_root!()"]
206+
#[macro_export]
207+
macro_rules! setup_library {
208+
() => {
209+
::godot_rust_script::define_script_root!();
210+
};
211+
}
212+
205213
pub trait GodotScriptEnum: GodotConvert + FromGodot + ToGodot {}
206214

207215
#[macro_export]

tests-scripts-lib/src/lib.rs

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

7-
godot_rust_script::setup_library!();
7+
godot_rust_script::define_script_root!();

0 commit comments

Comments
 (0)