StarArawn / kayak_ui

Other
469 stars 50 forks source link

State entity objects do not remain in the Bevy World #211

Closed madamc closed 1 year ago

madamc commented 1 year ago

In the simple_state example, I added the below system to the Bevy App:

fn checkState(
    query: Query<Entity, With<CurrentCountState>>
) {
    let entity = query.get_single();

    if let Ok(sub_entity) = entity {
        println!("Everything is Fine {}", sub_entity.index());
    } else {
        println!("no component here!");
    }
}

The console prints "no component here!" the whole time.

In my own personal project, I actually can query the state object, but as soon as the render function completes, it's gone.

I'd like to be able to change the state value by querying the state in other systems and modifying it there. Perhaps I'm going about it in the wrong way, though. The state seems to only be accessible from a KayakWidgetContext which the documentation states only can be provisioned via a render or update function as a widget system.

If the above behavior is as intended, I'll close this issue.

StarArawn commented 1 year ago

If the widget is no longer rendered it despawns which removes the state. There are a couple of expectations for example A1 to A2 will share state since they are the same widget.

I think the get_single is the problem. I believe that method expects there to be only 1 entity that matches. Kayak stores a clone of the previous widget in the world which means at a minimum you will have two. This is necessary because we compare the previous values to the current for change detection. You can ignore those entities by filtering them out using the PreviousWidget(I might be mis-remembering the name here) component tag.