Vino: My problem is that game logic (especially logic controlling something small like an animation or a single entity) written in C++ has an unacceptably long iteration cycle to modify (like up to 2 minutes to recompile and return to the point I was at in the program) and is error-prone. Writing non-core parts of my game in LUA seems to address this problem? But I want to know if I'm walking into something that's going to create performance headaches later.
I use Lua with C++. Can't be bothered to read the original article but I get the basic idea. Of course it's possible to create a trainwreck with either Lua, or with C++. Here is my advice, speaking as a veteran:
- Use C++ for your core engine, Lua for game logic
- DO NOT try to create an exact 1:1 relationship between Lua classes and C++ classes-- what would be the point?
- What I do to reference objects, is that objects inside the core engine are simply named; so if the Lua code tries to reference an object that doesn't exist on the C++ side of things, just print an error that it couldn't find "frog_prince_23". This helps with debugging a lot, much better than leaky pointers and/or segfaults because something was removed from the game engine.
- So the api calls (from Lua to C++) just reference those names, and pass in other parameters
- You will probably have a big api with lots of calls! Price of doing business-- there is no way to glue two languages like this together for free. If you are thoughtful in specifically WHERE you need lua code to touch C++ code, it's not that bad.
- Don't try and automate the API creation with a tool-- do you really want to break your Lua code because you need to reorganize some structures on the C++ end? No. The api should make sense from the Lua side of things.
- Use *ONE* Lua state for your entire program; all lua code and objects need to be able to access all other code and lua objects directly.
I use some other more specific tricks, in particular when creating objects in the C++ engine I pass in a lua table (with possible sub-tables.) Then I have certain helper functions for dealing with those tables, pulling integers or vectors or whatnot out of them. So for instance I can do:
R = {
type = "point",
center_x = 10, center_y = 15, center_z = -5,
}
set_renderable (SpriteName, R)
The C++ code then has branches to create a type of renderable "point" (based on the type field) and then pulls the position x/y/z out with a little function luahelp_table_getv3d (L, "center")
My advice for writing lua code itself:
Lua's big weakness is also it's strength, which is that it's incredibly freely-typed. So you need to use a class system within lua, and keep your code organized and well-commented. Whereas in C++ you are going to rely on the compiler or code completion to tell you what methods are, or how data structures work, in Lua you are going to use introspection. So write a print_r (Object) that knows how to print out any kind of variables, including sub-values inside of tables.
Well, that's all for now. I love Lua! MOST of the coding I do now is in Lua, in terms of optimization you just need to be clever and put the stuff that matters into C++ but let Lua do basically all of the game logic. Keep your C++ engine like this boring bland thing that seems like it should do nothing except know how to add and remove things, draw them, compute some physics and motion, play sound effects. If your C++ engine knows whether or not you are at the title screen, it's doing too much. Do the rest in Lua!