Closed MelvIsntNormal closed 4 years ago
There is no inheritance in Rust, and Rust types are not classes. #[inherit]
is just a way to tell our procedural macro to tell Godot that your script can be attached to an Godot object of a certain type. It isn't anything close to the magic that OO languages perform behind the scenes. Unfortunately, there is no straightforward way to translate an OO class hierarchy to idiomatic Rust. You will have to do some work to re-design your architecture.
For more information on how Rust differs from traditional OO languages in its approach, TRPL has an excellent chapter: https://doc.rust-lang.org/book/ch17-00-oop.html
Thanks for the quick reply, though I feel like I shouldn't have been so imprecise in my language now! I am aware of how Rust works and how it doesn't have classes or inheritance; I used the word "class" to refer to something I could attach to a Node (I'm still struggling to word that properly). I guess that's what I get for being lazy :/
I was hoping to avoid changing things too much since I'm trying to translate a tutorial, but it looks like I will have to work around that now. Again, thanks for the help.
something I could attach to a Node
In GDNative terms we usually call that a "script". The base class to script "inheritance" provided by Godot core is only single-level. Inheritance between GDScript classes, for example, depends on language features instead. Since there is no language-level inheritance in Rust, there is no easy inheritance between script types either.
For your specific case, I think it's fine to simply put knockback_vector
on Hitbox
. The original example is gratuitously "OO" and there is really no need to emulate that in Rust.
I'm trying to port some code from GDScript to Rust, and I'm looking for a way for a class I've written to inherit another class I've written:
Here's my Rust code for
Hitbox
:This is what I've tried for
SwordHitbox
:And I get this error:
I've had a look at
GodotObject
and it seems like I need to implement a couple of methods that I don't know the details of. Is inheriting Rust structs implemented? Is there any documentation I could read on this topic?