#event behavior

19 messages · Page 1 of 1 (latest)

quick karma
#
pub fn update_cells(
    time: Res<Time>,
    mut cells_query: Query<(&mut Transform, Entity, &Velocity), With<DeleteOnReset>>,
    mut delete_cell_event_writer: EventWriter<DeleteCellEvent>,
    mut spawn_cell_event_writer: EventWriter<SpawnCellEvent>,
) {
    println!("starting update");
    let dt = time.delta_secs();
    for (mut cell_transform, cell_entity_handle, cell_velocity) in cells_query.iter_mut() {
        cell_transform.scale += dt * GROWTH_RATE;
        cell_transform.translation += dt * cell_velocity.0;
        if cell_transform.scale.x  >= 2. {
            delete_cell_event_writer.write(DeleteCellEvent(cell_entity_handle));
            let random_direction = Vec3::new(
                rand::random::<f32>() * 2.0 - 1.0,
                rand::random::<f32>() * 2.0 - 1.0,
                rand::random::<f32>() * 2.0 - 1.0,
            ).normalize();
            let opposite_direction = -1.0 * random_direction;
            spawn_cell_event_writer.write(SpawnCellEvent {
                position: cell_transform.translation + random_direction * BASE_CELL_SIZE,
                velocity: random_direction,
            });
            spawn_cell_event_writer.write(SpawnCellEvent {
                position: cell_transform.translation + opposite_direction * BASE_CELL_SIZE,
                velocity: opposite_direction,
            });
        }
    }
    println!("ending update");
}

pub fn spawn_cell_event(mut events: EventReader<SpawnCellEvent>, mut commands: Commands, cell_mesh_handle: Res<CellMeshHandle>, cell_material_handle: Res<CellMaterialHandle>) {
    if events.is_empty() {
        return;
    }
    for event in events.read() {
        println!("Spawning cell!");
        commands.spawn((
            Mesh3d(cell_mesh_handle.0.clone()),
            MeshMaterial3d(cell_material_handle.0.clone()),
            Transform::from_translation(event.position),
            Velocity(event.velocity),
            DeleteOnReset,
        ));
    }
}```
#
ending update
Deleting cell!
Spawning cell!
Spawning cell!
starting update
ending update
Deleting cell!
Spawning cell!
Spawning cell!
starting update
ending update``` this code produces this output. I do not understand how it is possible that the delete and spawn section is called 2 frames in a row. It results in 4 cells instead of the expected 2
#

Its my understanding that bevy ordering goes
update -> events -> next update -> next events. In which case the cell triggering the >= 2.0 condition should not exist on the next update

turns out that for some reason the entity persists 1 frame after its deletion which is weird

wispy cape
#

the ordering purely depends on what order you setup between your systems. Could you show me the part of code where you inserted these systems in the app ?

quick karma
#

.add_systems(Update, (update_cells, spawn_cell_event, delete_cell_event).run_if(in_state(AppState::CellEvo)))

wispy cape
#

here, these events are not ordered compared to each other

#

so their order is going to be arbitrary

#

if this is the expected order, you can fix it using (update_cells, spawn_cell_event, delete_cell_event).chain().run_if(in_state(AppState::CellEvo))

#

(notice the chain())

#

it should help it being more coherent

quick karma
#

the results didnt seem arbitrary, it was always 4 cells spawning

wispy cape
#

arbitrary doesn't mean random

#

but if say, the delete system runs before the update event, things are gonna get weird

#

Another question; how many cells do you start with?

#

i.e. can I see the setup system that prepares the initial cells?

quick karma
wispy cape
#

alright, then I'm curious to see if adding chain() helps

quick karma
#

yeah seems to work, thanks