I agree with you that it’s too complex, not documented well enough, or just isn’t built to allow adding the Javascript/Lua runtime into a game originally created with c++ and it definitely should be both possible and not to difficult to setup a simple command in the cocos tool to copy over the needed files or link to the correct libraries (or library projects) in the same manner as occurs when you specify a Lua/JS project type in the original cocos create.
That said, have you tried to create a new project with JS and then tested copying over all your C++ files and running the c++ game scene instead of the default runScript(script_scene) instead?
Scratch that: I just created one. It’s definitely possible.
It should be easier to add lua into c++ for scripting, without doing it this way, and so you can not include some of the bindings if you don’t need them.
Create Project
cocos new -p com.p.cpp-lua-test -l lua -d ~/dev/tests cpp-lua-test
AppDelegate
// ...
#include "CCComponentLua.h"
// ...
#else
if (engine->executeScriptFile("src/main.lua"))
{
log("ERROR EXEC main.lua");
return false;
}
#endif
// Add test and run scene from c++ instead of main.lua
auto scene = Scene::create();
{
auto sprite = Sprite::create();
sprite->setTextureRect(Rect(0,0,100,100));
sprite->setColor(Color3B::WHITE);
scene->addChild(sprite);
// create a Sprite and add a LUA component
auto luaComponent = ComponentLua::create("src/player.lua");
sprite->addComponent(luaComponent);
log("created!");
// test c++ side actions
scene->scheduleOnce([sprite](float dt){
log(dt);
auto move = MoveBy::create(.5f, Vec2(100,20));
auto seq = Sequence::create(move, move->reverse(), nullptr);
sprite->runAction(RepeatForever::create(seq));
}, 2.f, "asdf");
}
Director::getInstance()->runWithScene(scene);
src/player.lua
local player = {
onEnter = function(self)
print("entering")
local director = cc.Director:getInstance()
local winSize = director:getVisibleSize()
local visibleOrigin = director:getVisibleOrigin()
local me = self:getOwner()
local contentSize = me:getContentSize()
me:setPosition(winSize.width/2 - contentSize.width/2 + visibleOrigin.x,
winSize.height/2 - contentSize.height/2 + visibleOrigin.y)
end,
onExit = function(self)
print("exiting")
end,
update = function(self)
-- on update
local color = cc.c3b(math.random() * 255, math.random() * 255, math.random() * 255)
self:getOwner():setColor(color)
end
}
print("test")
-- it is needed to return player to let c++ nodes know it
return player
src/main.lua (comment out the lua side mvc app and scene creation)
local function main()
--require("app.MyApp"):create():run()
math.randomseed(os.time())
end