Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to a state in on_enter, and maybe on_exit #12

Open
soanvig opened this issue Jun 23, 2024 · 3 comments
Open

Access to a state in on_enter, and maybe on_exit #12

soanvig opened this issue Jun 23, 2024 · 3 comments

Comments

@soanvig
Copy link

soanvig commented Jun 23, 2024

This is actually self-descriptive.

I need to perform some behavior (insert a component) that depends on the state I'm inserting. If I have no access to the state, then I cannot effectively perform any logic inside on_enter.

I can help on PR.

@Seldom-SE
Copy link
Owner

Seldom-SE commented Jun 24, 2024

This seems reasonable API-wise, but I may significantly change some of the APIs in Bevy 0.14 (because of observers). So I'll get back to this.

For now, you can use the Added filter in a system, or an odd workaround like this:

commands.spawn((
    SomeInitialState,
    StateMachine::default()
        .on_enter::<MyState>(|entity_cmds| entity_cmds.add(|entity: Entity, world: &mut World| {
            if world.get::<MyState>(entity).unwrap().should_also_be_fancy {
                world.entity_mut(entity).insert(Fancy);
            }
        })),
));

@soanvig
Copy link
Author

soanvig commented Jun 24, 2024

I actually did use .command_on_enter as I have only one entity with a given state at the moment, so I can just query it. But thanks for the idea.

Looking forward 0.14 and observers

@Seldom-SE
Copy link
Owner

Ok, I don't plan to make large API changes, so this issue is unblocked. I'd accept a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants