ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 493 forks source link

get_or() overwriting existing table in favour of creating new one #1468

Open Phi-21bit opened 1 year ago

Phi-21bit commented 1 year ago

I've been doing some testing with get_or(), it seems ideal for my use case but so far it seems to fail to recognise the existing Lua table by the same name and instead chooses to create a new one overwriting the existing one. I would like to use this syntax if possible since I can't use the [ ] style in my use case I'm assuming this function is meant to get an existing table if it exists, so I wanted to check if this is a bug or if I'm using it incorrectly

Example just for demonstration Using Visual Studio 2022, Lua 5.4.4


#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
int main()
{
    sol::state lua;
    lua.open_libraries(sol::lib::base);

    lua.script("Days = { \"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"}");

    sol::table daysOfWeek = lua.get_or("Days", lua.create_named_table("Days"));
    return 0;
}