Why and when we need Lua with cocos2d-x?

Cocos2d-x is in C++ and this is very good as it is the fastest available language that comes after assembly. Now I don’t understand why we need Lua scripting in it? Doesn’t it break the performance? If I should write a code in Lua, then I will prefer to write instead in C# with Unity3D. But I chose cocos2d-x as far as it is in C++. Please tell me what is the meaning of Lua.

1 Like

I don’t know what’s the reason of using Lua for coding the entire game (which is shown in the samples).
As for me I’d use Lua for coding small parts of the game like behaviour of the enemies. However, I didn’t try this yet, so I can’t give any suggestions concerning the implementation.

As @dotsquid said, it’s useful for logic and things that you would want to tinker with without having to recompile the entire project over and over again.

As far as i know It is also good to do somethings that are really hard in C++ like metaprogramming, reflection etc due to its highly dynamic nature

In China, many game studios choose Lua mainly for the freedom to update their game without the permission of Apple. You know Apple audit a app need at least one week which is not acceptable.

If the game have some bugs, we could upload the zip package on the server and all users could update the game without re-download the game.

Another reason to choose Lua is its easy to learn to easy to use feature.

@owen in this case I don’t understand the meaning of Apple audit. If I can change (and I definitely can!) the way app operates, what is the meaning of the checking.

Also as I understand it is the same as you have used and interpreting scripting language to develop a game, which is, of course, slow. I guess is its a trade off between flexibility and speed. If you not really need much flexibility, you should use C++ and compile each time you change anything in the logic. Is this correct?

@naghekyan - When you submit an app to the Apple App Store. They review it for many things. Making sure the app does what is says, you aren’t hiding functionality in it, you are not using a beta OS and beta Xcode, you are not using private API’s, etc. This takes a week or more, usually for them to review your binary, approve it and put it up on the app store. Even minor updates/point releases are subject to this review time and scrutiny.

@naghekyan
@slackmoehrle has made things more clearer. By using Lua, we could do ad-hoc update to our games.

When programming games with Lua, we could achieve WYSWYG.

@owen Yes @slackmoehrle made it more clear, but did not answer my question. If I can download a script from server and run that and it will change my app from A app to B app and they can’t already review if B app is acceptable, what is the meaning of A app review. I can always send A app for review and when it passes the review I change it to B app, isn’t it?

Not actually. We could only use scripts to improve our app but we can’t change a app totally from A to B. The core of the app can’t be changed. Only the behavior of the app could be scripted.

In theory you can change the app in its entirety. Imagine if you bonded the entire ui kit api in lua.
In fact that is what frameworks like titanium do. They bind the entire sdk of ios and android and expose through javascript. So you can remotely update your app to a completely different one if you want.
But of course it has its trade offs. It will be inherently slower for example

i’ve been trying Lua lately and this is what i think about it so far:

  • the language is way less verbose, this makes it easier and faster to code
  • you can swap the script without recompiling, so you get to see your changes much faster. This also makes it faster to develop, specially as your game gets bigger and is really useful when prototyping.
  • the language is simple and concise, so some people may prefer to code in it rather than C++ which is known for being way more complicated