#Problem Deriving Reflect for `BinaryHeap` containing `Box<dyn MyTrait>`

10 messages · Page 1 of 1 (latest)

pine flame
#
pub trait ScheduledAction: Send + Sync + Reflect {
    fn execute(&mut self, world: &mut World);
}

#[derive(Reflect, Clone)]
enum ScheduleItem {
    PlayerAction,
    // We cant use function traits here because they can't be serialized.
    OneshotEvent(Box<dyn ScheduledAction>),
    TurnBasedEvent(Box<dyn ScheduledAction>),
}
#[derive(Reflect, Default, Clone)]
struct ScheduleEntry {
    time: u64,
    item: ScheduledAction,
}

#[derive(Resource, Reflect)]
#[reflect(Resource)]
pub struct RPGSchedule {
    heap: BinaryHeap<ScheduleEntry>,
}

I have two problems implementing Reflect for my turn based schedule.

  1. For BinaryHeap to implement Reflect ScheduleEntry needs to implement Copy but it can't otherwise it would not be object-safe
  2. I can't implement Reflect for Box<dyn ScheduledAction>

All I want from the scheudled action is to emit one event. My first try was to store events. But they are also not object-safe ...

I would love some input. Either on how to fix this system or on ideas for an alternative.
One obvious thing is I could just use a super fat enum and dispatch on that ... workable but I'd be interested in a "proper" solution.

Thank you for your time!
Cheers
Felix

steady barn
#

where and how are you storing these, assuming they worked?

#

just as a singleton resource?

pine flame
#

They work. Just the reflect does not. The main resource is the RPGSchedule.

pine flame
#

I have changed my implementation yet again ... now every turn event is just an entity in the ecs and I just have to be careful to flush the commands. But the answer to this problem would still interst me.

steady barn
#

i'm looking through the impl PartialReflects for foreign types, and while Box isn't there, VecDeque is
way bigger, but might work

#

oh, Vec<T> apparently too?

#

that's not too bad then, just two extra words

#

... doesn't work, apparently you can't coerce a Vec to dyn

#

there's probably a reason you can't Reflect a dyn