@@ -251,7 +251,7 @@ struct Player {
251
251
angular_speed : f64 ,
252
252
253
253
#[base]
254
- sprite : Base <Sprite2D >
254
+ base : Base <Sprite2D >
255
255
}
256
256
```
257
257
@@ -262,8 +262,8 @@ Let's break this down.
262
262
2 . The ` #[derive] ` attribute registers ` Player ` as a class in the Godot engine.
263
263
See [ API docs] [ api-derive-godotclass ] for details about ` #[derive(GodotClass)] ` .
264
264
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
267
267
`add_class()` registration call, or a `.gdns` file as it was the case with GDNative.
268
268
269
269
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.
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 ` 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).
280
281
281
282
- 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.
284
285
- You do not _ have to_ declare this field. If it is absent, you cannot access the base object from within ` self ` .
285
286
This is often not a problem, e.g. in data bundles inheriting ` RefCounted ` .
286
287
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`** .
289
290
Otherwise, your Rust logic will not run.
290
291
291
292
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;
303
304
304
305
#[godot_api]
305
306
impl ISprite2D for Player {
306
- fn init (sprite : Base <Sprite2D >) -> Self {
307
+ fn init (base : Base <Sprite2D >) -> Self {
307
308
godot_print! (" Hello, world!" ); // Prints to the Godot console
308
309
309
310
Self {
310
311
speed : 400.0 ,
311
312
angular_speed : std :: f64 :: consts :: PI ,
312
- sprite
313
+ base ,
313
314
}
314
315
}
315
316
}
@@ -352,9 +353,9 @@ impl ISprite2D for Player {
352
353
GDScript uses property syntax here; Rust requires explicit method calls instead. Also, access to base class methods -- such as ` rotate() `
353
354
in this example -- is done via the ` #[base] ` field.
354
355
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.
358
359
```
359
360
360
361
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 {
395
396
self . base_mut (). translate (velocity * delta as f32 );
396
397
397
398
// 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
400
402
// );
401
403
}
402
404
}
0 commit comments