Skip to content

Commit a46ccad

Browse files
authored
Merge pull request #43 from jacobsky/introtext
Did a second pass at the intro-page of the book.
2 parents 983c09f + 3af8ddf commit a46ccad

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/faq/code.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ Also, you can always put a Mutex<HashMap> into a Rust static and load everything
277277

278278
## How do I keep a reference of `Node`?
279279

280+
The idiomatic way to maintain a reference to a node in the SceneTree from Rust is to use `Option<Ref<T>>`.
281+
280282
For example, in the following GDScript code
281283
```gdscript
282284
extends Node
@@ -313,6 +315,51 @@ Note: As `TRef<T>` is a temporary pointer, it will be necessary to get the base
313315

314316
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.
315317

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+
struct MyNode {
334+
node2d: Option<Ref<Node>>
335+
instance: Option<Ref<MyClass>>,
336+
}
337+
338+
#[methods]
339+
impl MyNode {
340+
#[export]
341+
fn _ready(&self, owner: TRef<Node>) {
342+
// Get an existing child node that is a Godot class.
343+
let node2d = owner
344+
.get_node("Node2D")
345+
.expect("this node must have a child with the path `Node2D`");
346+
let node2d = unsafe { node2d.assume_safe() };
347+
let node2d = node2d.cast::<Node2D>();
348+
self.node2d = Some(node2d.claim());
349+
// Get an existing child node that is a Rust class.
350+
let instance = owner
351+
.get_node("MyClass")
352+
.expect("this node must have a child with the path `MyNode2D`");
353+
let instance = unsafe { node2d.assume_safe() };
354+
let instance = node2d.cast::<Node2D>();
355+
let instance = godot_egui.cast_instance::<MyClass>()
356+
.expect("child `MyNode2D` must be type `MyClass`");
357+
358+
self.instance = Some(instance.claim());
359+
}
360+
}
361+
```
362+
316363
## What types are supported for passing through the GDNative API?
317364

318365
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.

src/introduction.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,29 @@ Welcome to the `godot-rust` book! This is a work-in-progress user guide for the
44

55
If you're new to `godot-rust`, try the [Getting Started](./getting-started.md) tutorial first!
66

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+
713
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.
814

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:
20+
- Native levels of performance.
21+
- Memory safety validated at compile time.*
22+
- [Fearless Concurrency](https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html).*
23+
- 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+
930
## Contributing
1031

1132
The source repository for this book is [hosted on GitHub](https://github.com/godot-rust/book).

0 commit comments

Comments
 (0)