Need some guidances in using LUA, C++, and OpenGL.

Started by
2 comments, last by apefish 14 years ago
I have studied LUA and C++ for quite sometimes now, and I am still not sure how to figure this out. I mean, from all those tutorials online, I can see that you can call LUA scripts whenever you want, and you will get back to C++ after the script is finished. However, I am still puzzled about how to use it properly. I have seen that some games such as World of Warcraft used LUA for user interface, Simcity 4 used LUA for tutorial scripting and calculating crucial values, etc. Here comes my questions that need some guidances: 1. Is it possible to share a data structure (like, for instance, a vector) between LUA and C++? 2. If I want both C++ and LUA to be able to access the same OpenGL texture or swapping buffer, how can this be done? 3. I have seen that there is a bit of performance penalty when you call LUA from C++ very often, so should almost the whole game be run in LUA? Frankly, I do not like this idea, as I want OpenGL to draw things natively. 4. Is there such thing as an advise or recommended method for using C++ & LUA, like when should we use LUA and when we should run in native C++? Thank you.
Advertisement
Typically, You would have a native representation of your objects and provide lua with a set of functions to "do things to" the given object.

The lua portion of my blog will most likely clear things up for you, as well as a lua video presentation i did.

------------------------------

redwoodpixel.com

It depends on what you want to do and what you are comfortable with. With the modern binding libraries you can every easily bind every single OpenGL function or even use one of the existing OpenGL Lua binding libraries and write all your rendering code in Lua.

That will give you great flexibility but for what purpose? Most games already have a full fledge rendering engine written in C++ and there isn't any need to write render code directly in Lua. Lua is a dynamic high level scripting language, used for complex logic which can't be easily done in C++ or other low level languages. Its combination of speed and simplicity makes it a good match for many tasks (like UI programming, core game logic, tool chain programming, etc..).

Your game will define how you need to use Lua. If say you were making a soccer game, you would only need/want to write the AI and game rules in Lua and perhaps the UI, keeping the rest of the code in C++, since that breakdown makes the most sense. If you were writing a puzzle game, you can write 80% of the game in Lua and only use the C++ for basic stuff like rendering sprites and playing sound fx etc..

Good Luck!

-ddn
4. I think the best way to think of it is that the low level native engine code is provided as a library to your Lua code. You should think of your program as being written in Lua, with performance critical and API connection stuff written in C or C++.

3. The API is pretty quick, but it does cost. For instance, you probably don't gain much by calling into C++ to do the physics update of a single object, but it would be worth it to do all of them at once. Of course there may be alot of complex logic in a big operation that you wouldn't want to do in C++.

1&2. Lua can access anything you provide access to. If you want Lua to be able to use a C++ vector, you have to think what sort of things you want to do. Indexing, adding stuff, and creation, presumably, so you would have to provide functions for those and then attach them to a Lua userdata. Keep in mind that the Lua GC won't trace inside your userdata objects, so you can't keep Lua-accessable objects inside userdata.

Most importantly, it is written as 'Lua', not 'LUA'. Lua is not an acronym, it means 'moon' in portugese.

This topic is closed to new replies.

Advertisement