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

Using commands.spawn in a non-startup system causes erratic behavior #440

Closed
Laremere opened this issue Sep 5, 2020 · 2 comments
Closed
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior

Comments

@Laremere
Copy link

Laremere commented Sep 5, 2020

I have a sample which creates two circles of spheres: one in system startup, and one in a normal (?) system.
For each spawn call, I am creating a new material so you can see the order the spheres were constructed in.
The startup system successfully creates a nice range of colors for the spheres:

image

However once the spheres created by spawn_spheres start spawning, things go wonky and the colors change a bunch:
image

The colors mostly just swap around, but occasionally are duplicated.

My expectation is that spawning both in setup and spawn_spheres should behave the same.

I also was experiencing a panic with the code before I simplified (with a different component besides the material), though it seems to be triggered under the same condition, so not creating an issue for that as well.

use bevy::prelude::*;

fn main() {
    App::build()
        .add_resource(R(0.0))
        .add_default_plugins()
        .add_startup_system(setup.system())

        .add_system(spawn_spheres.system())

        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    commands
        .spawn(LightComponents {
            translation: Translation::new(4.0, 8.0, 4.0),
            ..Default::default()
        })
        .spawn(Camera3dComponents {
            	transform: Transform::new_sync_disabled(Mat4::face_toward(
                	Vec3::new(-3.0, 5.0, 8.0),
                	Vec3::new(0.0, 0.0, 0.0),
                	Vec3::new(0.0, 1.0, 0.0),
            )),
            ..Default::default()
        })
        ;


 	let mut r = 0.0;
    while r < std::f32::consts::PI * 2.0 {
		let value = r / (std::f32::consts::PI * 2.0);
		commands.spawn(PbrComponents {
            mesh: meshes.add(Mesh::from(shape::Icosphere {
                subdivisions: 4,
                radius: 0.5,
            })),
            material: materials.add(Color::rgb(value, value, value).into()),
            translation: Translation::new(r.cos() * 4.0,0.0, r.sin() * 4.0),
            ..Default::default()
        });
		r += std::f32::consts::PI / 10.0;
	}
}

struct R(f32);

fn spawn_spheres(
	mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut r: ResMut<R>,
    time: Res<Time>,
) {
	if r.0 * 5.0 + 5.0 < time.seconds_since_startup as f32 {
		if r.0 < std::f32::consts::PI * 2.0 {
			let value = r.0 / (std::f32::consts::PI * 2.0);
			commands.spawn(PbrComponents {
	            mesh: meshes.add(Mesh::from(shape::Icosphere {
	                subdivisions: 4,
	                radius: 0.5,
	            })),
	            material: materials.add(Color::rgb(value, value, value).into()),
	            translation: Translation::new(r.0.cos() * 4.0,2.0, r.0.sin() * 4.0),
	            ..Default::default()
	        });
		}
		r.0 += std::f32::consts::PI / 10.0;
	}
}
@Moxinilian Moxinilian added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events A-Rendering Drawing game state to the screen and removed A-ECS Entities, components, systems, and events labels Sep 5, 2020
@memoryruins
Copy link
Contributor

Thanks for reporting! I think this was fixed recently (similar to #328). With your code, I am able to recreate the issue with version 0.1.3, but with master branch 5f1fef3, it now behaves correctly.

image

@Laremere
Copy link
Author

Laremere commented Sep 5, 2020

Using github latest fixed it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants