From 0aceeb79eaacd2a43dad46d1cbec1ba9b293560f Mon Sep 17 00:00:00 2001 From: InvalidArgument3 Date: Fri, 14 Jun 2024 18:13:35 -0500 Subject: [PATCH] attempt 2 --- .../AsteroidEntities/AsteroidEntity.cs | 25 ++++++++++- .../Scripts/DynamicAsteroids/MainSession.cs | 42 ++++++------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidEntity.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidEntity.cs index c269b5b1..edea3f37 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidEntity.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidEntity.cs @@ -85,9 +85,30 @@ public static AsteroidEntity CreateAsteroid(Vector3D position, float size, Vecto var ent = new AsteroidEntity(); Log.Info($"Creating AsteroidEntity at Position: {position}, Size: {size}, InitialVelocity: {initialVelocity}, Type: {type}"); - if (entityId.HasValue) + // Server-side: Generate a unique ID if none is provided + if (MyAPIGateway.Session.IsServer) { - ent.EntityId = entityId.Value; + if (entityId.HasValue) + { + ent.EntityId = entityId.Value; + } + else + { + ent.EntityId = MyEntityIdentifier.AllocateId(); // Generate a unique ID + } + } + else + { + // Client-side: Use the provided ID + if (entityId.HasValue) + { + ent.EntityId = entityId.Value; + } + else + { + Log.Info("Client did not receive a valid entity ID. Skipping asteroid creation."); + return null; + } } try diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs index 15cbe09b..4db61cba 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs @@ -150,34 +150,15 @@ private void OnMessageReceived(byte[] message) var asteroidMessage = MyAPIGateway.Utilities.SerializeFromBinary(message); Log.Info($"Client: Deserialized asteroid message"); - Log.Info($"Client: Received message to create/remove asteroid:"); - Log.Info($"Position: {asteroidMessage.GetPosition()}"); - Log.Info($"Size: {asteroidMessage.Size}"); - Log.Info($"InitialVelocity: {asteroidMessage.GetVelocity()}"); - Log.Info($"AngularVelocity: {asteroidMessage.GetAngularVelocity()}"); - Log.Info($"Type: {asteroidMessage.GetType()}"); - Log.Info($"IsSubChunk: {asteroidMessage.IsSubChunk}"); - Log.Info($"EntityId: {asteroidMessage.EntityId}"); - Log.Info($"IsRemoval: {asteroidMessage.IsRemoval}"); - Log.Info($"IsInitialCreation: {asteroidMessage.IsInitialCreation}"); - Log.Info($"Rotation: {asteroidMessage.GetRotation()}"); - - // Check if asteroid already exists - var existingAsteroid = MyEntities.GetEntityById(asteroidMessage.EntityId) as AsteroidEntity; - if (existingAsteroid != null) - { - Log.Info($"Client: Asteroid with ID {asteroidMessage.EntityId} already exists. Skipping creation."); - return; - } - + // Handle removal of asteroids if (asteroidMessage.IsRemoval) { var asteroid = MyEntities.GetEntityById(asteroidMessage.EntityId) as AsteroidEntity; if (asteroid != null) { Log.Info($"Client: Removing asteroid with ID {asteroidMessage.EntityId}"); + MyEntities.Remove(asteroid); asteroid.Close(); - MyEntities.Remove(asteroid); // Ensure the entity is removed from MyEntities Log.Info($"Client: Removed asteroid with ID {asteroidMessage.EntityId}"); } else @@ -199,9 +180,12 @@ private void OnMessageReceived(byte[] message) asteroidMessage.GetType(), asteroidMessage.GetRotation(), asteroidMessage.EntityId); - asteroid.Physics.AngularVelocity = asteroidMessage.GetAngularVelocity(); - MyEntities.Add(asteroid); - Log.Info($"Client: Created initial asteroid with ID {asteroid.EntityId}"); + if (asteroid != null) + { + asteroid.Physics.AngularVelocity = asteroidMessage.GetAngularVelocity(); + MyEntities.Add(asteroid); + Log.Info($"Client: Created initial asteroid with ID {asteroid.EntityId}"); + } } else { @@ -213,14 +197,12 @@ private void OnMessageReceived(byte[] message) asteroidMessage.GetType(), asteroidMessage.GetRotation(), asteroidMessage.EntityId); - if (asteroid == null) + if (asteroid != null) { - Log.Info("Failed to create asteroid, skipping"); - return; + asteroid.Physics.AngularVelocity = asteroidMessage.GetAngularVelocity(); + MyEntities.Add(asteroid); + Log.Info($"Client: Created asteroid with ID {asteroid.EntityId}"); } - asteroid.Physics.AngularVelocity = asteroidMessage.GetAngularVelocity(); - MyEntities.Add(asteroid); - Log.Info($"Client: Created asteroid with ID {asteroid.EntityId}"); } } catch (Exception ex)