Can we use Lua inside a cpp cocos project?

Hey guys!

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?

Anyone knows?

You can add a lua-script to Node.

auto player = Sprite::create("player.png");

auto luaComponent = ComponentLua::create("player.lua");
player->addComponent(luaComponent);

But that allows only 3 functions: onEnter, onExit, update. What if I want to call some other C++ function from Lua or vice-versa?

I blogged about this a while ago at
http://pooperpig.com/?p=150

See if that helps?!

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.

Now I gotta learn how to write Lua.

I’m not so familiar with Lua at all. I will ask the engineering team.

I DO have a cocos/scripting folder and it contains:

image

I DO NOT see ComponentLua in our published API-Ref either.

Any news on that? I also want to include lua into my pipeline and wanted to know if cocos still supports it with 3.17.x for cpp projects.

@zhangxm what is the status of ComponentLua?

If you want to use Lua, my suggestion is create Lua project.

1 Like

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.

2 Likes