Add proper networked objects destruction upon despawn or shutdown

This commit is contained in:
Wojciech Figat
2022-10-28 11:12:39 +02:00
parent e8d39e706f
commit 2c2071285a
2 changed files with 25 additions and 3 deletions

View File

@@ -18,6 +18,7 @@
#include "Engine/Level/Actor.h" #include "Engine/Level/Actor.h"
#include "Engine/Level/SceneObject.h" #include "Engine/Level/SceneObject.h"
#include "Engine/Profiler/ProfilerCPU.h" #include "Engine/Profiler/ProfilerCPU.h"
#include "Engine/Scripting/Script.h"
#include "Engine/Scripting/Scripting.h" #include "Engine/Scripting/Scripting.h"
#include "Engine/Scripting/ScriptingObjectReference.h" #include "Engine/Scripting/ScriptingObjectReference.h"
#include "Engine/Threading/Threading.h" #include "Engine/Threading/Threading.h"
@@ -213,6 +214,14 @@ void SendObjectRoleMessage(const NetworkReplicatedObject& item, const NetworkCli
} }
} }
FORCE_INLINE void DeleteNetworkObject(ScriptingObject* obj)
{
if (obj->Is<Script>() && ((Script*)obj)->GetParent())
((Script*)obj)->GetParent()->DeleteObject();
else
obj->DeleteObject();
}
#if !COMPILE_WITHOUT_CSHARP #if !COMPILE_WITHOUT_CSHARP
#include "Engine/Scripting/ManagedCLR/MUtils.h" #include "Engine/Scripting/ManagedCLR/MUtils.h"
@@ -328,6 +337,9 @@ void NetworkReplicator::DespawnObject(ScriptingObject* obj)
// Prevent spawning // Prevent spawning
SpawnQueue.Remove(obj); SpawnQueue.Remove(obj);
// Delete object locally
DeleteNetworkObject(obj);
} }
uint32 NetworkReplicator::GetObjectClientId(ScriptingObject* obj) uint32 NetworkReplicator::GetObjectClientId(ScriptingObject* obj)
@@ -415,6 +427,16 @@ void NetworkInternal::NetworkReplicatorClear()
#if NETWORK_REPLICATOR_DEBUG_LOG #if NETWORK_REPLICATOR_DEBUG_LOG
LOG(Info, "[NetworkReplicator] Shutdown"); LOG(Info, "[NetworkReplicator] Shutdown");
#endif #endif
for (auto it = Objects.Begin(); it.IsNotEnd(); ++it)
{
auto& item = it->Item;
ScriptingObject* obj = item.Object.Get();
if (obj && item.Spawned)
{
// Cleanup any spawned objects
DeleteNetworkObject(obj);
}
}
Objects.Clear(); Objects.Clear();
Objects.SetCapacity(0); Objects.SetCapacity(0);
IdsRemappingTable.Clear(); IdsRemappingTable.Clear();
@@ -742,7 +764,7 @@ void NetworkInternal::OnNetworkMessageObjectSpawn(NetworkEvent& event, NetworkCl
{ {
if (parent && parent->Object.Get() && parent->Object->Is<Actor>()) if (parent && parent->Object.Get() && parent->Object->Is<Actor>())
sceneObject->SetParent(parent->Object.As<Actor>()); sceneObject->SetParent(parent->Object.As<Actor>());
else if (auto* parentActor = Scripting::FindObject<Actor>(msgData.ParentId)) else if (auto* parentActor = Scripting::TryFindObject<Actor>(msgData.ParentId))
sceneObject->SetParent(parentActor); sceneObject->SetParent(parentActor);
} }
@@ -769,7 +791,7 @@ void NetworkInternal::OnNetworkMessageObjectDespawn(NetworkEvent& event, Network
// Remove object // Remove object
Objects.Remove(obj); Objects.Remove(obj);
Delete(obj); DeleteNetworkObject(obj);
} }
else else
{ {

View File

@@ -67,7 +67,7 @@ public:
API_FUNCTION() static void SpawnObject(ScriptingObject* obj); API_FUNCTION() static void SpawnObject(ScriptingObject* obj);
/// <summary> /// <summary>
/// Despawns the object from the other clients. /// Despawns the object from the other clients. Deletes object from remove clients.
/// </summary> /// </summary>
/// <remarks>Does nothing if network is offline.</remarks> /// <remarks>Does nothing if network is offline.</remarks>
/// <param name="obj">The object to despawn on other clients.</param> /// <param name="obj">The object to despawn on other clients.</param>