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

Send Priority event from ScriptWorld #74

Open
zwazel opened this issue Sep 30, 2023 · 2 comments
Open

Send Priority event from ScriptWorld #74

zwazel opened this issue Sep 30, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@zwazel
Copy link
Contributor

zwazel commented Sep 30, 2023

Hi, love this crate!

I just came across a small issue.
I need to send out PriorityEvents from a rust function that is registered as a script function.
So it has only access to the ScriptWorld.

With the Normal Bevy Events I can just use world.send_event().
But I can't seem to figure out how to send out priority events with only having the ScriptWorld.

Did I miss anything?

@makspll
Copy link
Owner

makspll commented Oct 7, 2023

Hi @zwazel thanks!
What do you mean by a rust function registered as a script function, am I correct in thinking you're trying to send out events back from the script to the bevy world?
In this case you can do something similar to world.send_event which is defined using world.send_event_batch like so:

    /// Sends a batch of [`Event`]s from an iterator.
    #[inline]
    pub fn send_event_batch<E: Event>(&mut self, events: impl IntoIterator<Item = E>) {
        match self.get_resource_mut::<Events<E>>() {
            Some(mut events_resource) => events_resource.extend(events),
            None => bevy_utils::tracing::error!(
                    "Unable to send event `{}`\n\tEvent must be added to the app with `add_event()`\n\thttps://docs.rs/bevy/*/bevy/app/struct.App.html#method.add_event ",
                    std::any::type_name::<E>()
                ),
        }
    }

in this case instead of Events you'll simply use PriorityEvents like so:

    /// Sends a batch of [`Event`]s from an iterator.
    #[inline]
    pub fn send_event_batch<E: Event>(&mut self, events: impl IntoIterator<Item = E>, prio: u32) {
        match self.get_resource_mut::<PriorityEvents<E>>() {
            Some(mut events_resource) =>events_resource.events.extend(events.map(|v| EventInstance::new(v, prio))),
            None => bevy_utils::tracing::error!(
                    "Unable to send event `{}`\n\tEvent must be added to the app with `add_event()`\n\thttps://docs.rs/bevy/*/bevy/app/struct.App.html#method.add_event ",
                    std::any::type_name::<E>()
                ),
        }
    }

@makspll
Copy link
Owner

makspll commented Oct 7, 2023

I should probably extend the world API with a custom trait containing priority event dispatchers tbh

@makspll makspll added the enhancement New feature or request label Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants