-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Implement world.trigger_event remote method #21798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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());
} |
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: