From 901ebcf9026dc46a83fd1efbdf28d6a9cceb03a6 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Thu, 15 Aug 2024 19:18:26 -0400 Subject: [PATCH] Added bool operator and isvalid function to .Net API --- Editor/src/ComponentsPanel/NetScriptPanel.cpp | 10 +++++++--- .../src/Scripting/NetModules/SceneNetAPI.cpp | 7 +++++++ NuakeNet/src/Entity.cs | 20 ++++++++++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Editor/src/ComponentsPanel/NetScriptPanel.cpp b/Editor/src/ComponentsPanel/NetScriptPanel.cpp index a32ec44c..87e6ee27 100644 --- a/Editor/src/ComponentsPanel/NetScriptPanel.cpp +++ b/Editor/src/ComponentsPanel/NetScriptPanel.cpp @@ -210,19 +210,23 @@ void NetScriptPanel::Draw(Nuake::Entity entity) if (field.Type == Nuake::NetScriptExposedVarType::Entity) { - if (!field.Value.has_value()) + if (!field.Value.has_value() && field.DefaultValue.has_value()) { field.Value = field.DefaultValue; } + int entityId = -1; + if (field.Value.has_value()) + { + entityId = std::any_cast(field.Value); + } - int entityId = std::any_cast(field.Value); auto entity = Nuake::Engine::GetCurrentScene()->GetEntityByID(entityId); bool invalid = !entity.IsValid(); std::string buttonLabel; if (invalid) { - buttonLabel = "Invalid Entity*"; + buttonLabel = "Invalid Entity"; } else { diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index dbf0f72f..d8880c09 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -200,6 +200,12 @@ namespace Nuake { entity.GetComponent().Name = newEntityName; } + bool EntityIsValid(int handle) + { + Entity entity = { (entt::entity)(handle), Engine::GetCurrentScene().get() }; + return entity.IsValid(); + } + void TransformSetPosition(int entityId, float x, float y, float z) { Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() }; @@ -361,6 +367,7 @@ namespace Nuake { RegisterMethod("Entity.EntityGetEntityIcall", &EntityGetEntity); RegisterMethod("Entity.EntityGetNameIcall", &EntityGetName); RegisterMethod("Entity.EntitySetNameIcall", &EntitySetName); + RegisterMethod("Entity.EntityIsValidIcall", &EntityIsValid); // Scene RegisterMethod("Scene.GetEntityIcall", &GetEntity); diff --git a/NuakeNet/src/Entity.cs b/NuakeNet/src/Entity.cs index dc0708f1..8c50abb3 100644 --- a/NuakeNet/src/Entity.cs +++ b/NuakeNet/src/Entity.cs @@ -26,6 +26,7 @@ namespace Nuake.Net internal static unsafe delegate* EntityGetEntityIcall; internal static unsafe delegate* EntityGetNameIcall; internal static unsafe delegate* EntitySetNameIcall; + internal static unsafe delegate* EntityIsValidIcall; public enum ComponentTypes { @@ -56,7 +57,7 @@ namespace Nuake.Net public int ID { get; set; } - public int ECSHandle { get; set; } + public int ECSHandle { get; set; } = -1; public string Name { @@ -184,5 +185,22 @@ namespace Nuake.Net return entityInstance as T; } + + public static implicit operator bool(Entity entity) + { + bool isValid = false; + + if(object.ReferenceEquals(entity, null)) + { + return false; + } + + unsafe + { + isValid = EntityIsValidIcall(entity.ECSHandle); + } + + return isValid; + } } }