#Observer Not Triggering, if the entity is a ChildOf

8 messages · Page 1 of 1 (latest)

spark belfry
#

I have this 'lobby' UI that spawns a few labels, and a button in a container.

When the Ready button is not spawned as a child of the container, it works fine and I get the "Ready" logs when I click.

Whenever I move it inside the container events no longer seem to work.

Spawning Code:

/// Polls for a GameLobby to be added to a client here, realistically this probably should be RPC based and not straight up replication
fn spawn_lobby_ui(mut cmds: Commands, replicated_lobby: Query<&GameLobby, Added<GameLobby>>, ui_assets: Res<UiAssets>){
    //for each connected player in the lobby show a row
    if replicated_lobby.is_empty() {
        debug!("No Lobbies on client!");
        return;
    } else {
        info!("Ok we should be good to spawn a lobby")
    }

    //Spawn UI + Ready box
    cmds.spawn((Name::new("LobbyUI"), Node{
        width: Val::Percent(50.0),
        height: Val::Percent(80.0),
        top: Val::Percent(10.0),
        left: Val::Percent(25.0),
        display: Display::Flex,
        flex_direction: FlexDirection::Column,
        ..default()
    })).with_children(|rs|{
        //Spawn a label for each connected player
        let lobby = replicated_lobby.single_inner().expect("God said we would have a lobby by now!");
        for player in &lobby.player_data {
            ui_assets.transmute_label_info(&mut rs.spawn(Name::new("Player Lobby Entry")), player.0.to_string());
        }

        //Ready Button
        let mut btn = rs.spawn(Name::new("Ready Button"));
        ui_assets.transmute_button(&mut btn, "Ready");
        btn.observe(|_trigger: Trigger<Pointer<Click>>|{
            info!("Ready");
        });
    });
}

TransmuteButton code:


    fn transmute_button(&self, cmds: &'a mut EntityCommands<'w>, text: impl Into<String>) -> &'a mut EntityCommands<'w> {
        cmds.insert((
                Node {
                    width: Val::Px(200.0),
                    height: Val::Px(50.0),
                    border: UiRect::all(Val::Px(5.0)),
                    justify_content: JustifyContent::Center,
                    align_items: AlignItems::Center,
                    ..default()
                },
                BackgroundColor(css::GOLDENROD.into()),
                Text::new(text.into()),
                self.textfont.clone(),
                self.primary_text_color,
                Button,
                TextLayout::new_with_justify(JustifyText::Center)
            ));

        cmds
    }

If anyone knows what I'm missing here I'd appreciate it. I can't recall having this particular problem before.

weak oxide
#

probably not solving the root issue but does anything change if you slap a Pickable::IGNORE on the lobby ui?

#

this smells like a propagation issue to me but my brain isn't working rn

spark belfry
#

Ah yeah that did it

#

Thank you!

weak oxide
#

of course! I think the "proper" fix would be to take the triggered pointer event and make sure it doesnt propagate, so according to this example there is in fact a macro (#[event(auto_propagate = false)]) to attach to your event to disable propagation by default, though I haven't tried this yet personally.

#

you can also set the trigger to not propagate in the observer system (i forget the exact method) but its like trigger.set_propagate(false).

To my understanding, the reason this is happening is that your event is bubbling up from the button to the parent container, which doesn't itself have an observer for that event.

If all else fails, glad to hear the IGNORE component works!

spark belfry
#

Ah that makes sense, I assumed propagation would trigger everything on the way up the hierarchy, until it's stopped; rather than not trigger anything until it's stopped