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

Automatically Register Bevy Events #159

Open
ncomendant opened this issue Jan 4, 2024 · 8 comments
Open

Automatically Register Bevy Events #159

ncomendant opened this issue Jan 4, 2024 · 8 comments
Labels
editor core Core functional of editor

Comments

@ncomendant
Copy link

Once issue #158 is resolved, SpaceEditorPlugin should register all of Bevy's events using App::editor_registry_event(). This is similar to how it is done for components.

@ncomendant ncomendant changed the title Pre-register Automattically Register Bevy Events Jan 4, 2024
@ncomendant ncomendant changed the title Automattically Register Bevy Events Automatically Register Bevy Events Jan 4, 2024
@naomijub naomijub self-assigned this Jan 4, 2024
@naomijub naomijub added the editor core Core functional of editor label Jan 4, 2024
@naomijub
Copy link
Collaborator

naomijub commented Jan 6, 2024

This can be an issue due to dependency on Resource.

Dependency on Resource is due to the fact that we need a way to store the event so we can manage it

@rewin123 rewin123 added this to the 0.4 milestone Jan 6, 2024
@ncomendant
Copy link
Author

ncomendant commented Jan 6, 2024

I made some progress by wrapping the events into structs that implemented the needed traits. Other changes regarding registration were made, but here's an example for bevy::input::gamepad::GamepadEvent:

use std::any::TypeId;

use bevy::{
    input::gamepad::{GamepadConnection, GamepadConnectionEvent, GamepadEvent},
    prelude::*,
    reflect::GetTypeRegistration,
};

use crate::prelude::EditorRegistryBevyExt;

pub(crate) trait BevyEvent:
    Event + Default + Resource + Reflect + Send + Clone + 'static + GetTypeRegistration
{
    type T: Event + Clone;

    fn inner_event(&self) -> &Self::T;

    fn inner_path() -> String {
        std::any::type_name::<Self::T>().to_string()
    }

    fn inner_type_id() -> TypeId {
        TypeId::of::<Self::T>()
    }
}

#[derive(Event, Resource, Reflect, Clone)]
pub(crate) struct WrappedGamepadEvent(GamepadEvent);

impl Default for WrappedGamepadEvent {
    fn default() -> Self {
        Self(GamepadEvent::Connection(GamepadConnectionEvent {
            gamepad: Gamepad::new(0),
            connection: GamepadConnection::Disconnected,
        }))
    }
}

impl BevyEvent for WrappedGamepadEvent {
    type T = GamepadEvent;

    fn inner_event(&self) -> &Self::T {
        &self.0
    }
}

pub(crate) fn register_events(app: &mut App) -> &mut App {
    app.editor_registry_bevy_event::<WrappedGamepadEvent>()
        .register_type::<GamepadEvent>()
        .register_type::<GamepadConnectionEvent>()
}

And I was able to get this (with working dispatch!):
image
image

However, I'm struggling to change some of the values:
image

@naomijub
Copy link
Collaborator

naomijub commented Jan 6, 2024

However, I'm struggling to change some of the values:

I think you might need to wrap the subtypes as well or this type depends on something else that is not available (like GamepadButtonChangedEvent)

@naomijub naomijub assigned ncomendant and unassigned naomijub Jan 6, 2024
@naomijub
Copy link
Collaborator

naomijub commented Jan 6, 2024

This is the way I thought about doing it as well and how we did with XPBD

@ncomendant
Copy link
Author

You're probably right. I have this right now:

pub(crate) fn register_events(app: &mut App) -> &mut App {
    app.editor_registry_bevy_event::<WrappedGamepadEvent>()
        .register_type::<GamepadEvent>()
        .register_type::<GamepadConnection>()
        .register_type::<GamepadConnectionEvent>()
        .register_type::<GamepadButtonChangedEvent>()
        .register_type::<GamepadAxisChangedEvent>()
        .register_type::<GamepadInfo>()
}

But still no luck. I'll come back to this later.

@ncomendant ncomendant removed their assignment Jan 6, 2024
@ncomendant
Copy link
Author

Seems like all the subtypes need #[reflect(Default)] to work properly. Wrapping everything to accomplish this would be a very brittle solution. I also need a better understanding of how bevy_reflect works. I'll keep thinking about it, but it's best to unassign myself for now.

@naomijub
Copy link
Collaborator

naomijub commented Jan 6, 2024

Agreed to the brittleness, but that is the way I also thought about it. Sadly, every new bevy update would mean A LOT OF CHANGES

@naomijub
Copy link
Collaborator

naomijub commented Jan 7, 2024

@ncomendant can you merge your current work into this branch?

issue-159

@ncomendant ncomendant mentioned this issue Jan 7, 2024
@rewin123 rewin123 modified the milestones: 0.4, 0.5 Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor core Core functional of editor
Projects
None yet
Development

No branches or pull requests

3 participants