[{"data":1,"prerenderedAt":170},["ShallowReactive",2],{"\u002F2018-05-09-ue4-and-sol2":3},{"id":4,"title":5,"body":6,"date":146,"description":12,"extension":147,"meta":148,"navigation":151,"path":163,"seo":164,"stem":165,"tags":166,"__hash__":169},"blogs\u002F_legacy\u002F2018\u002F2018-05-09-ue4-and-sol2.md","UE4中Sol2的接入",{"type":7,"value":8,"toc":138},"minimark",[9,13,16,19,30,35,38,41,44,47,50,53,56,60,63,66,69,72,75,86,89,93,96,99,105,108,111,114,117,120,126,129],[10,11,12],"p",{},"虽然说Lua的C++接入本身在网上能找到很多示例，但是在尝试对Sol2进行接入的时候还是遇到了一些问题，所以在此进行记录。",[10,14,15],{},"当前使用的UE4版本为4.19.1。",[10,17,18],{},"Sol2是进行Lua绑定的C++类库，过程中最主要的问题是，对Lua本身一知半解，所以没有很好的理解Sol2官方文档中所描述的一些术语。",[10,20,21,22,29],{},"之所以会选择Sol2，是因为它号称自己是[",[23,24,28],"a",{"href":25,"rel":26},"http:\u002F\u002Fsol2.readthedocs.io\u002Fen\u002Flatest\u002Fbenchmarks.html",[27],"nofollow","地上最快","]的~",[31,32,34],"h2",{"id":33},"luajit","LuaJit",[10,36,37],{},"似乎lua本身的脚本解释器有两个公开的版本，一个是LuaJit，一个是vanilla Lua。",[10,39,40],{},"由于没有仔细的对这两者之间的差别进行区分，所以并不是特别清楚其中的具体区别。",[10,42,43],{},"直接Google的话，Lua是指向vanilla lua的，而且LuaJit的Lua版本也比较靠前。",[10,45,46],{},"但是由于Sol2的官方文档中说LuaJit的速度会比较快，所以就采用了LuaJit。",[10,48,49],{},"那么首先第一步就是下载LuaJit，到了这一步就比较明了了。",[10,51,52],{},"LuaJit有提供CMake的配置文件，用CMake配置一下之后就可以进行生成了。",[10,54,55],{},"对于下一步的接入需要的是生成的Lua51.lib和Lua51.dll。",[31,57,59],{"id":58},"sol2","Sol2",[10,61,62],{},"这个库本身也有提供CMake，直接使用CMake配置就好了。",[10,64,65],{},"但是遇到的主要问题是，在和LuaJit的对接上，不过实际上在对整个库进行理清之后就不会有什么问题。",[10,67,68],{},"Sol2有提供很多的Test所以在使用方面会有很多的便利。",[10,70,71],{},"不过这里用CMake进行配置其实是不必要的，Sol2本身有提供单文件版本，其实只要include就可以了。",[10,73,74],{},"但是LuaJit的话还是需要进行链接的，并且必须根据使用的Lua解释器的不同来提供不同的宏。基本这样包含就可以了：",[76,77,82],"pre",{"className":78,"code":80,"language":81},[79],"language-text","extern \"C\"\n{\n#include \u003Clua.h>\n#include \u003Clualib.h>\n#include \u003Clauxlib.h>\n}\n\n#define SOL_USING_CXX_LUA        1\n#define SOL_USING_CXX_LUAJIT    1\n#include \"sol.hpp\"\n","text",[83,84,80],"code",{"__ignoreMap":85},"",[10,87,88],{},"最主要的是前面的Extern “C”不能忘记，不然会报很多的链接错误~",[31,90,92],{"id":91},"ue4插件接入","UE4插件接入",[10,94,95],{},"据说以前Sol2接入的话会有一些问题，因为sol2本身对check有定义，所以会造成定冲突。",[10,97,98],{},"于是sol2的作者后面给出了解决方案：",[76,100,103],{"className":101,"code":102,"language":81},[79],"#if defined(UE_BUILD_DEBUG) || defined(UE_BUILD_DEVELOPMENT) || defined(UE_BUILD_TEST) || defined(UE_BUILD_SHIPPING) || defined(UE_SERVER)\n#define SOL_INSIDE_UNREAL\n#endif \u002F\u002F Unreal Engine 4 bullshit\n\n#ifdef SOL_INSIDE_UNREAL\n#ifdef check\n#define SOL_INSIDE_UNREAL_REMOVED_CHECK\n#undef check\n#endif\n#endif \u002F\u002F Unreal Engine 4 Bullshit\n",[83,104,102],{"__ignoreMap":85},[10,106,107],{},"总之就是回避了一下：D",[10,109,110],{},"不过说到UE4兼容，最近VA也新增了对UE4的特别支持。记得一开始的时候，VA经常在对UE4的函数进行跳转的时候崩掉VS，而且还有一定几率的崩掉VA建立的代码解析缓存，然后需要重新进行一次代码解析，非常的欢乐。",[10,112,113],{},"不过新增的UE4支持还没有完全的测试过，不知实际上有什么改进还不是特别的清楚呢~",[31,115,116],{"id":116},"测试",[10,118,119],{},"最后在UE4中使用蓝图函数进行了简单的测试：",[76,121,124],{"className":122,"code":123,"language":81},[79],"UE_LOG(LogTemp,Log, TEXT(\"=== basic ===\"));\n\n\u002F\u002F create an empty lua state\nsol::state lua;\n\n\u002F\u002F by default, libraries are not opened\n\u002F\u002F you can open libraries by using open_libraries\n\u002F\u002F the libraries reside in the sol::lib enum class\nlua.open_libraries(sol::lib::base);\n\u002F\u002F you can open all libraries by passing no arguments\n\u002F\u002Flua.open_libraries();\n\n\u002F\u002F call lua code directly\nlua.script(\"print('hello world')\");\n\n\u002F\u002F call lua code, and check to make sure it has loaded and run properly:\nauto handler = &sol::script_default_on_error;\nlua.script(\"print('hello again, world')\", handler);\n\n\u002F\u002F Use a custom error handler if you need it\n\u002F\u002F This gets called when the result is bad\nauto simple_handler = [](lua_State*, sol::protected_function_result result) {\n\u002F\u002F You can just pass it through to let the call-site handle it\n  return result;\n};\n\u002F\u002F the above lambda is identical to sol::simple_on_error, but it's\n\u002F\u002F shown here to show you can write whatever you like\n\n\u002F\u002F\n{\n  auto result = lua.script(\"print('hello hello again, world') \\n return 24\", simple_handler);\n  if (result.valid()) {\n    UE_LOG(LogTemp, Log, TEXT(\"the third script worked, and a double-hello statement should appear above this one!\"));\n    int value = result;\n    ensure(value == 24);\n  }\n  else {\n    UE_LOG(LogTemp, Log, TEXT(\"the third script failed, check the result type for more information!\"));\n  }\n}\n\n\u002F**    \u003CThis test will log out 0xE24C4A03 *\u002F\n{\n  auto result = lua.script(\"does.not.exist\", simple_handler);\n  if (result.valid()) {\n    UE_LOG(LogTemp, Log, TEXT(\"the fourth script worked, which it wasn't supposed to! Panic!\"));\n    int value = result;\n    ensure(value == 24);\n  }\n  else {\n    sol::error err = result;\n    UE_LOG(LogTemp, Log, TEXT(\"the fourth script failed, which was intentional! nError: %s\"), ANSI_TO_TCHAR(err.what()));\n  }\n}\n",[83,125,123],{"__ignoreMap":85},[10,127,128],{},"由于接下来还没有具体的使用计划，所以就在这里了。",[10,130,131,132,137],{},"绑定好的Sol2插件可以到Github上下载：[",[23,133,136],{"href":134,"rel":135},"https:\u002F\u002Fgithub.com\u002FArisego\u002FSol2UE4",[27],"Sol2UE4","]，这样有需要的童鞋就没有必要重新再绑一遍了~",{"title":85,"searchDepth":139,"depth":140,"links":141},2,3,[142,143,144,145],{"id":33,"depth":139,"text":34},{"id":58,"depth":139,"text":59},{"id":91,"depth":139,"text":92},{"id":116,"depth":139,"text":116},"2018-05-09","md",{"layout":149,"status":150,"published":151,"author":152,"author_login":154,"author_email":155,"wordpress_id":156,"wordpress_url":157,"date_gmt":158,"excerpt":159},"post","publish",true,{"display_name":153,"login":154,"email":155,"url":85},"风铃","flinkor","flinkor@foxmail.com",2352,"\u002F?p=2352","2018-05-09 15:25:11 +0000",{"type":7,"value":160},[161],[10,162,12],{},"\u002F2018-05-09-ue4-and-sol2",{"title":5,"description":12},"_legacy\u002F2018\u002F2018-05-09-ue4-and-sol2",[167,59,168],"UE4","lua","AN5DDWVMtt3GIYCq4gzzCGWkoUTmUT45hP9SOx1QSL0",1788763179197]