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
@@ -94,13 +96,14 @@ There are two ways to solve it.
94
96
2. Store `data` in a `RefCell<LargeData>` if it should only be accessed from the same thread (such as with signals) or `Mutex<LargeData>` if you need thread-safety. Then you can modify `update_data()` to the following snippet:
// 1. Cast Variant to Ref of associated Godot type, and convert to TRef.
283
286
letmy_node=unsafe {
284
287
my_node
@@ -294,7 +297,7 @@ impl AnotherNativeScript {
294
297
295
298
// 3. Map over the RefInstance to process the underlying user data.
296
299
my_node
297
-
.map(|my_node, _owner| {
300
+
.map(|my_node, _base| {
298
301
// Now my_node is of type MyNode2D.
299
302
})
300
303
.expect("Failed to map over my_node instance");
@@ -397,10 +400,10 @@ struct MyNode {
397
400
398
401
#[methods]
399
402
implMyNode {
400
-
#[export]
401
-
fn_ready(&self, owner:TRef<Node>) {
403
+
#[method]
404
+
fn_ready(&self, #[base] base:TRef<Node>) {
402
405
// Get an existing child node that is a Godot class.
403
-
letnode2d=owner
406
+
letnode2d=base
404
407
.get_node("Node2D")
405
408
.expect("this node must have a child with the path `Node2D`");
406
409
letnode2d=unsafe { node2d.assume_safe() };
@@ -409,7 +412,7 @@ impl MyNode {
409
412
self.node2d =Some(node2d.claim());
410
413
411
414
// Get an existing child node that is a Rust class.
412
-
letinstance=owner
415
+
letinstance=base
413
416
.get_node("MyClass")
414
417
.expect("this node must have a child with the path `MyClass`");
415
418
letinstance=unsafe { instance.assume_safe() };
@@ -447,7 +450,7 @@ For more information about the Godot profiler, please refer to the [official doc
447
450
In order for Godot to profile your function, all the following must be true:
448
451
- The function belongs to a struct that derives `NativeClass`.
449
452
- The function is included in an `impl` block that is attributed with the `#[methods]` attribute.
450
-
- The function is attributed with `#[export]` attribute.
453
+
- The function is attributed with `#[method]` attribute.
451
454
452
455
As such, this method is _only_ useful for exported code and is subject to the Godot profiler's limitations, such as millisecond accuracy in profiler metrics.
Copy file name to clipboardExpand all lines: src/getting-started/hello-world.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,7 +118,7 @@ Unfortunately, this won't compile just yet: Rust will complain about the lack of
118
118
// You may add any number of ordinary `impl` blocks as you want. However, ...
119
119
implHelloWorld {
120
120
/// The "constructor" of the class.
121
-
fnnew(_owner:&Node) ->Self {
121
+
fnnew(_base:&Node) ->Self {
122
122
HelloWorld
123
123
}
124
124
}
@@ -156,24 +156,24 @@ You can now run your project from the editor! If all goes correctly, it should l
156
156
#[methods]
157
157
implHelloWorld {
158
158
159
-
// To make a method known to Godot, use the #[export] attribute.
159
+
// To make a method known to Godot, use the #[method] attribute.
160
160
// In Godot, script "classes" do not actually inherit the parent class.
161
-
// Instead, they are "attached" to the parent object, called the "owner".
161
+
// Instead, they are "attached" to the parent object, called the "base".
162
162
//
163
-
//In order to enable access to the owner, it is passed as the second
164
-
//argument to every single exposed method. As a result, all exposed
165
-
//methods MUST have `owner: &BaseClass` as their second arguments,
166
-
//before all other arguments in the signature.
167
-
#[export]
168
-
fn_ready(&self, _owner:&Node) {
163
+
//If access to the base instance is desired, the 2nd parameter can be
164
+
//annotated with #[base]. It must have type `&T` or `TRef<T>`, where `T`
165
+
//is the base type specified in #[inherit]. If you don't need this parameter,
166
+
//feel free to omit it entirely.
167
+
#[method]
168
+
fn_ready(&self, #[base] base:&Node) {
169
169
// The `godot_print!` macro works like `println!` but prints to the Godot-editor
170
170
// output tab as well.
171
-
godot_print!("Hello, world!");
171
+
godot_print!("Hello world from node {}!", base.to_string());
172
172
}
173
173
}
174
174
```
175
175
176
-
Here, the `#[export]` attribute is used to tell godot-rust to expose your methods to Godot. In this case, we are overriding [`_ready`](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-ready) and printing a line of text.
176
+
Here, the `#[method]` attribute is used to tell godot-rust to expose your methods to Godot. In this case, we are overriding [`_ready`](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-ready) and printing a line of text.
177
177
178
178
Now, re-compile the crate using `cargo build` and copy the resulting binary to the Godot project. Launch the project from the editor, and you should see `Hello, world!` in the Godot console!
Instead, you can wrap the object in a `Instance<Enemy, Unique>`, using `emplace()`. For an in-depth explanation of the `Instance` class, read [this section](../gdnative-overview/wrappers.md#instance-reference-with-attached-rust-class).
58
58
```rust
59
-
#[export]
60
-
fncreate_enemy(
61
-
&self,
62
-
_owner:&Node
63
-
) ->Instance<Enemy, Unique> {
59
+
#[method]
60
+
fncreate_enemy(&self) ->Instance<Enemy, Unique> {
64
61
letenemy=Enemy {
65
62
pos:Vector2::new(7.0, 2.0),
66
63
health:100.0,
@@ -177,7 +174,7 @@ When calling GDScript functions from Rust, a few things need to be kept in mind.
177
174
178
175
**Safety:** Since the calls are dynamic, it is possible to invoke any other functions through them, including unsafe ones like `free()`. As a result, `call()` and its alternatives are unsafe.
179
176
180
-
**Re-entrancy:** When calling from Rust to GDScript, your Rust code is usually already running in an exported `#[export]` method, meaning that it has bound its receiver object via `&T` or `&mut T` reference. In the GDScript code, you must not invoke any method on the same Rust receiver, which would violate safety rules (aliasing of `&mut`).
177
+
**Re-entrancy:** When calling from Rust to GDScript, your Rust code is usually already running in an exported `#[method]` method, meaning that it has bound its receiver object via `&T` or `&mut T` reference. In the GDScript code, you must not invoke any method on the same Rust receiver, which would violate safety rules (aliasing of `&mut`).
Copy file name to clipboardExpand all lines: src/rust-binding/classes.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,15 +52,15 @@ pub struct GodotApi {}
52
52
#[methods]
53
53
implGodotApi {
54
54
// Constructor, either:
55
-
fnnew(owner:&Node) ->Self { ... }
55
+
fnnew(base:&Node) ->Self { ... }
56
56
// or:
57
-
fnnew(owner:TRef<Node>) ->Self { ... }
57
+
fnnew(base:TRef<Node>) ->Self { ... }
58
58
}
59
59
```
60
60
61
61
The [`#[derive(NativeClass)]` macro](https://docs.rs/gdnative/latest/gdnative/derive.NativeClass.html) enables a Rust type to be usable as a _native class_ in Godot. It implements [the `NativeClass` trait](https://docs.rs/gdnative/latest/gdnative/nativescript/trait.NativeClass.html), which fills in the glue code required to make the class available in Godot. Among other information, this includes class name and registry of exported methods and properties. For the user, the utility methods `new_instance()` and `emplace()` are provided for constructing `Instance` objects.
62
62
63
-
The function `new()` corresponds to `_init()` in GDScript. The _owner_ is the base object of the script, and must correspond to the class specified in the `#[inherit]` attribute (or `Reference` if the attribute is absent). The parameter can be a shared reference `&T` or a `TRef<T>`.
63
+
The function `new()` corresponds to `_init()` in GDScript. The _base_ is the base object of the script, and must correspond to the class specified in the `#[inherit]` attribute (or `Reference` if the attribute is absent). The parameter can be a shared reference `&T` or a `TRef<T>`.
64
64
65
65
With a `new()` method, you are able to write `GodotApi.new()` in GDScript. If you don't need this, you can add the `#[no_constructor]` attribute to the struct declaration.
0 commit comments