Singleton binding in SLB
I have a singleton class and I would like to bind it to use lua. I am using SLB (Simple Lua Binder). I really don't know how to do this. All my ideas just don't work. Anyone?
void Logger::export_class_to_lua(SLB::Manager *m) {
SLB::Class< Logger, SLB::Instance::NoCopyNoDestroy >("Logger",m)
.set("getInstance",&Logger::getInstance)
.set("log",&Logger::log)
.set("info",&Logger::info)
.set("warning",&Logger::warning)
.set("error",&Logger::error)
.set("fatal",&Logger::fatal);
}
+3
source to share
1 answer
Using code:
void Logger::export_class_to_lua(SLB::Manager *m) {
SLB::Class< Logger, SLB::Instance::NoCopyNoDestroy >("Logger",m)
//.set("getInstance",&Logger::getInstance) // OMIT THIS
.set("log",&Logger::log)
.set("info",&Logger::info)
.set("warning",&Logger::warning)
.set("error",&Logger::error)
.set("fatal",&Logger::fatal);
// Next we set global variable within LUA to access the Singleton
SLB::setGlobal<Logger*>(&(*lua_State), getInstance(), "logger");
}
lua_State will be a pointer to any lua_State you create. "logger" is the object / class / variable name that you use in LUA to access the Singleton.
For example; inside LUA you would do:
logger:log("Logging some information.")
logger:error("An error has occured.")
Assuming your registration and error functions are taking const char * or whatever.
+1
source to share