Lua 5.3 -- SOL2.0 用戶指南 【2】


系列教程指南【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 libraries lua.open_libraries(sol::lib::base, sol::lib::package); lua.script("print('bark bark bark!')"); std::cout << std::endl; return 0; }


在lua_State上使用sol2 * 

對於已經使用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; }

運行lua代碼

#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 string lua.script("a = 'test'"); // load and execute from file lua.script_file("a_lua_script.lua"); // run a script, get the result int 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 work c_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; }

運行lua代碼(底層)

您可以使用單獨的加載和函數調用操作符來加載,檢查,然后運行和檢查代碼。

警告

這只是在你需要某種細粒度控制的情況下:對於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 execute sol::load_result script1 = lua.load_file("a_lua_script.lua"); //execute script1(); // load string without execute sol::load_result script2 = lua.load("a = 'test'"); //execute sol::protected_function_result script2result = script2(); // optionally, check if it worked if (script2result.valid()) { // yay! } else { // aww } sol::load_result script3 = lua.load("return 24"); // execute, get return value int value2 = script3(); c_assert(value2 == 24); return 0; }

 QQ群號: 790763256

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM