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

EffectSpawner:: has_more_to_spawn to report if particles will be spawned #273

Open
jpedrick opened this issue Feb 13, 2024 · 1 comment
Open
Labels
A - components Change related to an ECS component C - enhancement New feature or request

Comments

@jpedrick
Copy link

jpedrick commented Feb 13, 2024

Describe the solution you'd like
A function describing whether an effect spawner has more particles to spawn without reset() being called.

    /// Get whether the spawner is active.
    ///
    /// Inactive spawners do not spawn any particle.
    pub fn has_more_to_spawn(&self) -> bool {
        if self.active {
            if self.limit.is_finite() {
                self.active
            } else {
                self.spawn_remainder > 1.
            }
        } else {
            self.active
        }
    }

Describe why you want that solution. Is this related to a problem?
I would like to remove expired particle spawners after some time:

#[derive(Component)]
struct ScheduleDespawn{
    timer: Timer
}

fn despawn_explosion_effect( 
    mut commands: Commands,
    effect: Query<(Entity, &EffectSpawner), (With<ExplosionEffect>, Without<ScheduleDespawn>)>,
){
    for (e, spawner) in effect.iter() {
        if !spawner.has_more_to_spawn() {
            commands.entity(e).insert(ScheduleDespawn{
                timer: Timer::new(Duration::from_millis(5000), TimerMode::Once),
            });
        }
    }
}

fn despawn_scheduled(
    mut commands: Commands,
    mut scheduled: Query<(Entity, &mut ScheduleDespawn)>,
    time: Res<Time>,
){
    for (e, mut scheduled_despawn) in scheduled.iter_mut() {
        scheduled_despawn.timer.tick(time.delta());
        if scheduled_despawn.timer.finished() {
            commands.entity(e).despawn();
        }
    }
}

Describe alternatives you've considered
Just despawn entities after a known time limit. This change would just allow users to track or infer fewer parameters themselves.

@djeedai djeedai added C - enhancement New feature or request A - components Change related to an ECS component labels Feb 13, 2024
@jpedrick jpedrick changed the title EffectSpawner::is_active to report if particles will be spawned EffectSpawner:: has_more_to_spawn to report if particles will be spawned Feb 13, 2024
jpedrick added a commit to jpedrick/bevy_hanabi that referenced this issue Feb 13, 2024
@djeedai
Copy link
Owner

djeedai commented Mar 18, 2024

I looked again at this, I'm ok in principle I think, but the proposed implementation looks problematic. An "active" spawner is already defined conceptually as a spawner which ticks, irrelevant of whether or not more particle can be spawned. So a Spawner::rate() for example, which is infinite, would return has_more_to_spawn() == false if paused (set inactive) which feels wrong, if only because in your use case @jpedrick you'd despawn it even though it's not "finished". Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A - components Change related to an ECS component C - enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants