系列教程指南【1】
注意
在你学习了sol的基础知识之后,建议你如果认为某些东西可以运行,你应该尝试一下。它可能会运行!
以下所有代码均可在sol2教程示例中找到。
The implementation for assert.hpp with c_assert looks like so:
你需要代码中#include<sol.hpp>#include "sol.hpp"。SOL只有头文件,所以你不需要编译任何东西。但是,Lua必须编译并可用。有关更多详细信息,请参阅入门教程。
assert.hpp 和 c_assert 的实现看起来像这样:
#ifndef EXAMPLES_ASSERT_HPP #define EXAMPLES_ASSERT_HPP # define m_assert(condition, message) do { if (! (condition)) { std::cerr << "Assertion `" #condition "` failed in " << __FILE__ << " line " << __LINE__ << ": " << message << std::endl; std::terminate(); } } while (false) # define c_assert(condition) do { if (! (condition)) { std::cerr << "Assertion `" #condition "` failed in " << __FILE__ << " line " << __LINE__ << std::endl; std::terminate(); } } while (false) #else # define m_assert(condition, message) do { if (false) { (void)(condition); (void)sizeof(message); } } while (false) # define c_assert(condition) do { if (false) { (void)(condition); } } while (false) #endif #endif // EXAMPLES_ASSERT_HPP
这是下面示例代码中使用的断言。
#define SOL_CHECK_ARGUMENTS 1 #include <sol.hpp> #include <iostream> #include "../../assert.hpp" int main(int, char*[]) {std::cout << "=== opening a state ===" << std::endl;sol::state lua;// open some common librarieslua.open_libraries(sol::lib::base, sol::lib::package);lua.script("print('bark bark bark!')");std::cout << std::endl;return 0; }
对于已经使用Lua或使用其他的Lua系统(LuaBridge,kaguya,Luwra等)的系统/游戏,你仍然会喜欢sol2的:
#define SOL_CHECK_ARGUMENTS 1 #include <sol.hpp> #include <iostream> int use_sol2(lua_State* L) {sol::state_view lua(L);lua.script("print('bark bark bark!')");return 0; } int main(int, char*[]) {std::cout << "=== opening sol::state_view on raw Lua ===" << std::endl;lua_State* L = luaL_newstate();luaL_openlibs(L);lua_pushcclosure(L, &use_sol2, 0);lua_setglobal(L, "use_sol2");if (luaL_dostring(L, "use_sol2()")) {lua_error(L);return -1;}std::cout << std::endl;return 0; }
#define SOL_CHECK_ARGUMENTS 1 #include <sol.hpp> #include <fstream> #include <iostream> #include "../../assert.hpp" int main(int, char*[]) {std::cout << "=== running lua code ===" << std::endl;sol::state lua;lua.open_libraries(sol::lib::base);// load and execute from stringlua.script("a = 'test'");// load and execute from filelua.script_file("a_lua_script.lua");// run a script, get the resultint value = lua.script("return 54");c_assert(value == 54);
//要运行Lua代码但需有错误处理程序以防出现问题:
auto bad_code_result = lua.script("123 herp.derp", [](lua_State*, sol::protected_function_result pfr) {// pfr will contain things that went wrong, for either loading or executing the script// Can throw your own custom error// You can also just return it, and let the call-site handle the error if necessary.return pfr;});// it did not workc_assert(!bad_code_result.valid());// the default handler panics or throws, depending on your settings// uncomment for explosions://auto bad_code_result_2 = lua.script("bad.code", &sol::script_default_on_error);return 0; }
您可以使用单独的加载和函数调用操作符来加载,检查,然后运行和检查代码。
警告
这只是在你需要某种细粒度控制的情况下:对于99%的情况,运行lua代码是首选,并避免在不理解脚本/加载与需要在加载后运行块之间的差异时的缺陷。
#define SOL_CHECK_ARGUMENTS 1 #include <sol.hpp> #include <fstream> #include <iostream> #include <cstdio> #include "../../assert.hpp" int main(int, char*[]) {std::cout << "=== running lua code (low level) ===" << std::endl;sol::state lua;lua.open_libraries(sol::lib::base);// load file without executesol::load_result script1 = lua.load_file("a_lua_script.lua");//executescript1();// load string without executesol::load_result script2 = lua.load("a = 'test'");//executesol::protected_function_result script2result = script2();// optionally, check if it workedif (script2result.valid()) {// yay!}else {// aww}sol::load_result script3 = lua.load("return 24");// execute, get return valueint value2 = script3();c_assert(value2 == 24);return 0; }
QQ群号: 790763256
相关知识
一般来说,我们会lua字符串中的转义序列中的()的来添加双引号
400万 融资融券利率5.3
这个智能花盆,能让你像养宠物一样种花
《西秀区中医养生健康讲座》(八)春季养生食物·菊花5.3
Prospora游戏策略:掌握孢子群进化与防御
Pytorch介绍与linux、windows环境下安装
霍城县5.3万余亩薰衣草花盛开 游客陶醉紫色花海
口袋妖怪究极绿宝石5.3华丽大赛评分标准详解
合集—次性认识36种康乃馨 你认识多少康乃馨,快来看看吧
花是有喜怒哀乐的,让你轻松读懂植物“心情”
网址: Lua 5.3 https://m.huajiangbk.com/newsview1163518.html
上一篇: Minecraft 游戏新手指南 |
下一篇: 教你学会如何制作一朵简单的毛线花 |