bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.24k stars 3.58k forks source link

Added a hook in 0.14 and it breaks the order of my commands #14581

Closed ethereumdegen closed 3 months ago

ethereumdegen commented 3 months ago

Bevy version

0.14

 impl Component for InteractionAssertedComponent {
    const STORAGE_TYPE: StorageType = StorageType::Table;

    fn register_component_hooks(hooks: &mut ComponentHooks) {
        // Whenever this component is removed, or an entity with
        // this component is despawned...
        hooks.on_remove(|mut world, targeted_entity, _component_id|{

                //THESE TWO LINES BEING ADDED BREAKS COMMAND ORDERING
           //  let asserted_component = world.get::<InteractionAssertedComponent>(targeted_entity)  ; 
           //  let source = asserted_component.map( |s|  s.source.clone()).flatten() ; //drop the borrow of world ..
           // ----------

            let source = None ;

            let target = targeted_entity;

            let asserted = false;

            let Some(mut interaction_asserted_event_writer) = world.get_resource_mut::<Events<InteractionAssertedEvent>>() 
            else {return};

               info!("assert hook  remove");

            interaction_asserted_event_writer.send(
                InteractionAssertedEvent {
                    source,
                    target,
                    asserted

                }
            );
            //handle_interactable_assertion(&mut world, source, targeted_entity, false);

        });

I have proven that those two lines of code, when added to that hook, break the ordering constraints of commands elsewhere in my bevy codebase. This means that when those two lines of code are commented out , my commands which i define in order A, B actually execute in the next frame in the order B, A which ends up causing a fatal error in my code which i can not rectify without a massive massive massive rewrite.

It is true that commands are supposed to run in the order declared right?

Something bad is happening w this hook.

ethereumdegen commented 3 months ago

I noticed that the hook PR did make modifications to CommandQueue.rs and other command related files so i wonder if it introduced something bad like this

https://github.com/bevyengine/bevy/pull/10839

hymm commented 3 months ago

What are A and B here? Are they systems?

ethereumdegen commented 3 months ago

No, they are commands like command.entity(e).spawn and .despawn_recursive

And they inserted in the command queue in the same system (meaning they SHOULD be run in order that they are declared. But that hook makes them run out of order somehow )

ethereumdegen commented 3 months ago

Okay sorry nope i found the issue. When source was some and asserted was false, it was causing my other game code to do a " double level load " causing my issue. This isnt an issue with bevy but something very complex on my end.