I am trying to call some lua scripts from inside a cpp project. Is that possible? And if yes, can anyone tell me what all things have to be done (I am guessing we need to add some more libraries and dependencies) and a sample ‘hello world’ routine for the same? (The cpp project calls a lua function which returns a "Hello World’ string and we draw it on the screen).
Also, I don’t need all the cocos bindings in that. I don’t intend Lua to interact with cocos. I just need Lua for gameplay scripting, for example, for defining AI behavior etc. So I just want to know, is that possible, to connect lua with the cpp cocos project to exchange some values?
I was trying to find an answer to this myself (and @Maxxx’s link is dead, but it’s archived here), but I just found a project, sol for integrating C++ and Lua very painlessly.
I cloned that repo, took all the files that looked like C++ code, included it in cocos project. Then went and downloaded Lua 5.2 to make sure it worked. Then I pointed VS2013 at the lib file, and moved the .dll next to the exe (ie proj.win32/Debug.Win32/) and wrote a lua file in Resources/test.lua, and loaded the script like they do in the README there:
#include <sol.hpp>
...
sol::state lua;
int x = 0;
lua.set_function("beep", [&x]{ ++x; });
lua.script("beep()");
assert(x == 1);
//or load that specific file using
// cocos2d::FileUtils::getInstance()->getWritablePath() and cocos2d::FileUtils::getInstance()->getStringFromFile()
lua.script(content_of_file);
Now, I don’t have any of the lua bindings for cocos2dx going, because I couldn’t find the docs (and the Programmers guide tells you to use cocos2d::ComponentLUA but that doesn’t exist in my cocos folder. I don’t have cocos/scripting/ at all, so maybe I installed it wrong or something, who knows. Point is that within like an hour or less you can have lua working in any c++ project basically.
Yep, i think the component also works. But as @DarkNemesis said, it only invokes some functions because it is just a component. You can use lua project or modification the component to meet your requirements.