#How to have component polymorphism with Bevy?

6 messages · Page 1 of 1 (latest)

fallow kindle
#

A common pattern I used in OOP is having interfaces that components can then implement for their own custom behavior. I'd like to use this in ECS for serialization and other reading purposes.

But there are a few issues preventing me from implementing this:

  • Since rust doesn't have a runtime type system, it's not possible to check if a struct implements a trait at runtime and execute a method on that trait.
  • I'm not sure if I can even access all components at runtime, since the it seems bevy only provide ComponentInfo information about the components attached to an entity.

Would something like this be possible in Bevy? I might also be thinking in the wrong direction in terms of ECS.

icy rock
#

I wouldn't be surprised if you can do that with something like a component with a dyn box<something> but it feels like you're fighting the engine and language somewhat. It sounds like you could perhaps acomplish this with enum variants instead?

peak kelp
#

However generally in Rust you're probably better off with a enum, especially if you control the code that defines all the possible behaviours

icy rock
#

That can make some niche cases more Interesting but nothing unmanagable that I can think of

grizzled vector