Skip to content

Commit

Permalink
Update base composition with base(), base_mut() methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemiczek committed Jan 5, 2024
1 parent d2225e0 commit 994d05b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions src/intro/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Let's break this down.

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.

5. The `#[base]` attribute declares the `sprite` field, which allows `self` to access the base instance (via composition, as Rust does not have
5. The `#[base]` attribute declares the `sprite` field, which allows `self` to access the base instance (via `base()` or `base_mut()` as Rust does not have
native inheritance).

- The field must have type `Base<T>`.
Expand Down Expand Up @@ -342,7 +342,7 @@ impl ISprite2D for Player {
// In GDScript, this would be:
// rotation += angular_speed * delta

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

```admonish warning
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 the base class methods.
```


This is a point where you can compile your code, launch Godot and see the result. The sprite should rotate at a constant speed.

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

self.sprite.rotate((self.angular_speed * delta) as f32);
self.base_mut().rotate((self.angular_speed * delta) as f32);

let rotation = self.sprite.get_rotation();
let rotation = self.base().get_rotation();
let velocity = Vector2::UP.rotated(rotation) * self.speed as f32;
self.sprite.translate(velocity * delta as f32);
self.base_mut().translate(velocity * delta as f32);

// or verbose:
// self.sprite.set_position(
// self.sprite.position() + velocity * delta as f32
// self.base_mut().set_position(
// self.base().position() + velocity * delta as f32
// );
}
}
Expand All @@ -415,7 +420,7 @@ impl Player {
#[func]
fn increase_speed(&mut self, amount: f64) {
self.speed += amount;
self.sprite.emit_signal("speed_increased".into(), &[]);
self.base_mut().emit_signal("speed_increased".into(), &[]);
}

#[signal]
Expand Down
2 changes: 1 addition & 1 deletion src/intro/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ A few practical examples:

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

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

0 comments on commit 994d05b

Please sign in to comment.