From 2d3fe57d5a80791e0c05cad31538230637f71b69 Mon Sep 17 00:00:00 2001 From: Erris Date: Fri, 6 Mar 2026 18:07:51 +0100 Subject: [PATCH] added UUID --- editor/src/panels/scene_hierarchy.cpp | 1 - imgui.ini | 30 ++++++++-------- open_engine/include/open_engine/core/uuid.hpp | 36 +++++++++++++++++++ .../include/open_engine/scene/components.hpp | 16 ++++++--- .../include/open_engine/scene/entity.hpp | 4 +++ .../scene/native_scriptable_entity.hpp | 1 + .../include/open_engine/scene/scene.hpp | 2 ++ open_engine/src/open_engine/core/uuid.cpp | 24 +++++++++++++ open_engine/src/open_engine/scene/scene.cpp | 12 +++++++ .../open_engine/scene/scene_serializer.cpp | 8 +++-- 10 files changed, 111 insertions(+), 23 deletions(-) create mode 100644 open_engine/include/open_engine/core/uuid.hpp create mode 100644 open_engine/src/open_engine/core/uuid.cpp diff --git a/editor/src/panels/scene_hierarchy.cpp b/editor/src/panels/scene_hierarchy.cpp index bdcfdc6..652febf 100644 --- a/editor/src/panels/scene_hierarchy.cpp +++ b/editor/src/panels/scene_hierarchy.cpp @@ -1,4 +1,3 @@ -#include "open_engine/scene/components.hpp" #include "open_engine/scene/entity.hpp" #include "editor_component.hpp" diff --git a/imgui.ini b/imgui.ini index a2c5d73..9bfba42 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,6 +1,6 @@ [Window][WindowOverViewport_11111111] Pos=0,24 -Size=2560,1371 +Size=1272,1363 Collapsed=0 [Window][Debug##Default] @@ -10,31 +10,31 @@ Collapsed=0 [Window][Statistics] Pos=0,24 -Size=224,439 +Size=224,437 Collapsed=0 DockId=0x00000003,0 [Window][Properties] -Pos=2110,24 -Size=450,810 +Pos=822,24 +Size=450,805 Collapsed=0 DockId=0x00000007,0 [Window][Viewport] Pos=226,61 -Size=1882,964 +Size=594,956 Collapsed=0 DockId=0x00000012,0 [Window][Dear ImGui Demo] -Pos=2110,836 -Size=450,559 +Pos=822,831 +Size=450,556 Collapsed=0 DockId=0x00000008,0 [Window][Scene] -Pos=0,465 -Size=224,930 +Pos=0,463 +Size=224,924 Collapsed=0 DockId=0x00000004,0 @@ -143,8 +143,8 @@ Collapsed=0 DockId=0x00000012,1 [Window][Assets] -Pos=226,1027 -Size=1882,368 +Pos=226,1019 +Size=594,368 Collapsed=0 DockId=0x0000000C,0 @@ -156,12 +156,12 @@ DockId=0x0000000F,0 [Window][##play_state_bar] Pos=226,24 -Size=1882,35 +Size=594,35 Collapsed=0 DockId=0x00000011,0 [Docking][Data] -DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2560,1371 Split=X +DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=1272,1363 Split=X DockNode ID=0x00000005 Parent=0x08BD597D SizeRef=820,1386 Split=X DockNode ID=0x00000001 Parent=0x00000005 SizeRef=224,1386 Split=Y Selected=0xE601B12F DockNode ID=0x00000003 Parent=0x00000001 SizeRef=255,417 Selected=0x553E127E @@ -178,6 +178,6 @@ DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2560,1371 DockNode ID=0x0000000C Parent=0x00000009 SizeRef=409,368 Selected=0x42C24103 DockNode ID=0x0000000A Parent=0x00000002 SizeRef=483,600 Selected=0x1BCA3180 DockNode ID=0x00000006 Parent=0x08BD597D SizeRef=450,1386 Split=Y Selected=0x8C72BEA8 - DockNode ID=0x00000007 Parent=0x00000006 SizeRef=444,810 Selected=0x8C72BEA8 - DockNode ID=0x00000008 Parent=0x00000006 SizeRef=444,559 Selected=0x5E5F7166 + DockNode ID=0x00000007 Parent=0x00000006 SizeRef=444,805 Selected=0x8C72BEA8 + DockNode ID=0x00000008 Parent=0x00000006 SizeRef=444,556 Selected=0x5E5F7166 diff --git a/open_engine/include/open_engine/core/uuid.hpp b/open_engine/include/open_engine/core/uuid.hpp new file mode 100644 index 0000000..3ab6d36 --- /dev/null +++ b/open_engine/include/open_engine/core/uuid.hpp @@ -0,0 +1,36 @@ +#ifndef UUID_HPP +#define UUID_HPP + +#include + +namespace OpenEngine { + + class UUID + { + public: + UUID(); + UUID(uint64_t uuid); + UUID(const UUID&) = default; + + operator uint64_t() const { return uuid; }; + + private: + uint64_t uuid; + }; + +} + +namespace std { + + template<> + struct hash + { + std::size_t operator()(const OpenEngine::UUID& uuid) const + { + return hash()((uint64_t)uuid); + } + }; + +} + +#endif // UUID_HPP diff --git a/open_engine/include/open_engine/scene/components.hpp b/open_engine/include/open_engine/scene/components.hpp index 9375e32..d5fe5cb 100644 --- a/open_engine/include/open_engine/scene/components.hpp +++ b/open_engine/include/open_engine/scene/components.hpp @@ -1,12 +1,12 @@ #ifndef COMPONENTS_HPP #define COMPONENTS_HPP -#include "open_engine/scene/native_scriptable_entity.hpp" #include "open_engine/renderer/renderer3d.hpp" #include "open_engine/scene/scene_camera.hpp" #include "open_engine/renderer/texture.hpp" #include "open_engine/renderer/renderer3d.hpp" #include "open_engine/physics.hpp" +#include "open_engine/core/uuid.hpp" #include #include @@ -28,6 +28,15 @@ namespace OpenEngine { + struct IDComponent + { + UUID id; + + IDComponent() = default; + IDComponent(const IDComponent&) = default; + IDComponent(const UUID& uuid) : id(uuid) {}; + }; + struct TagComponent { std::string tag; @@ -74,6 +83,8 @@ namespace OpenEngine { CameraComponent(const CameraComponent&) = default; }; + class NativeScriptableEntity; + struct NativeScriptComponent { NativeScriptableEntity* instance = nullptr; @@ -81,9 +92,6 @@ namespace OpenEngine { NativeScriptableEntity* (*InstanciateScript)(); void (*DestroyInstanceScript)(NativeScriptComponent*); - void OnImGuiRender(Entity& entity) - {}; - template void Bind() { diff --git a/open_engine/include/open_engine/scene/entity.hpp b/open_engine/include/open_engine/scene/entity.hpp index bfe1e30..50d375a 100644 --- a/open_engine/include/open_engine/scene/entity.hpp +++ b/open_engine/include/open_engine/scene/entity.hpp @@ -3,6 +3,8 @@ #include "open_engine/core.hpp" +#include "open_engine/core/uuid.hpp" +#include "open_engine/scene/components.hpp" #include "open_engine/scene/scene.hpp" #include @@ -48,6 +50,8 @@ namespace OpenEngine { return scene->registry.all_of(handle); }; + UUID GetUUID() { return GetComponents().id; }; + operator bool() const { return handle != entt::null; }; operator entt::entity() const { return handle; }; operator uint32_t() const { return (uint32_t)handle; }; diff --git a/open_engine/include/open_engine/scene/native_scriptable_entity.hpp b/open_engine/include/open_engine/scene/native_scriptable_entity.hpp index dceb611..65ae6c2 100644 --- a/open_engine/include/open_engine/scene/native_scriptable_entity.hpp +++ b/open_engine/include/open_engine/scene/native_scriptable_entity.hpp @@ -1,6 +1,7 @@ #ifndef NATIVE_SCRIPTABLE_ENTITY_HPP #define NATIVE_SCRIPTABLE_ENTITY_HPP +#include "open_engine/scene/native_scriptable_entity.hpp" #include "open_engine/scene/entity.hpp" namespace OpenEngine { diff --git a/open_engine/include/open_engine/scene/scene.hpp b/open_engine/include/open_engine/scene/scene.hpp index c475590..24d405a 100644 --- a/open_engine/include/open_engine/scene/scene.hpp +++ b/open_engine/include/open_engine/scene/scene.hpp @@ -1,6 +1,7 @@ #ifndef SCENE_HPP #define SCENE_HPP +#include "open_engine/core/uuid.hpp" #include "open_engine/renderer/editor_camera.hpp" #include "open_engine/physics.hpp" @@ -24,6 +25,7 @@ namespace OpenEngine { void OnRuntimeStop(); Entity CreateEntity(const std::string& name = std::string()); + Entity CreateEntityWithUUID(UUID uuid, const std::string& name = std::string()); void DeleteEntity(entt::entity entity); void MarkEntityForDeletion(Entity entity); diff --git a/open_engine/src/open_engine/core/uuid.cpp b/open_engine/src/open_engine/core/uuid.cpp new file mode 100644 index 0000000..5e08f75 --- /dev/null +++ b/open_engine/src/open_engine/core/uuid.cpp @@ -0,0 +1,24 @@ +#include +#include + +#include + +#include + +namespace OpenEngine { + + static std::random_device s_random_device; + static std::mt19937_64 s_engine(s_random_device()); + static std::uniform_int_distribution s_uniform_distribution; + + UUID::UUID() + : uuid(s_uniform_distribution(s_engine)) + { + } + + UUID::UUID(uint64_t uuid) + : uuid(uuid) + { + } + +} diff --git a/open_engine/src/open_engine/scene/scene.cpp b/open_engine/src/open_engine/scene/scene.cpp index dfc26fb..75297ad 100755 --- a/open_engine/src/open_engine/scene/scene.cpp +++ b/open_engine/src/open_engine/scene/scene.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -98,9 +99,15 @@ namespace OpenEngine { } Entity Scene::CreateEntity(const std::string& name) + { + return CreateEntityWithUUID(UUID(), name); + } + + Entity Scene::CreateEntityWithUUID(UUID uuid, const std::string& name) { Entity entity = { registry.create(), this }; + entity.AddComponent(uuid); auto& tag = entity.AddComponent(); tag.tag = name.empty() ? "Entity" : name; @@ -340,4 +347,9 @@ namespace OpenEngine { void Scene::OnComponentAdded(Entity entity, BoxShapeComponent& component) { } + + template<> + void Scene::OnComponentAdded(Entity entity, IDComponent& component) + { + } } diff --git a/open_engine/src/open_engine/scene/scene_serializer.cpp b/open_engine/src/open_engine/scene/scene_serializer.cpp index 818e3d1..9733ea6 100644 --- a/open_engine/src/open_engine/scene/scene_serializer.cpp +++ b/open_engine/src/open_engine/scene/scene_serializer.cpp @@ -88,8 +88,10 @@ namespace OpenEngine { static void SerializeEntity(YAML::Emitter& out, Entity entity) { + OE_CORE_ASSERT(entity.HasComponent(), "Entity is missing UUID."); + out << YAML::BeginMap; - out << YAML::Key << "Entity" << YAML::Value << "412741205"; // Needs random + out << YAML::Key << "Entity" << YAML::Value << entity.GetUUID(); // Needs random if (entity.HasComponent()) { @@ -258,7 +260,7 @@ namespace OpenEngine { { for (auto entity : entities) { - uint64_t uuid = entity["Entity"].as(); // TODO + uint64_t uuid = entity["Entity"].as(); std::string name; auto tagComponent = entity["TagComponent"]; @@ -267,7 +269,7 @@ namespace OpenEngine { OE_CORE_TRACE("Deserialized entity with ID = {0}, name = {1}", uuid, name); - Entity deserializedEntity = context->CreateEntity(name); + Entity deserializedEntity = context->CreateEntityWithUUID(uuid, name); auto transformComponent = entity["TransformComponent"]; if (transformComponent)