Skip to content

Commit 54b1c89

Browse files
committed
Follow-up changes to base()/base_mut()
1 parent 9c3ef55 commit 54b1c89

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/intro/hello-world.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ struct Player {
251251
angular_speed: f64,
252252

253253
#[base]
254-
sprite: Base<Sprite2D>
254+
base: Base<Sprite2D>
255255
}
256256
```
257257

@@ -262,8 +262,8 @@ Let's break this down.
262262
2. The `#[derive]` attribute registers `Player` as a class in the Godot engine.
263263
See [API docs][api-derive-godotclass] for details about `#[derive(GodotClass)]`.
264264

265-
```admonish info
266-
`#[derive(GodotClass)]` _automatically_ registers the class -- you don't need an explicit
265+
```admonish info title="Auto-registration"
266+
`#[derive(GodotClass)]` _automatically_ registers the class -- you don't need an explicit
267267
`add_class()` registration call, or a `.gdns` file as it was the case with GDNative.
268268
269269
Before Godot 4.2, you will need to restart the Godot editor for it to take effect.
@@ -275,17 +275,18 @@ Let's break this down.
275275

276276
4. We define two fields `speed` and `angular_speed` for the logic. These are regular Rust fields, no magic involved. More about their use later.
277277

278-
5. The `#[base]` attribute declares the `sprite` field, which allows `self` to access the base instance (via `base()` or `base_mut()` as Rust does
279-
not have native inheritance).
278+
5. The `#[base]` attribute declares the `base` field, which allows `self` to access the base instance (via composition, as Rust does not have
279+
native inheritance). This enables two methods that can be accessed as `self.base()` and `self.base_mut()` on your type (through an extension
280+
trait).
280281

281282
- The field must have type `Base<T>`.
282-
- `T` must match the declared base class, e.g. `#[class(base=Sprite2D)]` implies `Base<Sprite2D>`.
283-
- The name can be freely chosen. Here it's `sprite`, but `base` is also a common convention.
283+
- `T` must match the declared base class. For example, `#[class(base=Sprite2D)]` implies `Base<Sprite2D>`.
284+
- The name can be freely chosen, but `base` is a common convention.
284285
- You do not _have to_ declare this field. If it is absent, you cannot access the base object from within `self`.
285286
This is often not a problem, e.g. in data bundles inheriting `RefCounted`.
286287

287-
```admonish warning
288-
When adding an instance of your `Player` class to the scene, make sure to select node type `Player` and not its base `Sprite2D`.
288+
```admonish warning title="Correct node type"
289+
When adding an instance of your `Player` class to the scene, make sure to select node type `Player` **and not its base `Sprite2D`**.
289290
Otherwise, your Rust logic will not run.
290291
291292
If Godot fails to load a Rust class (e.g. due to an error in your extension), it may silently replace it with its base class.
@@ -303,13 +304,13 @@ use godot::engine::ISprite2D;
303304

304305
#[godot_api]
305306
impl ISprite2D for Player {
306-
fn init(sprite: Base<Sprite2D>) -> Self {
307+
fn init(base: Base<Sprite2D>) -> Self {
307308
godot_print!("Hello, world!"); // Prints to the Godot console
308309

309310
Self {
310311
speed: 400.0,
311312
angular_speed: std::f64::consts::PI,
312-
sprite
313+
base,
313314
}
314315
}
315316
}
@@ -352,9 +353,9 @@ impl ISprite2D for Player {
352353
GDScript uses property syntax here; Rust requires explicit method calls instead. Also, access to base class methods -- such as `rotate()`
353354
in this example -- is done via the `#[base]` field.
354355

355-
```admonish warning
356-
Make sure you are not using the `self.sprite` field directly. Use `base()` or `base_mut()` instead, otherwise you won't be able to access and call
357-
the base class methods.
356+
```admonish warning title="Direct field access"
357+
Do not use the `self.base` field directly. Use `self.base()` or `self.base_mut()` instead, otherwise you won't be able to access and call
358+
the base class methods.
358359
```
359360

360361
This is a point where you can compile your code, launch Godot and see the result. The sprite should rotate at a constant speed.
@@ -395,8 +396,9 @@ impl ISprite2D for Player {
395396
self.base_mut().translate(velocity * delta as f32);
396397

397398
// or verbose:
398-
// self.base_mut().set_position(
399-
// self.base().position() + velocity * delta as f32
399+
// let this = self.base_mut();
400+
// this.set_position(
401+
// this.position() + velocity * delta as f32
400402
// );
401403
}
402404
}

0 commit comments

Comments
 (0)