Script cutscenes, quests... with Lua or JS

Hi,

Hi,

I’m working on a game using cocos2d-x. All the code is written in c++ but now we would like to use lua or javascript only in certain parts of the code. For example in a cutscene system.

I know that cocos2dx support these two languages, and there are tons of tutorials that explains how to write a game using only one of these languages for the whole game. But in my case I want to run some scripts when a trigger is activated and then continue with the c++ code.

I don’t know where start. Anybody knows a good tutorial which covers this?

Thanks in advance

Based on what your described. lua is better integrated with cocos2d-x c++, we’ll slowly moving js support back.

You’ll need the binding generator to generate the binding you need. And modify the binding templates

Thanks!

I’m trying to initializing all the stuff in order to support lua. After include the cocos2d_lua_bindings project, I added these two lines of code:

bool AppDelegate::applicationDidFinishLaunching()
{

auto engine = LuaEngine::getInstance();

ScriptEngineManager::getInstance()->setScriptEngine(engine);

but the app crashs since _state is null after execute this code

bool LuaStack::init(void)
{
_state = lua_open();

What is wrong?

Thanks

How do you get LuaStack

When I call

auto engine = LuaEngine::getInstance();

the static create method is called:

LuaEngine* LuaEngine::getInstance(void)
{
if (!_defaultEngine)
{
_defaultEngine = new LuaEngine();
_defaultEngine->init();
}
return _defaultEngine;
}

and the init method calls LuaStack::create()

bool LuaEngine::init(void)
{
_stack = LuaStack::create();
_stack->retain();
executeScriptFile(“DeprecatedEnum.lua”);
executeScriptFile(“DeprecatedClass.lua”);
executeScriptFile(“Deprecated.lua”);
return true;
}

and finally, this ,method calls LuaStack::init() where the app crashes.

LuaStack *LuaStack::create(void)
{
LuaStack *stack = new LuaStack();
stack->init();
stack->autorelease();
return stack;
}

I’m compiling in mac and I found this post:

Should I compile only for 32 bits?

Thanks

What version of cocos2d-x are you using?

Also can you create a lua project with command cocos new -l lua, and see what’s the difference between default lua project and your project?

Fixed!

For 64 bits its necessary additional linker flags

Thanks

1 Like

Hi,

OK, lua is working and I can execute a script from a file… but I have more questions.

In c++ I have some classes that inherit from a GameObject class and use components. My idea is manage the scripts encapsulating them into components. In this way a GameObject could have one or more scripts, these scripts could inherit from a super script with methods like update, onEnter, onExit… so the GameObject could call from the c++ code the proper lua functions when an event occurs.

But I need more information about these topics:

How to create lua classes in .lua files and how to create different lua object instances from the same class.

how to link these instances to the c++ parent object. So the c++ code would call the member methods of its scripts and this lua code would update the attributes of its own lua instances and the lua code would call the c++ methods of its GameObject parent.

How to call the different methods of these instances. By now I only know how to run a whole .lua script

Sorry for all of these questions. I know that this sounds very complex and I’m a newby with lua.

Thanks

Your idea is really good, actually that’s how a lot of AAA game work, use lua as scripting/AI layer

invoke lua from c++ is actually not that hard

Here is some good read about binding c++ instance to lua using the self

Basically as long as a lua object have the pointer to the c++ instance, you can call all the functions on the c++ instance, vise-versa, if a c++ instance have access to the lua object, it can always invoke lua functions.

I’m not an export on lua, but you can always ask the folks knows lua here.
http://discuss.cocos2d-x.org/category/cocos2d-x/lua