Implement world.trigger_event remote method#21798
Conversation
|
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
|
gave it a brief look. What happens if you use an Otherwise generally seems reasonable and follows the pattern for other such types. |
#[derive(EntityEvent, Reflect)]
#[reflect(Event)]
pub struct Explode(pub Entity);
commands.spawn(Name::new("WontExplode".to_string()));
commands.spawn(Name::new("WillExplode".to_string())).observe(
|event: On<Explode>, mut commands: Commands| {
println!("Boom!");
commands.entity(event.event_target()).despawn();
},
);Sending the event for both entities only explodes "WillExplode", so I guess |
|
I'm trying to pass CI locally but I keep getting impl ReflectEventFns {
/// Get the default set of [`ReflectEventFns`] for a specific event type using its
/// [`FromType`] implementation.
///
/// This is useful if you want to start with the default implementation before overriding some
/// of the functions to create a custom implementation.
pub fn new<'a, T: Event + FromReflect + TypePath>() -> Self
where
T::Trigger<'a>: Default,
{
<ReflectEvent as FromType<T>>::from_type().0
}
}It's right but doesn't warn on the equivalent methods for components, resources, bundles, etc. and they have no usage either according to my IDE. Anything I'm missing? I don't want to |
|
It's probably because |
|
Very sensible idea. Do we have pre-existing tests for this module? If so, we should be testing this too. If not, well, that can wait for follow-up. |
|
Added a test for I'm also trying to write a test for Here's what I had for #[derive(EntityEvent, Reflect)]
#[reflect(Event)]
struct Explode(pub Entity);
#[derive(Component)]
struct DespawnOnExplode;
#[test]
fn trigger_event() {
let mut world = World::new();
let type_registry = AppTypeRegistry::default();
{
let mut registry = type_registry.write();
registry.register::<Explode>();
registry.register_type_data::<Explode, ReflectEvent>();
}
world.insert_resource(type_registry);
let mut system_state: SystemState<Commands> = SystemState::new(&mut world);
let mut commands = system_state.get_mut(&mut world);
let entity = commands
.spawn_empty()
.observe(|event: On<Explode>, mut commands: Commands| {
commands.entity(event.0).despawn()
})
.id();
let entity2 = commands.spawn(DespawnOnExplode).id();
commands.add_observer(
|event: On<Explode>,
entities: Query<Entity, With<DespawnOnExplode>>,
mut commands: Commands| {
for entity in entities.iter() {
commands.entity(entity).despawn()
}
},
);
let mut reflect_event = DynamicTupleStruct::default();
reflect_event.insert(entity.to_bits());
{
let registry = *world.resource::<AppTypeRegistry>().write();
let event =
from_reflect_with_fallback::<Explode>(&reflect_event, &mut world, ®istry);
world.trigger(event);
}
assert!(world.get_entity(entity).is_err());
assert!(world.get_entity(entity2).is_err());
} |
|
So my local CI runs keep flagging unrelated code so I can't verify locally and now the actions CI will fail if I'm missing a Also why are there like 3 different actions ( |
You can add the link in-line, using e.g.
In some ways! Generally our CI is limited by cache size and length of the longest job, not the number of runners. Some of the duplicated builds use slightly different settings as well. Not saying that it's perfect, but just giving you a bit of background. |
|
Thanks! I might be able to get it to pass CI now! Still wondering what I did wrong for local CI to fail on latest main so I'm going off the remote CI's feedback |
Yep, that's totally fine. I sometimes do this myself when it's acting up or I'm feeling lazy. |
AlephCubed
left a comment
There was a problem hiding this comment.
I don't know much about reflection, but this seems pretty straightforward/makes logical sense.
Objective
Tools using
bevy_remotewill be able to identify (via the schema) and trigger events.bevy_ecs/src/reflect/event.rsSolution
I've added a method
world.trigger_event, addedEventto the schema's reflection metadata andReflectEventto allow this.Testing
I have copied the (tested) code from my game but have NOT tested this branch yet.
I am new to Rust/Cargo and need to go to sleep now, I'll figure this out and test it tomorrow.
Showcase
Here's what I needed to add to my game in order to allow my editor to access and trigger an event:
Here's a screenshot of my editor using this feature: