Hey there
I'm trying to make a custom server for a certain type of game (the servers are really simple), and I wanted to make it based on a Lua plugins and gamemodes system, like how Garrys Mod servers function. Each plugin is one lua file.
So far, I have a Plugin class (one per plugin), that each has its own instance of the Lua interpreter to isolate them. I have successfully made them load their lua files and execute, so that's not the issue.
What is the issue is registering C++ functions to each plugin. For instance, I made a function to replace the normal print() function (this function does the same thing, but puts [PluginName]: in front of its outputted text. Here it is:
int Plugin::Print(lua_State *L) {
int numArgs = lua_gettop(L);
printf(pluginName);
for (int i=1; i <= numArgs; i++) {
printf(lua_tostring(L, i));
}
printf("\n");
return 0;
}
pluginName is a const char* private member of Plugin. And Lua doesn't like non-static functions (And this can't be static because it has to access members of "this").
So my problem is what to do about this. I hope by this point you see exactly what I'm trying to accomplish. I've tried toLua++ and luaBind, but they both seemed very complex, and I was unable to build either of them. Other libraries don't seem to do what I want. What should I do?
In case it helps you see my situation, here is the rest of the Plugin class.
Here's Plugin.h:
#ifndef _PLUGIN_H
#define _PLUGIN_H
/* ============================================
The Plugin class represents an indivitual
plugin, which is one lua file. Each plugin
has its own instance of Lua.
============================================ */
#pragma comment(lib, "lua/lua51.lib")
extern "C" {
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
}
class Plugin {
public:
Plugin();
~Plugin();
void Load(const char *pluginFileName, const char *name);
private:
lua_State *Lua;
const char *pluginName;
void RegisterLua();
/* Lua Functions */
static void Plugin::Print(const char *str);
static int Plugin::Print_Glue(lua_State *L);
/* ! Lua Functions */
};
#endif
And here's Plugin.cpp:
#include "Plugin.h"
#include "Lua_Functions.h"
Plugin::Plugin() {
}
Plugin::~Plugin() {
lua_close(Lua);
}
void Plugin::Load(const char* pluginFileName, const char *name) {
Lua = lua_open();
if(Lua == NULL) {
return;
}
luaL_openlibs(Lua);
pluginName = name;
RegisterLua();
luaL_dofile(Lua, pluginFileName);
}
Thank you for your time.
-Drew