Skip to content

Commit

Permalink
Spaceships predicted despawn behaviour (#808)
Browse files Browse the repository at this point in the history
* fix

* fix
  • Loading branch information
cBournhonesque authored Jan 13, 2025
1 parent 6699c3d commit 6920b23
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
21 changes: 7 additions & 14 deletions examples/spaceships/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use lightyear::shared::replication::components::Controlled;
use tracing::Level;

use lightyear::prelude::client::*;
use lightyear::prelude::server::ReplicationTarget;
use lightyear::prelude::server::{DespawnReplicationCommandExt, ReplicationTarget};
use lightyear::prelude::TickManager;
use lightyear::prelude::*;
use lightyear::shared::ping::diagnostics::PingDiagnosticsPlugin;
Expand Down Expand Up @@ -295,11 +295,10 @@ pub(crate) fn lifetime_despawner(
// if ttl.origin_tick.wrapping_add(ttl.lifetime) > *tick_manager.tick() {
if identity.is_server() {
// info!("Despawning {e:?} without replication");
// commands.entity(e).despawn_without_replication(); // CRASH ?
commands.entity(e).remove::<server::Replicate>().despawn();
commands.entity(e).despawn();
} else {
// info!("Despawning:lifetime {e:?}");
commands.entity(e).despawn_recursive();
commands.entity(e).prediction_despawn();
}
}
}
Expand Down Expand Up @@ -356,12 +355,9 @@ pub(crate) fn process_collisions(
if let Ok((bullet, col, bullet_pos)) = bullet_q.get(contacts.entity1) {
// despawn the bullet
if identity.is_server() {
commands
.entity(contacts.entity1)
.remove::<server::Replicate>()
.despawn();
commands.entity(contacts.entity1).despawn();
} else {
commands.entity(contacts.entity1).despawn_recursive();
commands.entity(contacts.entity1).prediction_despawn();
}
let victim_client_id = player_q
.get(contacts.entity2)
Expand All @@ -377,12 +373,9 @@ pub(crate) fn process_collisions(
}
if let Ok((bullet, col, bullet_pos)) = bullet_q.get(contacts.entity2) {
if identity.is_server() {
commands
.entity(contacts.entity2)
.remove::<server::Replicate>()
.despawn();
commands.entity(contacts.entity2).despawn();
} else {
commands.entity(contacts.entity2).despawn_recursive();
commands.entity(contacts.entity2).prediction_despawn();
}
let victim_client_id = player_q
.get(contacts.entity1)
Expand Down
4 changes: 2 additions & 2 deletions lightyear/src/client/prediction/despawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Command for PredictionDespawnCommand {

// if we are in host server mode, there is no rollback so we can despawn the entity immediately
if world.resource::<ClientConfig>().shared.mode == Mode::HostServer {
world.despawn(self.entity);
world.entity_mut(self.entity).despawn_recursive();
}

if let Ok(mut entity) = world.get_entity_mut(self.entity) {
Expand All @@ -55,7 +55,7 @@ impl Command for PredictionDespawnCommand {
} else if let Some(confirmed) = entity.get::<Confirmed>() {
// TODO: actually we should never despawn directly on the client a Confirmed entity
// it should only get despawned when replicating!
entity.despawn();
entity.despawn_recursive();
} else {
error!("This command should only be called for predicted entities!");
}
Expand Down
2 changes: 1 addition & 1 deletion lightyear/src/client/prediction/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy::prelude::{
use bevy::reflect::Reflect;
use bevy::time::{Fixed, Time};
use parking_lot::RwLock;
use tracing::{debug, error, trace, trace_span};
use tracing::{debug, error, info, trace, trace_span};

use crate::client::components::{Confirmed, SyncComponent};
use crate::client::config::ClientConfig;
Expand Down

0 comments on commit 6920b23

Please sign in to comment.