From ff7daaa9cc3dec578d07b7fa9431ef6867c3fc92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20J=C3=B6nsson?= Date: Sun, 24 Sep 2023 11:00:27 +0200 Subject: [PATCH] Mono script copying now works and fixed a bug with duplicating --- Volt/Volt/src/Volt/Scene/Entity.cpp | 39 +++++++++++++++++++++++------ Volt/Volt/src/Volt/Scene/Entity.h | 2 +- Volt/Volt/src/Volt/Scene/Scene.cpp | 3 ++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Volt/Volt/src/Volt/Scene/Entity.cpp b/Volt/Volt/src/Volt/Scene/Entity.cpp index 8788f2e15..0769c9fa9 100644 --- a/Volt/Volt/src/Volt/Scene/Entity.cpp +++ b/Volt/Volt/src/Volt/Scene/Entity.cpp @@ -481,10 +481,8 @@ namespace Volt return {}; } - void Entity::Copy(Entity srcEntity, Entity dstEntity) + void Entity::Copy(Entity srcEntity, Entity dstEntity, bool skipRelationships) { - // #TODO_Ivar: Implement MonoComponent copying - auto srcScene = srcEntity.GetScene(); auto& srcRegistry = srcScene->GetRegistry(); @@ -525,6 +523,7 @@ namespace Volt CopyMonoScripts(srcEntity, dstEntity); // Clear the relationship component, as this is a standalone entity + if (skipRelationships) { RelationshipComponent& relComp = dstEntity.GetComponent(); relComp.children.clear(); @@ -549,7 +548,7 @@ namespace Volt for (const auto& child : srcEntity.GetChildren()) { - newChildren.emplace_back(Duplicate(child).GetID()); + newChildren.emplace_back(Duplicate(child, newEntity).GetID()); } newEntity.GetComponent().children = newChildren; @@ -596,6 +595,11 @@ namespace Volt void Entity::CopyMonoScripts(Entity srcEntity, Entity dstEntity) { + if (!srcEntity.HasComponent() || !dstEntity.HasComponent()) + { + return; + } + const auto& srcComponent = srcEntity.GetComponent(); auto& dstComponent = dstEntity.GetComponent(); @@ -605,21 +609,40 @@ namespace Volt dstComponent.scriptNames = srcComponent.scriptNames; dstComponent.scriptIds.clear(); - for (size_t i = 0; i < dstComponent.scriptNames.size(); i++) + for (size_t i = 0; i < srcComponent.scriptNames.size(); i++) { dstComponent.scriptIds.emplace_back(); - if (!MonoScriptEngine::EntityClassExists(dstComponent.scriptNames.at(i))) + if (!MonoScriptEngine::EntityClassExists(srcComponent.scriptNames.at(i))) { continue; } - const auto& classFields = MonoScriptEngine::GetScriptClass(dstComponent.scriptNames.at(i)); + const auto& classFields = MonoScriptEngine::GetScriptClass(srcComponent.scriptNames.at(i))->GetFields(); - if (!srcScene->GetScriptFieldCache().GetCache().contains(dstComponent.scriptIds.at(i))) + if (!srcScene->GetScriptFieldCache().GetCache().contains(srcComponent.scriptIds.at(i))) { continue; } + + const auto& srcFields = srcScene->GetScriptFieldCache().GetCache().at(srcComponent.scriptIds.at(i)); + auto& dstFields = dstScene->GetScriptFieldCache().GetCache()[dstComponent.scriptIds[i]]; + + for (const auto& [name, field] : classFields) + { + if (!srcFields.contains(name)) + { + continue; + } + + auto& srcField = srcFields.at(name); + + dstFields[name] = CreateRef(); + dstFields.at(name)->field = srcFields.at(name)->field; + + const void* dataPtr = srcField->data.As(); + dstFields.at(name)->SetValue(dataPtr, srcFields.at(name)->field.type.typeSize); + } } } diff --git a/Volt/Volt/src/Volt/Scene/Entity.h b/Volt/Volt/src/Volt/Scene/Entity.h index 75b6d828d..ca1764053 100644 --- a/Volt/Volt/src/Volt/Scene/Entity.h +++ b/Volt/Volt/src/Volt/Scene/Entity.h @@ -101,7 +101,7 @@ namespace Volt static Entity Null(); // Copies a single entity - static void Copy(Entity srcEntity, Entity dstEntity); + static void Copy(Entity srcEntity, Entity dstEntity, bool skipRelationships = true); // Duplicates an entire entity tree static Entity Duplicate(Entity srcEntity, Entity parent = Entity::Null()); diff --git a/Volt/Volt/src/Volt/Scene/Scene.cpp b/Volt/Volt/src/Volt/Scene/Scene.cpp index ead19acb6..7592f89f2 100644 --- a/Volt/Volt/src/Volt/Scene/Scene.cpp +++ b/Volt/Volt/src/Volt/Scene/Scene.cpp @@ -760,7 +760,7 @@ namespace Volt m_registry.each([&](entt::entity id) { id = otherScene->m_registry.create(id); - Entity::Copy(Entity{ id, this }, Entity{ id, otherScene }); + Entity::Copy(Entity{ id, this }, Entity{ id, otherScene }, false); }); } @@ -841,6 +841,7 @@ namespace Volt m_registry.on_construct().connect<&Scene::MeshColliderComponent_OnCreate>(this); m_registry.on_construct().connect<&Scene::AudioSourceComponent_OnCreate>(this); m_registry.on_construct().connect<&Scene::AudioListenerComponent_OnCreate>(this); + m_registry.on_construct().connect<&Scene::CameraComponent_OnCreate>(this); m_registry.on_destroy().connect<&Scene::RigidbodyComponent_OnDestroy>(this); m_registry.on_destroy().connect<&Scene::CharacterControllerComponent_OnDestroy>(this);