将 c++ 静态函数从另一个 class 绑定到 Lua
Bind c++ static functions from another class to Lua
嗨!
我的问题很简单:我在 extendedgamefunctions
class:
中有一个函数
在header中:
#include "Gameitems.h"
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
extern std::vector<std::vector<Gameitems>> items;
class A
{
A();
~A();
public:
static void messagewindow(lua_State*);
};
代码是:
Functions extfunctions;
A::A()
{}
A::~A()
{}
void A::messagewindow(lua_State *L)
{
string message =lua_tostring(L,0);
extfunctions.Messagewindow(message);
}
我想绑定另一个函数 Gamefunctions
:
#include "Externedgamefunctions.h"
A egfunctions;
lua_State* L;
void Gamefunctions::luainit()
{
L = luaL_newstate();
/* load Lua base libraries */
luaL_openlibs(L);
lua_pushcfunction(L,&A::messagewindow);
lua_setglobal(L, "messagewindow");
}
另一个class的函数是静态的,我得到这个错误:
Error 5 error C2664: 'lua_pushcclosure' : cannot convert parameter 2 from 'void (__cdecl *)(lua_State *)' to 'lua_CFunction' C:\Users\Bady\Desktop\MY Game\basicconfig\BlueButterfly\BlueButterfly\Gamefunctions.cpp 170
我不想在游戏函数中添加一个加号函数来获取消息窗口(除非我没有其他方法),因为我直接编写了扩展游戏函数而不是制作一个无穷无尽的字符串怪物。
编辑:
差点忘了:Lua 5.2 和 c++11
lua_CFunction
定义为typedef int (*lua_CFunction) (lua_State *L);
,所以需要将void A::messagewindow(lua_State *L)
改为int A::messagewindow(lua_State *L)
。
嗨!
我的问题很简单:我在 extendedgamefunctions
class:
在header中:
#include "Gameitems.h"
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
extern std::vector<std::vector<Gameitems>> items;
class A
{
A();
~A();
public:
static void messagewindow(lua_State*);
};
代码是:
Functions extfunctions;
A::A()
{}
A::~A()
{}
void A::messagewindow(lua_State *L)
{
string message =lua_tostring(L,0);
extfunctions.Messagewindow(message);
}
我想绑定另一个函数 Gamefunctions
:
#include "Externedgamefunctions.h"
A egfunctions;
lua_State* L;
void Gamefunctions::luainit()
{
L = luaL_newstate();
/* load Lua base libraries */
luaL_openlibs(L);
lua_pushcfunction(L,&A::messagewindow);
lua_setglobal(L, "messagewindow");
}
另一个class的函数是静态的,我得到这个错误:
Error 5 error C2664: 'lua_pushcclosure' : cannot convert parameter 2 from 'void (__cdecl *)(lua_State *)' to 'lua_CFunction' C:\Users\Bady\Desktop\MY Game\basicconfig\BlueButterfly\BlueButterfly\Gamefunctions.cpp 170
我不想在游戏函数中添加一个加号函数来获取消息窗口(除非我没有其他方法),因为我直接编写了扩展游戏函数而不是制作一个无穷无尽的字符串怪物。
编辑: 差点忘了:Lua 5.2 和 c++11
lua_CFunction
定义为typedef int (*lua_CFunction) (lua_State *L);
,所以需要将void A::messagewindow(lua_State *L)
改为int A::messagewindow(lua_State *L)
。