@@ -275,8 +275,8 @@ Let's break this down.
275
275
276
276
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.
277
277
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).
280
280
281
281
- The field must have type ` Base<T> ` .
282
282
- ` T ` must match the declared base class, e.g. ` #[class(base=Sprite2D)] ` implies ` Base<Sprite2D> ` .
@@ -342,7 +342,7 @@ impl ISprite2D for Player {
342
342
// In GDScript, this would be:
343
343
// rotation += angular_speed * delta
344
344
345
- self . sprite . rotate ((self . angular_speed * delta ) as f32 );
345
+ self . base_mut () . rotate ((self . angular_speed * delta ) as f32 );
346
346
// The 'rotate' method requires a f32,
347
347
// therefore we convert 'self.angular_speed * delta' which is a f64 to a f32
348
348
}
@@ -352,6 +352,11 @@ impl ISprite2D for Player {
352
352
GDScript uses property syntax here; Rust requires explicit method calls instead. Also, access to base class methods -- such as ` rotate() `
353
353
in this example -- is done via the ` #[base] ` field.
354
354
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
+
355
360
This is a point where you can compile your code, launch Godot and see the result. The sprite should rotate at a constant speed.
356
361
357
362
![ rotating sprite] [ img-sprite-rotating ]
@@ -383,15 +388,15 @@ impl ISprite2D for Player {
383
388
// var velocity = Vector2.UP.rotated(rotation) * speed
384
389
// position += velocity * delta
385
390
386
- self . sprite . rotate ((self . angular_speed * delta ) as f32 );
391
+ self . base_mut () . rotate ((self . angular_speed * delta ) as f32 );
387
392
388
- let rotation = self . sprite . get_rotation ();
393
+ let rotation = self . base () . get_rotation ();
389
394
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 );
391
396
392
397
// 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
395
400
// );
396
401
}
397
402
}
@@ -415,7 +420,7 @@ impl Player {
415
420
#[func]
416
421
fn increase_speed (& mut self , amount : f64 ) {
417
422
self . speed += amount ;
418
- self . sprite . emit_signal (" speed_increased" . into (), & []);
423
+ self . base_mut () . emit_signal (" speed_increased" . into (), & []);
419
424
}
420
425
421
426
#[signal]
0 commit comments