Getting started on exposing lua variables to Editor GUI
This commit is contained in:
parent
29fecdd0eb
commit
9d653be08c
14
imgui.ini
14
imgui.ini
@ -93,9 +93,9 @@ DockId=0x0000001A,0
|
||||
|
||||
[Window][Lua Text Editor##LuaEditor]
|
||||
Pos=523,793
|
||||
Size=503,376
|
||||
Size=1011,376
|
||||
Collapsed=0
|
||||
DockId=0x0000001D,0
|
||||
DockId=0x00000018,0
|
||||
|
||||
[Window][Scene Window@SceneWindow]
|
||||
Pos=8,27
|
||||
@ -116,10 +116,10 @@ Collapsed=0
|
||||
DockId=0x00000011,0
|
||||
|
||||
[Window][Profiler]
|
||||
Pos=1028,793
|
||||
Size=506,376
|
||||
Pos=523,793
|
||||
Size=1011,376
|
||||
Collapsed=0
|
||||
DockId=0x0000001E,0
|
||||
DockId=0x00000018,1
|
||||
|
||||
[Table][0xE9E836E4,4]
|
||||
Column 0 Weight=1.2999
|
||||
@ -150,9 +150,7 @@ DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,50 Size=1904
|
||||
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1202,849 Split=Y Selected=0xDF0EC458
|
||||
DockNode ID=0x0000000D Parent=0x00000003 SizeRef=1202,571 Split=Y Selected=0xDFF75B3F
|
||||
DockNode ID=0x00000017 Parent=0x0000000D SizeRef=1303,764 CentralNode=1 HiddenTabBar=1 Selected=0xDFF75B3F
|
||||
DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1303,376 Split=X Selected=0x7D9E6BA2
|
||||
DockNode ID=0x0000001D Parent=0x00000018 SizeRef=503,376 HiddenTabBar=1 Selected=0x7D9E6BA2
|
||||
DockNode ID=0x0000001E Parent=0x00000018 SizeRef=506,376 HiddenTabBar=1 Selected=0x9B5D3198
|
||||
DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1303,376 Selected=0x9B5D3198
|
||||
DockNode ID=0x0000000E Parent=0x00000003 SizeRef=1202,569 Selected=0xE98146C5
|
||||
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1202,291 Selected=0x9DD4E196
|
||||
DockNode ID=0x00000002 Parent=0x00000008 SizeRef=334,1142 HiddenTabBar=1 Selected=0x36DC96AB
|
||||
|
@ -80,6 +80,8 @@ bool ScriptComponent::Initialize()
|
||||
return false;
|
||||
}
|
||||
|
||||
m_LuaManager.PrintEngineVariables();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -150,6 +150,209 @@ bool LuaManager::Initialize(const std::string &scriptPath)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Implementation of GetGlobalVariables
|
||||
std::vector<LuaManager::LuaGlobalVariant> LuaManager::GetGlobalVariables()
|
||||
{
|
||||
std::vector<LuaManager::LuaGlobalVariant> globals;
|
||||
|
||||
if (!m_LuaState)
|
||||
{
|
||||
// Handle error: Lua state not initialized
|
||||
if (g_LoggerWindow)
|
||||
{
|
||||
g_LoggerWindow->AddLog("LuaManager: Lua state is not initialized.",
|
||||
std::optional<ImVec4>(ImVec4(1.0f, 0.0f, 0.0f, 1.0f)));
|
||||
}
|
||||
else
|
||||
{
|
||||
DEBUG_PRINT("LuaManager: Lua state is not initialized.");
|
||||
}
|
||||
return globals;
|
||||
}
|
||||
|
||||
// Push the global table onto the stack
|
||||
#if LUA_VERSION_NUM >= 502
|
||||
lua_pushglobaltable(m_LuaState);
|
||||
#else
|
||||
lua_pushvalue(m_LuaState, LUA_GLOBALSINDEX);
|
||||
#endif
|
||||
|
||||
// Start iterating with a nil key
|
||||
lua_pushnil(m_LuaState); // First key
|
||||
|
||||
// Iterate over the global table
|
||||
while (lua_next(m_LuaState, -2) != 0)
|
||||
{
|
||||
// Stack now contains key at -2 and value at -1
|
||||
|
||||
// Get the type of the value
|
||||
int valueType = lua_type(m_LuaState, -1);
|
||||
|
||||
switch (valueType)
|
||||
{
|
||||
case LUA_TNUMBER:
|
||||
#if LUA_VERSION_NUM >= 503
|
||||
if (lua_isinteger(m_LuaState, -1))
|
||||
{
|
||||
lua_Integer intVal = lua_tointeger(m_LuaState, -1);
|
||||
// Assuming you want to store as int
|
||||
globals.emplace_back(static_cast<int>(intVal));
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_Number numVal = lua_tonumber(m_LuaState, -1);
|
||||
// Store as float (you might choose double instead)
|
||||
globals.emplace_back(static_cast<float>(numVal));
|
||||
}
|
||||
#else
|
||||
{
|
||||
lua_Number numVal = lua_tonumber(m_LuaState, -1);
|
||||
// Lua 5.2 and earlier use double for all numbers
|
||||
// Decide how to store: here, stored as float
|
||||
globals.emplace_back(static_cast<float>(numVal));
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
case LUA_TSTRING:
|
||||
{
|
||||
size_t len = 0;
|
||||
const char *str = lua_tolstring(m_LuaState, -1, &len);
|
||||
if (str)
|
||||
{
|
||||
globals.emplace_back(std::string(str, len));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// Optionally handle other types or skip them
|
||||
// For example, you might skip tables, functions, booleans, etc.
|
||||
default:
|
||||
// Skip other types
|
||||
break;
|
||||
}
|
||||
|
||||
// Pop the value, keep the key for the next iteration
|
||||
lua_pop(m_LuaState, 1);
|
||||
}
|
||||
|
||||
// Pop the global table from the stack
|
||||
lua_pop(m_LuaState, 1);
|
||||
|
||||
return globals;
|
||||
}
|
||||
|
||||
// Implementation of PrintEngineVariables
|
||||
void LuaManager::PrintEngineVariables() {
|
||||
if (!m_LuaState) {
|
||||
std::cerr << "LuaManager: Lua state is not initialized." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Push the _T_Engine_Variables table onto the stack
|
||||
lua_getglobal(m_LuaState, "_T_Engine_Variables");
|
||||
if (!lua_istable(m_LuaState, -1)) {
|
||||
std::cerr << "LuaManager: _T_Engine_Variables is not a table or does not exist." << std::endl;
|
||||
lua_pop(m_LuaState, 1); // Remove non-table value
|
||||
return;
|
||||
}
|
||||
|
||||
// Start iterating with a nil key
|
||||
lua_pushnil(m_LuaState); // First key
|
||||
|
||||
std::cout << "Engine Variables:" << std::endl;
|
||||
|
||||
// Iterate over the _T_Engine_Variables table
|
||||
while (lua_next(m_LuaState, -2) != 0) {
|
||||
// Stack now contains key at -2 and value at -1
|
||||
|
||||
// Get the key
|
||||
const char* key = nullptr;
|
||||
if (lua_type(m_LuaState, -2) == LUA_TSTRING) {
|
||||
key = lua_tostring(m_LuaState, -2);
|
||||
} else {
|
||||
// For non-string keys, skip
|
||||
lua_pop(m_LuaState, 1); // Remove value, keep key for next iteration
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the type of the value
|
||||
int valueType = lua_type(m_LuaState, -1);
|
||||
|
||||
// Print the key and value based on type
|
||||
std::cout << key << " = ";
|
||||
|
||||
switch (valueType) {
|
||||
case LUA_TNUMBER:
|
||||
#if LUA_VERSION_NUM >= 503
|
||||
if (lua_isinteger(m_LuaState, -1)) {
|
||||
lua_Integer intVal = lua_tointeger(m_LuaState, -1);
|
||||
std::cout << intVal;
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
lua_Number numVal = lua_tonumber(m_LuaState, -1);
|
||||
std::cout << numVal;
|
||||
}
|
||||
break;
|
||||
|
||||
case LUA_TSTRING:
|
||||
{
|
||||
const char* str = lua_tostring(m_LuaState, -1);
|
||||
if (str) {
|
||||
std::cout << "\"" << str << "\"";
|
||||
} else {
|
||||
std::cout << "nil";
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LUA_TBOOLEAN:
|
||||
{
|
||||
int boolVal = lua_toboolean(m_LuaState, -1);
|
||||
std::cout << (boolVal ? "true" : "false");
|
||||
}
|
||||
break;
|
||||
|
||||
case LUA_TTABLE:
|
||||
std::cout << "table";
|
||||
break;
|
||||
|
||||
case LUA_TFUNCTION:
|
||||
std::cout << "function";
|
||||
break;
|
||||
|
||||
case LUA_TUSERDATA:
|
||||
std::cout << "userdata";
|
||||
break;
|
||||
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
std::cout << "lightuserdata";
|
||||
break;
|
||||
|
||||
case LUA_TTHREAD:
|
||||
std::cout << "thread";
|
||||
break;
|
||||
|
||||
case LUA_TNIL:
|
||||
std::cout << "nil";
|
||||
break;
|
||||
|
||||
default:
|
||||
std::cout << "unknown";
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
// Pop the value, keep the key for the next iteration
|
||||
lua_pop(m_LuaState, 1);
|
||||
}
|
||||
|
||||
// Pop the _T_Engine_Variables table from the stack
|
||||
lua_pop(m_LuaState, 1);
|
||||
}
|
||||
|
||||
// Update function called every frame
|
||||
void LuaManager::Update(float deltaTime)
|
||||
{
|
||||
|
@ -13,6 +13,8 @@ extern "C"
|
||||
// Include standard libraries
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
// Forward declarations to avoid circular dependencies
|
||||
class Component;
|
||||
@ -57,7 +59,10 @@ public:
|
||||
|
||||
void CallLuaFunction(std::string functionName);
|
||||
|
||||
using LuaGlobalVariant = std::variant<int, float, std::string>;
|
||||
|
||||
std::vector<LuaGlobalVariant> GetGlobalVariables();
|
||||
void PrintEngineVariables();
|
||||
|
||||
private:
|
||||
// Lua state
|
||||
|
Loading…
Reference in New Issue
Block a user