use bevy::ecs::component::Component;
use godot::{
classes::Node,
obj::{Gd, Inherits, InstanceId},
};
#[derive(Debug, Component, Clone)]
pub struct GodotRef {
instance_id: InstanceId,
}
impl GodotRef {
pub fn get<T: Inherits<Node>>(&mut self) -> Gd<T> {
self.try_get()
.unwrap_or_else(|| panic!("failed to get godot ref as {}", std::any::type_name::<T>()))
}
pub fn try_get<T: Inherits<Node>>(&mut self) -> Option<Gd<T>> {
Gd::try_from_instance_id(self.instance_id).ok()
}
pub fn new<T: Inherits<Node>>(reference: Gd<T>) -> Self {
Self {
instance_id: reference.instance_id(),
}
}
}