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
Engine.Log("This is a blue message.", {0.0, 0.0, 1.0, 1.0})
asd
end
@ -18,6 +20,7 @@ end
function OnUpdate(deltaTime)
ticks = ticks + 1
local timestep = 5
if itterator >= timestep then

View File

@ -2,125 +2,113 @@ 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
Transform:
Position: [0, 2.79999995, -12.6000004]
Rotation: [149.699997, -137.899994, -39.2999992]
Scale: [1, 1, 1]
- 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: 5
indexCount: 36
textureID: 3
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [7.80000019, -8.10000038, -20.6000004]
Rotation: [-86.3000031, 0, -66]
Scale: [1, 1, 1]
- 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: 5
indexCount: 36
textureID: 4
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
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: 5
indexCount: 36
textureID: 5
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
Name: Sky
Components:
Transform:
Position: [0, 0, 43.2000008]
Rotation: [0, 0, 0]
Scale: [100, 100, 100]
Mesh:
vao: 5
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: 5
indexCount: 36
textureID: 3
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [-6.5, -6, -18]
Rotation: [15.8000002, -18.2000008, -11.1000004]
Scale: [1, 1, 1]
- 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: 5
indexCount: 36
textureID: 4
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
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: 5
indexCount: 36
textureID: 1
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
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: 5
indexCount: 36
textureID: 2
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
Name: Script
Components:
ScriptComponent:
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]
ScriptPath: assets/scripts/script.lua

View File

@ -75,14 +75,7 @@ bool ScriptComponent::Initialize()
// Initialize LuaManager with the script path
if (!m_LuaManager.init(ScriptPath))
{
if (g_LoggerWindow)
{
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;
}
DEBUG_PRINT("ScriptComponent: Failed to initialize LuaManager");
return false;
}

View File

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

View File

@ -26,6 +26,8 @@ private:
bool m_firstCall;
bool m_Initialized;
// 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;
void InspectorWindow::Show()
{
// Increase window/item spacing for a cleaner look
@ -369,9 +368,11 @@ void InspectorWindow::Show()
}
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());
{
if (script->Initialize())
{
g_LoggerWindow->AddLog("Reloaded Script: %s", ImVec4(0.0f, 1.0f, 0.0f, 1.0f), script->ScriptPath.c_str());
}
}
}
}