Getting started on exposing lua variables to Editor GUI
This commit is contained in:
parent
29fecdd0eb
commit
9d653be08c
72
imgui.ini
72
imgui.ini
@ -93,9 +93,9 @@ DockId=0x0000001A,0
|
|||||||
|
|
||||||
[Window][Lua Text Editor##LuaEditor]
|
[Window][Lua Text Editor##LuaEditor]
|
||||||
Pos=523,793
|
Pos=523,793
|
||||||
Size=503,376
|
Size=1011,376
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x0000001D,0
|
DockId=0x00000018,0
|
||||||
|
|
||||||
[Window][Scene Window@SceneWindow]
|
[Window][Scene Window@SceneWindow]
|
||||||
Pos=8,27
|
Pos=8,27
|
||||||
@ -116,10 +116,10 @@ Collapsed=0
|
|||||||
DockId=0x00000011,0
|
DockId=0x00000011,0
|
||||||
|
|
||||||
[Window][Profiler]
|
[Window][Profiler]
|
||||||
Pos=1028,793
|
Pos=523,793
|
||||||
Size=506,376
|
Size=1011,376
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x0000001E,0
|
DockId=0x00000018,1
|
||||||
|
|
||||||
[Table][0xE9E836E4,4]
|
[Table][0xE9E836E4,4]
|
||||||
Column 0 Weight=1.2999
|
Column 0 Weight=1.2999
|
||||||
@ -128,36 +128,34 @@ Column 2 Weight=0.6474
|
|||||||
Column 3 Weight=1.0088
|
Column 3 Weight=1.0088
|
||||||
|
|
||||||
[Docking][Data]
|
[Docking][Data]
|
||||||
DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,50 Size=1904,1142 Split=X Selected=0xF7365A5A
|
DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,50 Size=1904,1142 Split=X Selected=0xF7365A5A
|
||||||
DockNode ID=0x00000013 Parent=0x14621557 SizeRef=513,1142 Split=Y Selected=0x818D04BB
|
DockNode ID=0x00000013 Parent=0x14621557 SizeRef=513,1142 Split=Y Selected=0x818D04BB
|
||||||
DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,456 Split=Y Selected=0x1D5D92B6
|
DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,456 Split=Y Selected=0x1D5D92B6
|
||||||
DockNode ID=0x00000019 Parent=0x0000001B SizeRef=264,366 HiddenTabBar=1 Selected=0x1D5D92B6
|
DockNode ID=0x00000019 Parent=0x0000001B SizeRef=264,366 HiddenTabBar=1 Selected=0x1D5D92B6
|
||||||
DockNode ID=0x0000001A Parent=0x0000001B SizeRef=264,364 HiddenTabBar=1 Selected=0x1C0788A1
|
DockNode ID=0x0000001A Parent=0x0000001B SizeRef=264,364 HiddenTabBar=1 Selected=0x1C0788A1
|
||||||
DockNode ID=0x0000001C Parent=0x00000013 SizeRef=264,254 HiddenTabBar=1 Selected=0x818D04BB
|
DockNode ID=0x0000001C Parent=0x00000013 SizeRef=264,254 HiddenTabBar=1 Selected=0x818D04BB
|
||||||
DockNode ID=0x00000014 Parent=0x14621557 SizeRef=749,1142 Split=X
|
DockNode ID=0x00000014 Parent=0x14621557 SizeRef=749,1142 Split=X
|
||||||
DockNode ID=0x00000015 Parent=0x00000014 SizeRef=371,1142 Split=X
|
DockNode ID=0x00000015 Parent=0x00000014 SizeRef=371,1142 Split=X
|
||||||
DockNode ID=0x00000011 Parent=0x00000015 SizeRef=265,1142 Selected=0x1D5D92B6
|
DockNode ID=0x00000011 Parent=0x00000015 SizeRef=265,1142 Selected=0x1D5D92B6
|
||||||
DockNode ID=0x00000012 Parent=0x00000015 SizeRef=1259,1142 Split=X
|
DockNode ID=0x00000012 Parent=0x00000015 SizeRef=1259,1142 Split=X
|
||||||
DockNode ID=0x00000009 Parent=0x00000012 SizeRef=364,1142 Split=Y Selected=0x3DC5AC3F
|
DockNode ID=0x00000009 Parent=0x00000012 SizeRef=364,1142 Split=Y Selected=0x3DC5AC3F
|
||||||
DockNode ID=0x00000005 Parent=0x00000009 SizeRef=364,745 Split=Y Selected=0x3DC5AC3F
|
DockNode ID=0x00000005 Parent=0x00000009 SizeRef=364,745 Split=Y Selected=0x3DC5AC3F
|
||||||
DockNode ID=0x0000000B Parent=0x00000005 SizeRef=364,452 HiddenTabBar=1 Selected=0x3DC5AC3F
|
DockNode ID=0x0000000B Parent=0x00000005 SizeRef=364,452 HiddenTabBar=1 Selected=0x3DC5AC3F
|
||||||
DockNode ID=0x0000000C Parent=0x00000005 SizeRef=364,291 Selected=0xAE3C694A
|
DockNode ID=0x0000000C Parent=0x00000005 SizeRef=364,291 Selected=0xAE3C694A
|
||||||
DockNode ID=0x00000006 Parent=0x00000009 SizeRef=364,395 HiddenTabBar=1 Selected=0x726D8899
|
DockNode ID=0x00000006 Parent=0x00000009 SizeRef=364,395 HiddenTabBar=1 Selected=0x726D8899
|
||||||
DockNode ID=0x0000000A Parent=0x00000012 SizeRef=1538,1142 Split=X
|
DockNode ID=0x0000000A Parent=0x00000012 SizeRef=1538,1142 Split=X
|
||||||
DockNode ID=0x00000007 Parent=0x0000000A SizeRef=357,1142 Selected=0x7737E8B2
|
DockNode ID=0x00000007 Parent=0x0000000A SizeRef=357,1142 Selected=0x7737E8B2
|
||||||
DockNode ID=0x00000008 Parent=0x0000000A SizeRef=1545,1142 Split=X
|
DockNode ID=0x00000008 Parent=0x0000000A SizeRef=1545,1142 Split=X
|
||||||
DockNode ID=0x00000001 Parent=0x00000008 SizeRef=1202,1142 Split=Y Selected=0xDF0EC458
|
DockNode ID=0x00000001 Parent=0x00000008 SizeRef=1202,1142 Split=Y Selected=0xDF0EC458
|
||||||
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1202,849 Split=Y Selected=0xDF0EC458
|
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=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=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=0x00000018 Parent=0x0000000D SizeRef=1303,376 Selected=0x9B5D3198
|
||||||
DockNode ID=0x0000001D Parent=0x00000018 SizeRef=503,376 HiddenTabBar=1 Selected=0x7D9E6BA2
|
DockNode ID=0x0000000E Parent=0x00000003 SizeRef=1202,569 Selected=0xE98146C5
|
||||||
DockNode ID=0x0000001E Parent=0x00000018 SizeRef=506,376 HiddenTabBar=1 Selected=0x9B5D3198
|
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1202,291 Selected=0x9DD4E196
|
||||||
DockNode ID=0x0000000E Parent=0x00000003 SizeRef=1202,569 Selected=0xE98146C5
|
DockNode ID=0x00000002 Parent=0x00000008 SizeRef=334,1142 HiddenTabBar=1 Selected=0x36DC96AB
|
||||||
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1202,291 Selected=0x9DD4E196
|
DockNode ID=0x00000016 Parent=0x00000014 SizeRef=376,1142 HiddenTabBar=1 Selected=0x8D0E8380
|
||||||
DockNode ID=0x00000002 Parent=0x00000008 SizeRef=334,1142 HiddenTabBar=1 Selected=0x36DC96AB
|
DockSpace ID=0xC6145A92 Pos=8,27 Size=1904,1142 Split=X
|
||||||
DockNode ID=0x00000016 Parent=0x00000014 SizeRef=376,1142 HiddenTabBar=1 Selected=0x8D0E8380
|
DockNode ID=0x0000000F Parent=0xC6145A92 SizeRef=301,1142 Selected=0xA8433A03
|
||||||
DockSpace ID=0xC6145A92 Pos=8,27 Size=1904,1142 Split=X
|
DockNode ID=0x00000010 Parent=0xC6145A92 SizeRef=1601,1142 CentralNode=1
|
||||||
DockNode ID=0x0000000F Parent=0xC6145A92 SizeRef=301,1142 Selected=0xA8433A03
|
|
||||||
DockNode ID=0x00000010 Parent=0xC6145A92 SizeRef=1601,1142 CentralNode=1
|
|
||||||
|
|
||||||
|
@ -80,6 +80,8 @@ bool ScriptComponent::Initialize()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_LuaManager.PrintEngineVariables();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,6 +150,209 @@ bool LuaManager::Initialize(const std::string &scriptPath)
|
|||||||
return true;
|
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
|
// Update function called every frame
|
||||||
void LuaManager::Update(float deltaTime)
|
void LuaManager::Update(float deltaTime)
|
||||||
{
|
{
|
||||||
|
@ -13,6 +13,8 @@ extern "C"
|
|||||||
// Include standard libraries
|
// Include standard libraries
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <variant>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
// Forward declarations to avoid circular dependencies
|
// Forward declarations to avoid circular dependencies
|
||||||
class Component;
|
class Component;
|
||||||
@ -57,7 +59,10 @@ public:
|
|||||||
|
|
||||||
void CallLuaFunction(std::string functionName);
|
void CallLuaFunction(std::string functionName);
|
||||||
|
|
||||||
|
using LuaGlobalVariant = std::variant<int, float, std::string>;
|
||||||
|
|
||||||
|
std::vector<LuaGlobalVariant> GetGlobalVariables();
|
||||||
|
void PrintEngineVariables();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Lua state
|
// Lua state
|
||||||
|
Loading…
Reference in New Issue
Block a user