From 0e750bda89d9f4ffa36c1c5fbd56448e5b2e7cad Mon Sep 17 00:00:00 2001 From: antopilo Date: Sat, 21 Sep 2024 14:02:20 -0400 Subject: [PATCH] work --- Nuake/src/Scene/Systems/ScriptingSystem.cpp | 47 +++++++++++++++------ NuakeNet/src/Entity.cs | 16 +++++++ NuakeNet/src/Scene.cs | 14 ++++-- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/Nuake/src/Scene/Systems/ScriptingSystem.cpp b/Nuake/src/Scene/Systems/ScriptingSystem.cpp index 948d95bb..8eb0b166 100644 --- a/Nuake/src/Scene/Systems/ScriptingSystem.cpp +++ b/Nuake/src/Scene/Systems/ScriptingSystem.cpp @@ -82,10 +82,41 @@ namespace Nuake if (!Engine::IsPlayMode()) return; - InitializeNewScripts(); - auto& scriptingEngineNet = ScriptingEngineNet::Get(); auto netEntities = m_Scene->m_Registry.view(); + + // Instance all the new entities + std::vector entityJustInstanced; + for (auto& e : netEntities) + { + auto& netScriptComponent = netEntities.get(e); + if (!netScriptComponent.Initialized) + { + if (netScriptComponent.ScriptPath.empty()) + continue; + + auto entity = Entity{ e, m_Scene }; + + // Creates an instance of the entity script in C# + scriptingEngineNet.RegisterEntityScript(entity); + + netScriptComponent.Initialized = true; + entityJustInstanced.push_back(entity); + } + } + + // Call init on the newly created instance + for (auto& e : entityJustInstanced) + { + auto& netScriptComponent = e.GetComponent(); + if (e.IsValid() && scriptingEngineNet.HasEntityScriptInstance(e)) + { + // We can now call on init on it. + auto scriptInstance = scriptingEngineNet.GetEntityScript(e); + scriptInstance.InvokeMethod("OnInit"); + } + } + // Then call update on all the other scripts for (auto& e : netEntities) { @@ -180,18 +211,6 @@ namespace Nuake entityJustInstanced.push_back(entity); } } - - // Call init on the newly created instance - for (auto& e : entityJustInstanced) - { - auto& netScriptComponent = e.GetComponent(); - if (e.IsValid() && scriptingEngineNet.HasEntityScriptInstance(e)) - { - // We can now call on init on it. - auto scriptInstance = scriptingEngineNet.GetEntityScript(e); - scriptInstance.InvokeMethod("OnInit"); - } - } } void ScriptingSystem::DispatchPhysicCallbacks() diff --git a/NuakeNet/src/Entity.cs b/NuakeNet/src/Entity.cs index ca0b94a6..af45ca92 100644 --- a/NuakeNet/src/Entity.cs +++ b/NuakeNet/src/Entity.cs @@ -301,6 +301,22 @@ namespace Nuake.Net return entity; } + public T? Instance(Vector3 position = default, Quaternion quat = default) where T : Entity + { + int handle; + unsafe { handle = PrefabInstanceIcall(Path, position, quat.X, quat.Y, quat.Z, quat.W); } + + if (handle == -1) + { + return null; + } + + T? instance = Scene.GetEntity(handle); + instance.OnInit(); + + return instance; + } + public static implicit operator bool(Prefab prefab) { if (object.ReferenceEquals(prefab, null)) diff --git a/NuakeNet/src/Scene.cs b/NuakeNet/src/Scene.cs index 75d56cb2..641432a0 100644 --- a/NuakeNet/src/Scene.cs +++ b/NuakeNet/src/Scene.cs @@ -54,7 +54,7 @@ namespace Nuake.Net return entity; } - public static Entity? InstancePrefab(string path, Entity? parent = null) + public static T? InstancePrefab(string path, Entity? parent = null) where T : class { int handle; unsafe { handle = InstancePrefabIcall(path); } @@ -73,9 +73,17 @@ namespace Nuake.Net // return entity as T; //} - Entity entity = new Entity(handle); + + NativeInstance nativeInstance; + unsafe { nativeInstance = GetEntityScriptFromHandleIcall(handle); } - return entity; + Entity? entity = nativeInstance.Get(); + if (entity != null && entity is T) + { + return entity as T; + } + + return null; } } } \ No newline at end of file