Skip to content

Commit 9c3ef55

Browse files
committed
Update base composition with base(), base_mut() methods.
Related to: godot-rust/gdext#501
1 parent d2225e0 commit 9c3ef55

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/intro/hello-world.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ 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 composition, as Rust does not have
279-
native inheritance).
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).
280280

281281
- The field must have type `Base<T>`.
282282
- `T` must match the declared base class, e.g. `#[class(base=Sprite2D)]` implies `Base<Sprite2D>`.
@@ -342,7 +342,7 @@ impl ISprite2D for Player {
342342
// In GDScript, this would be:
343343
// rotation += angular_speed * delta
344344

345-
self.sprite.rotate((self.angular_speed * delta) as f32);
345+
self.base_mut().rotate((self.angular_speed * delta) as f32);
346346
// The 'rotate' method requires a f32,
347347
// therefore we convert 'self.angular_speed * delta' which is a f64 to a f32
348348
}
@@ -352,6 +352,11 @@ impl ISprite2D for Player {
352352
GDScript uses property syntax here; Rust requires explicit method calls instead. Also, access to base class methods -- such as `rotate()`
353353
in this example -- is done via the `#[base]` field.
354354

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.
358+
```
359+
355360
This is a point where you can compile your code, launch Godot and see the result. The sprite should rotate at a constant speed.
356361

357362
![rotating sprite][img-sprite-rotating]
@@ -383,15 +388,15 @@ impl ISprite2D for Player {
383388
// var velocity = Vector2.UP.rotated(rotation) * speed
384389
// position += velocity * delta
385390

386-
self.sprite.rotate((self.angular_speed * delta) as f32);
391+
self.base_mut().rotate((self.angular_speed * delta) as f32);
387392

388-
let rotation = self.sprite.get_rotation();
393+
let rotation = self.base().get_rotation();
389394
let velocity = Vector2::UP.rotated(rotation) * self.speed as f32;
390-
self.sprite.translate(velocity * delta as f32);
395+
self.base_mut().translate(velocity * delta as f32);
391396

392397
// or verbose:
393-
// self.sprite.set_position(
394-
// self.sprite.position() + velocity * delta as f32
398+
// self.base_mut().set_position(
399+
// self.base().position() + velocity * delta as f32
395400
// );
396401
}
397402
}
@@ -415,7 +420,7 @@ impl Player {
415420
#[func]
416421
fn increase_speed(&mut self, amount: f64) {
417422
self.speed += amount;
418-
self.sprite.emit_signal("speed_increased".into(), &[]);
423+
self.base_mut().emit_signal("speed_increased".into(), &[]);
419424
}
420425

421426
#[signal]

src/intro/objects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ A few practical examples:
6363

6464
1. Retrieve a node relative to current -- type inferred as `Gd<Node3D>`:
6565
```rust
66-
let child = self.sprite.get_node_as::<Node3D>("Child");
66+
let child = self.base().get_node_as::<Node3D>("Child");
6767
```
6868

6969
2. Load a scene and instantiate it as a `RigidBody2D`:

0 commit comments

Comments
 (0)