Improved Lua Error Handling: Found Dumb bug where you can trag the Tag Component and break it.

This commit is contained in:
OusmBlueNinja 2024-12-27 16:52:47 -06:00
parent c559d9d18d
commit 376e63c501
6 changed files with 65 additions and 74 deletions

View File

@ -11,6 +11,8 @@ function OnInit()
-- Log a message with a custom blue color -- Log a message with a custom blue color
Engine.Log("This is a blue message.", {0.0, 0.0, 1.0, 1.0}) Engine.Log("This is a blue message.", {0.0, 0.0, 1.0, 1.0})
asd
end end
@ -18,6 +20,7 @@ end
function OnUpdate(deltaTime) function OnUpdate(deltaTime)
ticks = ticks + 1 ticks = ticks + 1
local timestep = 5 local timestep = 5
if itterator >= timestep then if itterator >= timestep then

View File

@ -2,125 +2,113 @@ Entities:
- ID: 0 - ID: 0
Name: Car Name: Car
Components: Components:
Transform:
Position: [0, 2.79999995, -12.6000004]
Rotation: [149.699997, -137.899994, -39.2999992]
Scale: [1, 1, 1]
Mesh: Mesh:
vao: 2 vao: 2
indexCount: 15810 indexCount: 15810
textureID: 1 textureID: 1
MeshPath: assets/models/LowPolyFiatUNO.obj MeshPath: assets/models/LowPolyFiatUNO.obj
Transform:
Position: [0, 2.79999995, -12.6000004]
Rotation: [149.699997, -137.899994, -39.2999992]
Scale: [1, 1, 1]
- ID: 2 - ID: 2
Name: Cube Yay 2 Name: Cube Yay 2
Components: Components:
Transform:
Position: [7.80000019, -8.10000038, -20.6000004]
Rotation: [-86.3000031, 0, -66]
Scale: [1, 1, 1]
Mesh: Mesh:
vao: 5 vao: 5
indexCount: 36 indexCount: 36
textureID: 3 textureID: 3
MeshPath: assets/models/DefaultMesh.obj MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [7.80000019, -8.10000038, -20.6000004]
Rotation: [-86.3000031, 0, -66]
Scale: [1, 1, 1]
- ID: 3 - ID: 3
Name: Cube Yay 3 Name: Cube Yay 3
Components: Components:
Transform:
Position: [-1.20000005, -3.4000001, -17.7000008]
Rotation: [-23.5, 15.8999996, -59.9000015]
Scale: [1, 1, 1]
Mesh: Mesh:
vao: 5 vao: 5
indexCount: 36 indexCount: 36
textureID: 4 textureID: 4
MeshPath: assets/models/DefaultMesh.obj MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [-1.20000005, -3.4000001, -17.7000008]
Rotation: [-23.5, 15.8999996, -59.9000015]
Scale: [1, 1, 1]
- ID: 4 - ID: 4
Name: Cube Yay 4 Name: Cube Yay 4
Components: Components:
Transform:
Position: [8.10000038, 0.800000012, -12]
Rotation: [-17.2999992, -16.1000004, -19.2999992]
Scale: [1, 1, 1]
Mesh: Mesh:
vao: 5 vao: 5
indexCount: 36 indexCount: 36
textureID: 5 textureID: 5
MeshPath: assets/models/DefaultMesh.obj MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [8.10000038, 0.800000012, -12]
Rotation: [-17.2999992, -16.1000004, -19.2999992]
Scale: [1, 1, 1]
- ID: 5 - ID: 5
Name: Sky Name: Sky
Components: Components:
Transform:
Position: [0, 0, 43.2000008]
Rotation: [0, 0, 0]
Scale: [100, 100, 100]
Mesh: Mesh:
vao: 5 vao: 5
indexCount: 36 indexCount: 36
textureID: 6 textureID: 6
MeshPath: assets/models/DefaultMesh.obj MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [0, 0, 43.2000008]
Rotation: [0, 0, 0]
Scale: [100, 100, 100]
- ID: 6 - ID: 6
Name: Cube Yay 6 Name: Cube Yay 6
Components: Components:
Transform:
Position: [-6.5, -6, -18]
Rotation: [15.8000002, -18.2000008, -11.1000004]
Scale: [1, 1, 1]
Mesh: Mesh:
vao: 5 vao: 5
indexCount: 36 indexCount: 36
textureID: 3 textureID: 3
MeshPath: assets/models/DefaultMesh.obj MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [-6.5, -6, -18]
Rotation: [15.8000002, -18.2000008, -11.1000004]
Scale: [1, 1, 1]
- ID: 7 - ID: 7
Name: Cube Yay 7 Name: Cube Yay 7
Components: Components:
Transform:
Position: [6.5, 1.79999995, -23.8999996]
Rotation: [-16.1000004, -15.8999996, -35]
Scale: [1, 1, 1]
Mesh: Mesh:
vao: 5 vao: 5
indexCount: 36 indexCount: 36
textureID: 4 textureID: 4
MeshPath: assets/models/DefaultMesh.obj MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [6.5, 1.79999995, -23.8999996]
Rotation: [-16.1000004, -15.8999996, -35]
Scale: [1, 1, 1]
- ID: 8 - ID: 8
Name: Cube Yay 8 Name: Cube Yay 8
Components: Components:
Transform:
Position: [-7.80000019, 0.200000003, -29.7999992]
Rotation: [22.2999992, -32.7999992, 0]
Scale: [1, 1, 1]
Mesh: Mesh:
vao: 5 vao: 5
indexCount: 36 indexCount: 36
textureID: 1 textureID: 1
MeshPath: assets/models/DefaultMesh.obj MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [-7.80000019, 0.200000003, -29.7999992]
Rotation: [22.2999992, -32.7999992, 0]
Scale: [1, 1, 1]
- ID: 9 - ID: 9
Name: Cube Yay 9 Name: Cube Yay 9
Components: Components:
Transform:
Position: [5.5, -2.9000001, -19.5]
Rotation: [-41.4000015, -22.6000004, -52.2999992]
Scale: [1, 1, 1]
Mesh: Mesh:
vao: 5 vao: 5
indexCount: 36 indexCount: 36
textureID: 2 textureID: 2
MeshPath: assets/models/DefaultMesh.obj MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [5.5, -2.9000001, -19.5]
Rotation: [-41.4000015, -22.6000004, -52.2999992]
Scale: [1, 1, 1]
- ID: 10 - ID: 10
Name: Script Name: Script
Components: Components:
ScriptComponent: ScriptComponent:
ScriptPath: assets/scripts/script.lua 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]

