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

Adding non-rollbacked children to rollbacked entities break the hierarchy #63

Closed
johanhelsing opened this issue Sep 24, 2023 · 1 comment · Fixed by #74
Closed

Adding non-rollbacked children to rollbacked entities break the hierarchy #63

johanhelsing opened this issue Sep 24, 2023 · 1 comment · Fixed by #74
Labels
bug Something isn't working

Comments

@johanhelsing
Copy link
Collaborator

Describe the bug

I'd like it to be possible to spawn cosmetic entities as children of rolled back entities.

Used to work prior to #31

For instance to:

  • compose a character of several sprites
  • use 3rd-party plugins for particle effects
pub fn spawn_character_particles(
    commands: &mut Commands,
    character_entity: Entity,
    sprites: &Res<SpriteAssets>,
) {
    info!("Adding particles to character: {character_entity:?}");
    commands
        .entity(character_entity)
        .with_children(|character| {
            character
                .spawn((
                    Name::new("JetPackParticles"),
                    ParticleSystemBundle {
                        // transform: Transform::from_xyz()
                        particle_system: ParticleSystem {
                            max_particles: 10_000,
                            texture: ParticleTexture::Sprite(sprites.particle.clone()),
                            spawn_rate_per_second: 40.0.into(),
                            emitter_shape: CircleSegment {
                                radius: 0.0.into(),
                                opening_angle: 0.5,
                                direction_angle: PI * 3. / 2.,
                            }
                            .into(),

                            initial_speed: JitteredValue::jittered(1.0, -0.5..0.5),
                            // acceleration: todo!(),
                            lifetime: JitteredValue::jittered(0.3, -0.1..0.1),
                            color: ColorOverTime::Gradient(Curve::new(vec![
                                CurvePoint::new(Color::WHITE, 0.0),
                                CurvePoint::new(Color::NONE, 1.0),
                            ])),
                            scale: ValueOverTime::Constant(0.0005),
                            looping: true,
                            system_duration_seconds: 10.0,
                            z_value_override: Some(JitteredValue::new(55.0)),
                            // bursts: todo!(),
                            // space: todo!(),
                            ..default()
                        },
                        ..default()
                    },
                    bevy_particle_systems::Playing,
                    JetPackParticles,
                ))
                .log_entity_id("spawned jetpack_particles");
        });
}

Currently, this results in Children containing "dead" entities after the first time a snapshot restore happens.

To Reproduce
Steps to reproduce the behavior:

  1. spawn an entity with .add_rollback(), and a child without .add_rollback()
  2. trigger a rollback
  3. traverse Children hierarchy, it now points to a "dead" entity

Expected behavior

I would expect the entity ids of the children without Rollback components to be left alone (like they used to).

Describe alternatives you've considered

Adding "cosmetic follower entities" in a separate hierarchy branch that contain a reference to the rolled back entity, and have a book-keeping step where entities with dangling refs are despawned, and positions are synced.

Additional context

The problem is that map_entities for unknown entities returns "dead" entities: https://github.com/bevyengine/bevy/blob/8ace2ff9e361dd7ef1bc620b84674def0cb56454/crates/bevy_hierarchy/src/components/children.rs#L26

bevyengine/bevy#6790

@johanhelsing johanhelsing added the bug Something isn't working label Sep 24, 2023
@Illiux
Copy link

Illiux commented Oct 9, 2023

I'm not sure if it's possible in your context, but can you add identity mappings for the entities in question to the map? That way, they'll be preserved during entity mapping instead of being replaced by dead entities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants