-
-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add GdDyn<dyn Trait> type #631
Comments
Don't know if exact duplicate, maybe just a different perspective on #426 |
I saw that issue but it talks about abstract classes from the Godot's point of view. I'm asking about rust specific implementation. It is much more limited and should be easier to implement. |
Let's say you have two types that you want to treat polymorphically: #[derive(GodotClass)]
#[class(init)]
struct Monster {
hp: u16,
}
#[derive(GodotClass)]
#[class(init)]
struct Bullet {
is_alive: bool,
} You can use a trait Health {
fn hitpoints(&self) -> u16;
}
// Use polymorphically here
fn is_dead(entity: &dyn Health): bool {
entity.hitpoints() == 0
} 1) impl on
|
@Bromeon, sometimes I want to store objects based on their functionality, not concrete implementations. Just as an example I have a platform (parent object) with multiple types of turrets. All turrets have a common functionality (for example Few remarks:
struct World {
...
}
struct TurretStats {
...
}
// both function have non godot-friendly parameters/results
trait Turret {
fn get_stats_mut(&mut self) -> &mut TurretStats;
fn shoot(&mut self, world: &mut World);
}
#[derive(GodotClass)]
#[class(base = Node3D)]
struct TurretType1 {
}
impl Turret for TurretType1 {
...
}
#[derive(GodotClass)]
#[class(base = Node3D)]
struct TurretType2 {
}
impl Turret for TurretType2 {
...
}
#[derive(GodotClass)]
#[class(base = Node3D)]
struct Platform {
world: World,
turrets: Vec<Gd<...>>, // what do I put here?
} I can have In short what I need is:
|
Continuing with the above I think that
|
Unfortunately, RTTI with trait object ( EDIT: Let's say i got a
You see, |
Request
Currently it is impossible to make
GodotClass
object-safe because of the multiple Rust limitations.Maybe it is possible to create a lighter alternative that doesn't require
Self: Sized
with limited functionality that allows to use trait-objects.Use-case:
I have multiple different objects that have a common trait. At this moment I don't see any rust-safe way to call anything common except
#[func]
(which only takes variants and also a big footgun for safety).Example
The text was updated successfully, but these errors were encountered: