From c559d9d18d81fbf062399c95752b650ef5e12df5 Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Fri, 27 Dec 2024 16:15:40 -0600 Subject: [PATCH] Added Manuel Script Reloading --- assets/scripts/script.lua | 11 ++- scenes/Default.scene | 42 +++++------ scenes/TakeToLongToLoad.scene | 126 ++++++++++++++++++++++++++++++++ src/Engine/LuaAPI.cpp | 8 +- src/Engine/LuaAPI.h | 4 +- src/Windows/InspectorWindow.cpp | 14 +++- 6 files changed, 173 insertions(+), 32 deletions(-) create mode 100644 scenes/TakeToLongToLoad.scene diff --git a/assets/scripts/script.lua b/assets/scripts/script.lua index dba0510..c6ead37 100644 --- a/assets/scripts/script.lua +++ b/assets/scripts/script.lua @@ -16,16 +16,19 @@ end function OnUpdate(deltaTime) - + ticks = ticks + 1 - - if itterator >= 1 then - Engine.Log("TPS: ".. ticks) + local timestep = 5 + if itterator >= timestep then + Engine.Log("TPS: ".. (ticks/timestep)) ticks = 0 itterator = 0 end + + itterator = itterator + deltaTime + end diff --git a/scenes/Default.scene b/scenes/Default.scene index 46f71e8..8e09b57 100644 --- a/scenes/Default.scene +++ b/scenes/Default.scene @@ -11,18 +11,6 @@ Entities: indexCount: 15810 textureID: 1 MeshPath: assets/models/LowPolyFiatUNO.obj - - ID: 1 - Name: Plant - Components: - Transform: - Position: [-8.60000038, 6.5, -15.6000004] - Rotation: [-180, 0, 0] - Scale: [1, 1, 1] - Mesh: - vao: 5 - indexCount: 589632 - textureID: 7 - MeshPath: assets/models/OutSidePlant.obj - ID: 2 Name: Cube Yay 2 Components: @@ -31,7 +19,7 @@ Entities: Rotation: [-86.3000031, 0, -66] Scale: [1, 1, 1] Mesh: - vao: 6 + vao: 5 indexCount: 36 textureID: 3 MeshPath: assets/models/DefaultMesh.obj @@ -43,7 +31,7 @@ Entities: Rotation: [-23.5, 15.8999996, -59.9000015] Scale: [1, 1, 1] Mesh: - vao: 6 + vao: 5 indexCount: 36 textureID: 4 MeshPath: assets/models/DefaultMesh.obj @@ -55,7 +43,7 @@ Entities: Rotation: [-17.2999992, -16.1000004, -19.2999992] Scale: [1, 1, 1] Mesh: - vao: 6 + vao: 5 indexCount: 36 textureID: 5 MeshPath: assets/models/DefaultMesh.obj @@ -63,7 +51,7 @@ Entities: Name: Sky Components: Mesh: - vao: 6 + vao: 5 indexCount: 36 textureID: 6 MeshPath: assets/models/DefaultMesh.obj @@ -79,7 +67,7 @@ Entities: Rotation: [15.8000002, -18.2000008, -11.1000004] Scale: [1, 1, 1] Mesh: - vao: 6 + vao: 5 indexCount: 36 textureID: 3 MeshPath: assets/models/DefaultMesh.obj @@ -91,7 +79,7 @@ Entities: Rotation: [-16.1000004, -15.8999996, -35] Scale: [1, 1, 1] Mesh: - vao: 6 + vao: 5 indexCount: 36 textureID: 4 MeshPath: assets/models/DefaultMesh.obj @@ -103,7 +91,7 @@ Entities: Rotation: [22.2999992, -32.7999992, 0] Scale: [1, 1, 1] Mesh: - vao: 6 + vao: 5 indexCount: 36 textureID: 1 MeshPath: assets/models/DefaultMesh.obj @@ -115,7 +103,7 @@ Entities: Rotation: [-41.4000015, -22.6000004, -52.2999992] Scale: [1, 1, 1] Mesh: - vao: 6 + vao: 5 indexCount: 36 textureID: 2 MeshPath: assets/models/DefaultMesh.obj @@ -123,4 +111,16 @@ Entities: Name: Script Components: ScriptComponent: - ScriptPath: assets/scripts/script.lua \ No newline at end of file + ScriptPath: assets/scripts/script.lua + - ID: 10 + Name: New GameObject 10 + Components: + Mesh: + vao: 2 + indexCount: 15810 + textureID: 9 + MeshPath: assets/models/OutSidePlant.obj + Transform: + Position: [-5.9000001, 2.9000001, -9.89999962] + Rotation: [-191.899994, -246.899994, -28.2999992] + Scale: [1, 1, 1] \ No newline at end of file diff --git a/scenes/TakeToLongToLoad.scene b/scenes/TakeToLongToLoad.scene new file mode 100644 index 0000000..46f71e8 --- /dev/null +++ b/scenes/TakeToLongToLoad.scene @@ -0,0 +1,126 @@ +Entities: + - ID: 0 + Name: Car + Components: + Transform: + Position: [0, 2.79999995, -12.6000004] + Rotation: [149.699997, -137.899994, -39.2999992] + Scale: [1, 1, 1] + Mesh: + vao: 2 + indexCount: 15810 + textureID: 1 + MeshPath: assets/models/LowPolyFiatUNO.obj + - ID: 1 + Name: Plant + Components: + Transform: + Position: [-8.60000038, 6.5, -15.6000004] + Rotation: [-180, 0, 0] + Scale: [1, 1, 1] + Mesh: + vao: 5 + indexCount: 589632 + textureID: 7 + MeshPath: assets/models/OutSidePlant.obj + - ID: 2 + Name: Cube Yay 2 + Components: + Transform: + Position: [7.80000019, -8.10000038, -20.6000004] + Rotation: [-86.3000031, 0, -66] + Scale: [1, 1, 1] + Mesh: + vao: 6 + indexCount: 36 + textureID: 3 + MeshPath: assets/models/DefaultMesh.obj + - ID: 3 + Name: Cube Yay 3 + Components: + Transform: + Position: [-1.20000005, -3.4000001, -17.7000008] + Rotation: [-23.5, 15.8999996, -59.9000015] + Scale: [1, 1, 1] + Mesh: + vao: 6 + indexCount: 36 + textureID: 4 + MeshPath: assets/models/DefaultMesh.obj + - ID: 4 + Name: Cube Yay 4 + Components: + Transform: + Position: [8.10000038, 0.800000012, -12] + Rotation: [-17.2999992, -16.1000004, -19.2999992] + Scale: [1, 1, 1] + Mesh: + vao: 6 + indexCount: 36 + textureID: 5 + MeshPath: assets/models/DefaultMesh.obj + - ID: 5 + Name: Sky + Components: + Mesh: + vao: 6 + indexCount: 36 + textureID: 6 + MeshPath: assets/models/DefaultMesh.obj + Transform: + Position: [0, 0, 43.2000008] + Rotation: [0, 0, 0] + Scale: [100, 100, 100] + - ID: 6 + Name: Cube Yay 6 + Components: + Transform: + Position: [-6.5, -6, -18] + Rotation: [15.8000002, -18.2000008, -11.1000004] + Scale: [1, 1, 1] + Mesh: + vao: 6 + indexCount: 36 + textureID: 3 + MeshPath: assets/models/DefaultMesh.obj + - ID: 7 + Name: Cube Yay 7 + Components: + Transform: + Position: [6.5, 1.79999995, -23.8999996] + Rotation: [-16.1000004, -15.8999996, -35] + Scale: [1, 1, 1] + Mesh: + vao: 6 + indexCount: 36 + textureID: 4 + MeshPath: assets/models/DefaultMesh.obj + - ID: 8 + Name: Cube Yay 8 + Components: + Transform: + Position: [-7.80000019, 0.200000003, -29.7999992] + Rotation: [22.2999992, -32.7999992, 0] + Scale: [1, 1, 1] + Mesh: + vao: 6 + indexCount: 36 + textureID: 1 + MeshPath: assets/models/DefaultMesh.obj + - ID: 9 + Name: Cube Yay 9 + Components: + Transform: + Position: [5.5, -2.9000001, -19.5] + Rotation: [-41.4000015, -22.6000004, -52.2999992] + Scale: [1, 1, 1] + Mesh: + vao: 6 + indexCount: 36 + textureID: 2 + MeshPath: assets/models/DefaultMesh.obj + - ID: 10 + Name: Script + Components: + ScriptComponent: + ScriptPath: assets/scripts/script.lua \ No newline at end of file diff --git a/src/Engine/LuaAPI.cpp b/src/Engine/LuaAPI.cpp index 56ec5db..80355f1 100644 --- a/src/Engine/LuaAPI.cpp +++ b/src/Engine/LuaAPI.cpp @@ -18,7 +18,7 @@ int lua_log_message(lua_State *L); -LuaManager::LuaManager() : L(nullptr) {} +LuaManager::LuaManager() : L(nullptr), m_firstCall(false) {} LuaManager::~LuaManager() { @@ -55,7 +55,7 @@ bool LuaManager::init(const std::string &scriptPath) L = nullptr; return false; } - + callLuaFunction("OnInit"); return true; } @@ -96,6 +96,8 @@ bool LuaManager::onUpdate(float deltaTime) // Push the deltaTime argument lua_pushnumber(L, deltaTime); + m_firstCall = true; + // Call the function with 1 argument and 0 return values if (lua_pcall(L, 1, 0, 0) != LUA_OK) { @@ -135,7 +137,9 @@ bool LuaManager::onUpdate(float deltaTime) lua_pop(L, 1); // Remove error message + return false; + } else { diff --git a/src/Engine/LuaAPI.h b/src/Engine/LuaAPI.h index f332b3e..846d4b9 100644 --- a/src/Engine/LuaAPI.h +++ b/src/Engine/LuaAPI.h @@ -22,7 +22,9 @@ public: private: lua_State* L; - std::string m_LastErrorMessage; // Stores the last error message + std::string m_LastErrorMessage; // Stores the last error message + + bool m_firstCall; // Helper function to call a Lua function with no arguments and no return values diff --git a/src/Windows/InspectorWindow.cpp b/src/Windows/InspectorWindow.cpp index 4bf3b5c..6a72347 100644 --- a/src/Windows/InspectorWindow.cpp +++ b/src/Windows/InspectorWindow.cpp @@ -9,6 +9,9 @@ extern std::vector g_GameObjects; extern GameObject *g_SelectedObject; // Pointer to the currently selected object +extern LoggerWindow *g_LoggerWindow; + + void InspectorWindow::Show() { // Increase window/item spacing for a cleaner look @@ -74,12 +77,11 @@ void InspectorWindow::Show() // =========================== // 1) TRANSFORM // =========================== - + std::shared_ptr transform = g_SelectedObject->GetComponent(); std::shared_ptr mesh = g_SelectedObject->GetComponent(); std::shared_ptr script = g_SelectedObject->GetComponent(); - // Color the Transform header if (transform && g_SelectedObject) // Funny: I did not put a null check here and it broke everything. @@ -293,7 +295,7 @@ void InspectorWindow::Show() } } - if (mesh && g_SelectedObject) + if (mesh && g_SelectedObject) { // Transform* transform = &g_SelectedObject->transform; @@ -350,7 +352,6 @@ void InspectorWindow::Show() bool scriptOpen = ImGui::CollapsingHeader("Script##Main", ImGuiTreeNodeFlags_DefaultOpen); ImGui::PopStyleColor(); - // printf("%p\n", &transform); if (scriptOpen) { @@ -367,6 +368,11 @@ void InspectorWindow::Show() script->ScriptPath = buffer; } + if (ImGui::Button("Reload Script")) + { + script->Initialize(); + g_LoggerWindow->AddLog("Reloaded Script: %s", ImVec4(0.0f,1.0f,0.0f,1.0f), script->ScriptPath.c_str()); + } } }