View File

@ -75,14 +75,7 @@ bool ScriptComponent::Initialize()
// Initialize LuaManager with the script path // Initialize LuaManager with the script path
if (!m_LuaManager.init(ScriptPath)) if (!m_LuaManager.init(ScriptPath))
{ {
if (g_LoggerWindow) DEBUG_PRINT("ScriptComponent: Failed to initialize LuaManager");
{
g_LoggerWindow->AddLog("ScriptComponent: Failed to initialize LuaManager.", ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
}
else
{
std::cerr << "ScriptComponent: Failed to initialize LuaManager." << std::endl;
}
return false; return false;
} }

View File

@ -9,16 +9,11 @@
#include "Windows/LoggerWindow.h" #include "Windows/LoggerWindow.h"
extern LoggerWindow *g_LoggerWindow; extern LoggerWindow *g_LoggerWindow;
int lua_log_message(lua_State *L); int lua_log_message(lua_State *L);
LuaManager::LuaManager() : L(nullptr), m_firstCall(false), m_Initialized(false) {}
LuaManager::LuaManager() : L(nullptr), m_firstCall(false) {}
LuaManager::~LuaManager() LuaManager::~LuaManager()
{ {
@ -50,11 +45,14 @@ bool LuaManager::init(const std::string &scriptPath)
// Load and execute the Lua script // Load and execute the Lua script
if (luaL_dofile(L, scriptPath.c_str()) != LUA_OK) if (luaL_dofile(L, scriptPath.c_str()) != LUA_OK)
{ {
std::cerr << "Error running " << scriptPath << ": " << lua_tostring(L, -1) << "\n"; g_LoggerWindow->AddLog("%s", ImVec4(1.0f, 0.0f, 0.0f, 1.0f), lua_tostring(L, -1));
DEBUG_PRINT("%s: %s", scriptPath.c_str(), lua_tostring(L, -1));
lua_close(L); lua_close(L);
L = nullptr; L = nullptr;
return false; return false;
} }
m_Initialized = true;
callLuaFunction("OnInit"); callLuaFunction("OnInit");
return true; return true;
@ -62,6 +60,9 @@ bool LuaManager::init(const std::string &scriptPath)
bool LuaManager::callLuaFunction(const std::string &funcName) bool LuaManager::callLuaFunction(const std::string &funcName)
{ {
if (!m_Initialized) {
return false;
}
lua_getglobal(L, funcName.c_str()); lua_getglobal(L, funcName.c_str());
if (!lua_isfunction(L, -1)) if (!lua_isfunction(L, -1))
{ {
@ -83,7 +84,9 @@ bool LuaManager::callLuaFunction(const std::string &funcName)
bool LuaManager::onUpdate(float deltaTime) bool LuaManager::onUpdate(float deltaTime)
{ {
if (!m_Initialized) {
return false;
}
// Push the 'OnUpdate' function onto the stack // Push the 'OnUpdate' function onto the stack
lua_getglobal(L, "OnUpdate"); // Ensure correct case lua_getglobal(L, "OnUpdate"); // Ensure correct case
if (!lua_isfunction(L, -1)) if (!lua_isfunction(L, -1))
@ -102,7 +105,7 @@ bool LuaManager::onUpdate(float deltaTime)
if (lua_pcall(L, 1, 0, 0) != LUA_OK) if (lua_pcall(L, 1, 0, 0) != LUA_OK)
{ {
// Retrieve the error message from Lua // Retrieve the error message from Lua
const char* luaError = lua_tostring(L, -1); const char *luaError = lua_tostring(L, -1);
if (luaError) if (luaError)
{ {
std::string errorMsg(luaError); std::string errorMsg(luaError);
@ -135,11 +138,9 @@ bool LuaManager::onUpdate(float deltaTime)
std::cerr << "Unknown error calling 'OnUpdate'.\n"; std::cerr << "Unknown error calling 'OnUpdate'.\n";
} }
lua_pop(L, 1); // Remove error message lua_pop(L, 1); // Remove error message
return false; return false;
} }
else else
{ {
@ -156,6 +157,9 @@ bool LuaManager::onDrawGui()
bool LuaManager::callFunction(const std::string &funcName, int args, int returns) bool LuaManager::callFunction(const std::string &funcName, int args, int returns)
{ {
if (!m_Initialized) {
return false;
}
lua_getglobal(L, funcName.c_str()); lua_getglobal(L, funcName.c_str());
if (!lua_isfunction(L, -1)) if (!lua_isfunction(L, -1))
{ {
@ -183,7 +187,7 @@ int lua_log_message(lua_State *L)
// Check and retrieve the message string // Check and retrieve the message string
if (!lua_isstring(L, 1)) if (!lua_isstring(L, 1))
{ {
lua_pushstring(L, "Incorrect argument to 'log_message'. Expected string as first argument."); lua_pushstring(L, "Incorrect argument to 'Engine.Log'. Expected string as first argument.");
lua_error(L); lua_error(L);
return 0; // Never reached, lua_error long jumps return 0; // Never reached, lua_error long jumps
} }
@ -222,7 +226,7 @@ int lua_log_message(lua_State *L)
} }
else else
{ {
lua_pushstring(L, "Incorrect argument to 'log_message'. Expected table for color."); lua_pushstring(L, "Incorrect argument to 'Engine.Log'. Expected table for color.");
lua_error(L); lua_error(L);
return 0; return 0;
} }

View File

@ -26,6 +26,8 @@ private:
bool m_firstCall; bool m_firstCall;
bool m_Initialized;
// Helper function to call a Lua function with no arguments and no return values // Helper function to call a Lua function with no arguments and no return values

View File

@ -11,7 +11,6 @@ extern GameObject *g_SelectedObject; // Pointer to the currently selected object
extern LoggerWindow *g_LoggerWindow; extern LoggerWindow *g_LoggerWindow;
void InspectorWindow::Show() void InspectorWindow::Show()
{ {
// Increase window/item spacing for a cleaner look // Increase window/item spacing for a cleaner look
@ -369,9 +368,11 @@ void InspectorWindow::Show()
} }
if (ImGui::Button("Reload Script")) if (ImGui::Button("Reload Script"))
{ {
script->Initialize(); if (script->Initialize())
g_LoggerWindow->AddLog("Reloaded Script: %s", ImVec4(0.0f,1.0f,0.0f,1.0f), script->ScriptPath.c_str()); {
g_LoggerWindow->AddLog("Reloaded Script: %s", ImVec4(0.0f, 1.0f, 0.0f, 1.0f), script->ScriptPath.c_str());
}
} }
} }
} }