Lua 5.3 -- SOL2.0 用户指南 【1】

SOL2.2 是一个快速、简单的C++与LUA的绑定器。如果确定要在你的程序里面同时运行Lua和C++,SOL 是一个高性能的绑定器,是一个API使用方便的 GO-TO 框架。

 

简单看一下特点:这个链接到(未链接)大部分API。您也可以直接浏览API或阅读教程要了解有关usertypes的实现的详细信息,请参阅此处要了解如何处理函数参数,请参阅此注释看不到你想要的功能?向问题跟踪器发送有关特定抽象支持的请求。

 

下面的代码更多示例可以在examples目录中找到

 

#define SOL_CHECK_ARGUMENTS 1

#include <sol.hpp>
#include "../assert.hpp"

int main() {
	sol::state lua;
	int x = 0;
	lua.set_function("beep", [&x]{ ++x; });
	lua.script("beep()");
	c_assert(x == 1);

	sol::function beep = lua["beep"];
	beep();
	c_assert(x == 2);

	return 0;
}

#define SOL_CHECK_ARGUMENTS 1

#include <sol.hpp>
#include "../assert.hpp"

struct vars {
	int boop = 0;

	int bop () const {
		return boop + 1;
	}
};

int main() {
	sol::state lua;
	lua.new_usertype<vars>("vars", 
		"boop", &vars::boop,
		"bop", &vars::bop);
	lua.script("beep = vars.new()\n"
		"beep.boop = 1\n"
		"bopvalue = beep:bop()");

	vars& beep = lua["beep"];
	int bopvalue = lua["bopvalue"];

	c_assert(beep.boop == 1);
	c_assert(lua.get<vars>("beep").boop == 1);
	c_assert(beep.bop() == 2);
	c_assert(bopvalue == 2);

	return 0;
}





posted @ 2018-12-23 22:10  背灯和月就花阴  阅读(1202)  评论(0编辑  收藏  举报