Hi, first time poster and rust newbie here!
In order to learn graphics programming and rust itself, I’ve decided to try writing a (very small) game engine. All of my former experience in game programming stems almost exclusively from the Unity game engine.
I’m trying to use some of it’s patterns, one of which is the “game object - component” system, where each game object has a list of components of different types all inheriting from the same base class. Each game object has a method
GetComponent<T>();
which returns the component on the game object of the specified type.
My idea for an implementation looked something like this (written in rusty pseudocode):
pub struct GameObject<'w> {
components: Vec<&'w mut Component>,
}
impl<'w> GameObject<'w> {
pub fn components(&self) -> Vec<&'w Component> {
self.components.into_iter().map(|c| &*c).collect()
}
pub fn get_component<T>(&self) -> T
where T: Component
{
let component = self.components().retain(|&c| c.type == T.type)[0]; //need some way to compare types
component as T //and some way to downcast the component
}
}
I think the code should explain my problem, do you guys have any idea how to solve it?