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

Data field should be optional #2

Closed
EmiOnGit opened this issue Mar 19, 2023 · 3 comments
Closed

Data field should be optional #2

EmiOnGit opened this issue Mar 19, 2023 · 3 comments

Comments

@EmiOnGit
Copy link

This is only my opinion so feel free to just close this issue :)

The problem

There are quite a lot of objects that don't need any input to be initialized because they are always initialized in the same way,
or only depend on a resource which would have to be fetched by the world in the spawn function anyway (I guess?).

I think a more idiomatic way would be to model it similar to how Query in bevy are modeled with the default value () if not given otherwise (for the filtering)

@janhohenheim
Copy link
Owner

You mean as in setting the data type to () by default and then requiring the signature of the spawn functions to be only fn spawn_stuffs(world: &mut World)?

@EmiOnGit
Copy link
Author

Yes! As I prepared the example for issue #3 I thought it might have been nicer to not write ()

@janhohenheim
Copy link
Owner

Fixed by #5. Spawning functions are now just regular old Bevy systems with the user data being passed as an In, just like when you do piping.

New minimal example:

use bevy::prelude::*;
use spew::prelude::*;

#[derive(Debug, Eq, PartialEq)]
enum Object {
    Cube,
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(SpewPlugin::<Object>::default())
        .add_spawner((Object::Cube, spawn_cube))
        .add_system(spawn_something.on_startup())
        .run();
}

fn spawn_something(mut spawn_events: EventWriter<SpawnEvent<Object>>) {
    spawn_events.send(SpawnEvent::new(Object::Cube));
}

fn spawn_cube(mut commands: Commands) {
    info!("Spawning cube");
    commands.spawn(Name::new("Cube"));
}

New example with user data:

use bevy::prelude::*;
use spew::prelude::*;

#[derive(Debug, Eq, PartialEq)]
enum Object {
    Cube,
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(SpewPlugin::<Object, Transform>::default())
        .add_spawners(((Object::Cube, spawn_cube_with_transform),))
        .add_system(spawn_something_with_transform.on_startup())
        .run();
}

fn spawn_something_with_transform(mut spawn_events: EventWriter<SpawnEvent<Object, Transform>>) {
    spawn_events.send(SpawnEvent::with_data(
        Object::Cube,
        Transform::from_xyz(1.0, 2.0, 3.0),
    ));
}

fn spawn_cube_with_transform(In(transform): In<Transform>, mut commands: Commands) {
    info!("Spawning cube at {}", transform.translation);
    commands.spawn((Name::new("Cube"), transform));
}

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