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?
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.
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.