如何在不消耗它们的情况下读取 Bevy 事件?
How to read Bevy events without consuming them?
我目前正尝试在我的 Bevy 游戏中角色跳跃时使用事件发出信号。我希望处理玩家输入的系统发送一个 JumpedEvent
,然后其他系统可以接收它以执行适当的操作(设置正确的玩家动画、速度、声音等),但第一个系统读取事件消耗它。
Bevy 是否提供了一种无需消耗事件即可读取事件的方法?
这是我当前的代码:
// Sends a JumpedEvent when the jump key is pressed
fn player_jump_event_system(
keyboard_input: Res<Input<KeyCode>>,
mut jump_events: ResMut<Events<JumpedEvent>>,
mut query: Query<(&Controlled, &mut Jumps)>,
) {
for (controlled, mut jumps) in &mut query.iter() {
if keyboard_input.just_pressed(controlled.jump)
&& jumps.jumps_remaining > 0
{
jumps.jumps_remaining -= 1;
jump_events.send(JumpedEvent {
jump_number: jumps.max_jumps - jumps.jumps_remaining,
});
}
}
}
// This system consumes the JumpedEvent that was sent
fn control_player_jump_system(
jump_events: ResMut<Events<JumpedEvent>>,
mut jump_event_listener: ResMut<JumpedEventListener>,
mut query: Query<(&Controlled, &mut Jumps, &mut Velocity)>,
) {
const PLAYER_JUMP_SPEED: f32 = 450.0;
const MULTI_JUMP_MODIFIER: f32 = 0.9;
// For every jump event, make player jump slightly less high
for jump_event in jump_event_listener.jumped_event_reader.iter(&jump_events)
{
for (_, _, mut velocity) in &mut query.iter() {
*velocity.0.y_mut() = PLAYER_JUMP_SPEED
* MULTI_JUMP_MODIFIER.powi(i32::from(jump_event.jump_number));
}
}
}
// This is the system that cannot receive the event because the above system consumes it
fn select_animation_system(
texture_atlases: Res<Assets<TextureAtlas>>,
jump_events: ResMut<Events<JumpedEvent>>,
mut jump_event_listener: ResMut<JumpedEventListener>,
mut query: Query<(
&Jumps,
&Velocity,
&AnimatedPlayer,
&mut TextureAtlasSprite,
&mut Handle<TextureAtlas>,
)>,
) {
for (_, velocity, animations, mut sprite, mut texture_atlas) in
&mut query.iter()
{
// Check if the player just jumped
let just_jumped = jump_event_listener
.jumped_event_reader
.iter(&jump_events)
.next()
.is_some();
// Omitting irrelevant details...
if just_jumped {
sprite.index = 0;
}
}
}
我刚刚意识到我做错了什么。我正在使用单个全局资源 EventReader
来收听正在发送的 JumpedEvent
个实例。每个 EventReader
只读取每个事件一次。我需要做的是为每个需要侦听事件的系统设置一个单独的 EventReader
。我通过为每个需要侦听事件的系统使用 Local
EventReader
s 来做到这一点。请参阅下面修改后的代码:
// Sends a JumpedEvent when the jump key is pressed
fn player_jump_event_system(
keyboard_input: Res<Input<KeyCode>>,
mut jump_events: ResMut<Events<JumpedEvent>>,
mut query: Query<(&Controlled, &mut Jumps)>,
) {
for (controlled, mut jumps) in &mut query.iter() {
if keyboard_input.just_pressed(controlled.jump)
&& jumps.jumps_remaining > 0
{
jumps.jumps_remaining -= 1;
jump_events.send(JumpedEvent {
jump_number: jumps.max_jumps - jumps.jumps_remaining,
});
}
}
}
// This system consumes the JumpedEvent that was sent
fn control_player_jump_system(
jump_events: ResMut<Events<JumpedEvent>>,
// See that this line now specifies that the resource is local to the system
mut jump_event_listener: Local<JumpedEventListener>,
mut query: Query<(&Controlled, &mut Jumps, &mut Velocity)>,
) {
const PLAYER_JUMP_SPEED: f32 = 450.0;
const MULTI_JUMP_MODIFIER: f32 = 0.9;
// For every jump event, make player jump slightly less high
for jump_event in jump_event_listener.jumped_event_reader.iter(&jump_events)
{
for (_, _, mut velocity) in &mut query.iter() {
*velocity.0.y_mut() = PLAYER_JUMP_SPEED
* MULTI_JUMP_MODIFIER.powi(i32::from(jump_event.jump_number));
}
}
}
// This is the system that cannot receive the event because the above system consumes it
fn select_animation_system(
texture_atlases: Res<Assets<TextureAtlas>>,
jump_events: ResMut<Events<JumpedEvent>>,
// See that this line now specifies that the resource is local to the system
mut jump_event_listener: Local<JumpedEventListener>,
mut query: Query<(
&Jumps,
&Velocity,
&AnimatedPlayer,
&mut TextureAtlasSprite,
&mut Handle<TextureAtlas>,
)>,
) {
for (_, velocity, animations, mut sprite, mut texture_atlas) in
&mut query.iter()
{
// Check if the player just jumped
let just_jumped = jump_event_listener
.jumped_event_reader
.iter(&jump_events)
.next()
.is_some();
// Omitting irrelevant details...
if just_jumped {
sprite.index = 0;
}
}
}
因此,每个 EventReader
在读取每个事件时都会消耗它。因此,要有多个消费系统,每个系统需要一个 Local
reader.
我目前正尝试在我的 Bevy 游戏中角色跳跃时使用事件发出信号。我希望处理玩家输入的系统发送一个 JumpedEvent
,然后其他系统可以接收它以执行适当的操作(设置正确的玩家动画、速度、声音等),但第一个系统读取事件消耗它。
Bevy 是否提供了一种无需消耗事件即可读取事件的方法?
这是我当前的代码:
// Sends a JumpedEvent when the jump key is pressed
fn player_jump_event_system(
keyboard_input: Res<Input<KeyCode>>,
mut jump_events: ResMut<Events<JumpedEvent>>,
mut query: Query<(&Controlled, &mut Jumps)>,
) {
for (controlled, mut jumps) in &mut query.iter() {
if keyboard_input.just_pressed(controlled.jump)
&& jumps.jumps_remaining > 0
{
jumps.jumps_remaining -= 1;
jump_events.send(JumpedEvent {
jump_number: jumps.max_jumps - jumps.jumps_remaining,
});
}
}
}
// This system consumes the JumpedEvent that was sent
fn control_player_jump_system(
jump_events: ResMut<Events<JumpedEvent>>,
mut jump_event_listener: ResMut<JumpedEventListener>,
mut query: Query<(&Controlled, &mut Jumps, &mut Velocity)>,
) {
const PLAYER_JUMP_SPEED: f32 = 450.0;
const MULTI_JUMP_MODIFIER: f32 = 0.9;
// For every jump event, make player jump slightly less high
for jump_event in jump_event_listener.jumped_event_reader.iter(&jump_events)
{
for (_, _, mut velocity) in &mut query.iter() {
*velocity.0.y_mut() = PLAYER_JUMP_SPEED
* MULTI_JUMP_MODIFIER.powi(i32::from(jump_event.jump_number));
}
}
}
// This is the system that cannot receive the event because the above system consumes it
fn select_animation_system(
texture_atlases: Res<Assets<TextureAtlas>>,
jump_events: ResMut<Events<JumpedEvent>>,
mut jump_event_listener: ResMut<JumpedEventListener>,
mut query: Query<(
&Jumps,
&Velocity,
&AnimatedPlayer,
&mut TextureAtlasSprite,
&mut Handle<TextureAtlas>,
)>,
) {
for (_, velocity, animations, mut sprite, mut texture_atlas) in
&mut query.iter()
{
// Check if the player just jumped
let just_jumped = jump_event_listener
.jumped_event_reader
.iter(&jump_events)
.next()
.is_some();
// Omitting irrelevant details...
if just_jumped {
sprite.index = 0;
}
}
}
我刚刚意识到我做错了什么。我正在使用单个全局资源 EventReader
来收听正在发送的 JumpedEvent
个实例。每个 EventReader
只读取每个事件一次。我需要做的是为每个需要侦听事件的系统设置一个单独的 EventReader
。我通过为每个需要侦听事件的系统使用 Local
EventReader
s 来做到这一点。请参阅下面修改后的代码:
// Sends a JumpedEvent when the jump key is pressed
fn player_jump_event_system(
keyboard_input: Res<Input<KeyCode>>,
mut jump_events: ResMut<Events<JumpedEvent>>,
mut query: Query<(&Controlled, &mut Jumps)>,
) {
for (controlled, mut jumps) in &mut query.iter() {
if keyboard_input.just_pressed(controlled.jump)
&& jumps.jumps_remaining > 0
{
jumps.jumps_remaining -= 1;
jump_events.send(JumpedEvent {
jump_number: jumps.max_jumps - jumps.jumps_remaining,
});
}
}
}
// This system consumes the JumpedEvent that was sent
fn control_player_jump_system(
jump_events: ResMut<Events<JumpedEvent>>,
// See that this line now specifies that the resource is local to the system
mut jump_event_listener: Local<JumpedEventListener>,
mut query: Query<(&Controlled, &mut Jumps, &mut Velocity)>,
) {
const PLAYER_JUMP_SPEED: f32 = 450.0;
const MULTI_JUMP_MODIFIER: f32 = 0.9;
// For every jump event, make player jump slightly less high
for jump_event in jump_event_listener.jumped_event_reader.iter(&jump_events)
{
for (_, _, mut velocity) in &mut query.iter() {
*velocity.0.y_mut() = PLAYER_JUMP_SPEED
* MULTI_JUMP_MODIFIER.powi(i32::from(jump_event.jump_number));
}
}
}
// This is the system that cannot receive the event because the above system consumes it
fn select_animation_system(
texture_atlases: Res<Assets<TextureAtlas>>,
jump_events: ResMut<Events<JumpedEvent>>,
// See that this line now specifies that the resource is local to the system
mut jump_event_listener: Local<JumpedEventListener>,
mut query: Query<(
&Jumps,
&Velocity,
&AnimatedPlayer,
&mut TextureAtlasSprite,
&mut Handle<TextureAtlas>,
)>,
) {
for (_, velocity, animations, mut sprite, mut texture_atlas) in
&mut query.iter()
{
// Check if the player just jumped
let just_jumped = jump_event_listener
.jumped_event_reader
.iter(&jump_events)
.next()
.is_some();
// Omitting irrelevant details...
if just_jumped {
sprite.index = 0;
}
}
}
因此,每个 EventReader
在读取每个事件时都会消耗它。因此,要有多个消费系统,每个系统需要一个 Local
reader.