You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/faq/code.md
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -277,6 +277,8 @@ Also, you can always put a Mutex<HashMap> into a Rust static and load everything
277
277
278
278
## How do I keep a reference of `Node`?
279
279
280
+
The idiomatic way to maintain a reference to a node in the SceneTree from Rust is to use `Option<Ref<T>>`.
281
+
280
282
For example, in the following GDScript code
281
283
```gdscript
282
284
extends Node
@@ -313,6 +315,51 @@ Note: As `TRef<T>` is a temporary pointer, it will be necessary to get the base
313
315
314
316
This can be done with the [`TRef<T>::claim()`](https://docs.rs/gdnative/latest/gdnative/struct.TRef.html#method.claim) function that will return the persistent version of the pointer that you can store in your class.
315
317
318
+
## What is the Rust equivalent to `onready var` in GDScript
319
+
320
+
Rust does not have a direct equivalent to `onready var`. The most idiomatic workaround with Rust is to use `Option<Ref<T>>` of you need the Godot node type or `Option<Instance<T>>` if you are using a Rust based `NativeClass`.
321
+
322
+
```gdscript
323
+
extends Node
324
+
class_name MyClass
325
+
onready var node = $Node2d
326
+
```
327
+
You would need to use the following code.
328
+
329
+
```rust
330
+
#[derive(NativeClass)]
331
+
#[inherit(Node)]
332
+
#[no_constructor]
333
+
structMyNode {
334
+
node2d:Option<Ref<Node>>
335
+
instance:Option<Ref<MyClass>>,
336
+
}
337
+
338
+
#[methods]
339
+
implMyNode {
340
+
#[export]
341
+
fn_ready(&self, owner:TRef<Node>) {
342
+
// Get an existing child node that is a Godot class.
343
+
letnode2d=owner
344
+
.get_node("Node2D")
345
+
.expect("this node must have a child with the path `Node2D`");
346
+
letnode2d=unsafe { node2d.assume_safe() };
347
+
letnode2d=node2d.cast::<Node2D>();
348
+
self.node2d =Some(node2d.claim());
349
+
// Get an existing child node that is a Rust class.
350
+
letinstance=owner
351
+
.get_node("MyClass")
352
+
.expect("this node must have a child with the path `MyNode2D`");
353
+
letinstance=unsafe { node2d.assume_safe() };
354
+
letinstance=node2d.cast::<Node2D>();
355
+
letinstance=godot_egui.cast_instance::<MyClass>()
356
+
.expect("child `MyNode2D` must be type `MyClass`");
357
+
358
+
self.instance =Some(instance.claim());
359
+
}
360
+
}
361
+
```
362
+
316
363
## What types are supported for passing through the GDNative API?
317
364
318
365
The GDNative API supports any type that implements the [`ToVariant`](https://docs.rs/gdnative/latest/gdnative/core_types/trait.ToVariant.html) and/or [`FromVariant`](https://docs.rs/gdnative/latest/gdnative/core_types/trait.FromVariant.html) traits.
Copy file name to clipboardExpand all lines: src/introduction.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,29 @@ Welcome to the `godot-rust` book! This is a work-in-progress user guide for the
4
4
5
5
If you're new to `godot-rust`, try the [Getting Started](./getting-started.md) tutorial first!
6
6
7
+
For more information about architecture with `godot-rust`, the [GDNative Overview](gdnative-overview.md) gives a broad overview for _how_`godot-rust` can be used with a few different use-cases as well as some indepth information for the underlying API.
8
+
9
+
If have specific code questions that are not covered in the Getting Started guide, please check out the [Frequently Asked Questions](faq.md) or [Recipes](recipes.md) for some additional resources related to configuring `godot-rust`.
10
+
11
+
If you're new to Rust, before getting started, it is highly recommended that you familiarize yourself with all of the concepts outlined in the officially maintained [Rust Book](https://doc.rust-lang.org/book/) before you getting started with `godot-rust`.
12
+
7
13
If you have used earlier versions of `godot-rust` before, see [Migrating from godot-rust 0.8.x](./migrating-0-8.md) for a quick guided tour to the new API.
8
14
15
+
## About godot-rust
16
+
17
+
This project specifically supports the [Rust Programming Language](https://www.rust-lang.org/) bindings to the [GDNative API](https://docs.godotengine.org/en/stable/tutorials/plugins/gdnative/gdnative-cpp-example.html) for the Godot Game Engine.
18
+
19
+
Outside of personal preference, Rust may be a good choice for your game for some of the following reasons:
- The leveraging the [cargo](https://doc.rust-lang.org/stable/cargo/) build system to build the GDNative library.
24
+
- The ability to leverage Rust's crate ecosystem from [crates.io](https://crates.io/).
25
+
26
+
*: Compile time memory and thread safety guarantees only apply to the Rust code. As the user is allowed to call into the Godot engine (C++ code, via GDNative Foreign Function Interface) or into user-defined scripts (GDScript), some of the validity checks are outside godot-rust's control. However, `godot-rust` guides the user by making clear which operations are potentially unsafe.
27
+
28
+
For more information about the `godot-rust` project and how it may be useful to you, please refer to the [FAQ Section](faq/meta.md) for project specific information.
29
+
9
30
## Contributing
10
31
11
32
The source repository for this book is [hosted on GitHub](https://github.com/godot-rust/book).
0 commit comments