Page 1 of 1

wiring Lua into C++

Posted: Fri Dec 11, 2009 9:33 pm
by TheBuzzSaw
I understand the C++/Lua relationship and how it should be applied, but I am very new to the process of integration. How did you learn? Are there good tutorials out there? I am looking for the actual implementation process. For instance, what library should I use to bind the two? Where in the game loop is Lua invoked? Should any of it be threaded?

Re: wiring Lua into C++

Posted: Sat Dec 12, 2009 4:05 am
by K-Bal
The simple question is: what do you want to do with Lua?

Don't take it personally, but if you really were in the situation where you need something like Lua, you probably would be advanced enough to know how to apply it ;)

Re: wiring Lua into C++

Posted: Sat Dec 12, 2009 12:05 pm
by Falco Girgis
TheBuzzSaw wrote:I understand the C++/Lua relationship and how it should be applied, but I am very new to the process of integration. How did you learn? Are there good tutorials out there? I am looking for the actual implementation process. For instance, what library should I use to bind the two? Where in the game loop is Lua invoked? Should any of it be threaded?
The "how do you learn" part is kinda hazy. There's not really any halfway complete guide to helping you do it.

I spent a very large amount of time at http://lua.org looking at the official documentation. Then google something like "invoking Lua from C++" to see the process. It's basically instantiate the Lua VM, send it a stream of characters to interpret/execute, then close it when you're done.

As for binding libraries, it really all depends. They aren't required. You can write your own bindings from C++ to Lua. And while it's really a pain in the ass, I think that I would recommend at least getting the basics of that down (passing things to and from Lua's stack), before turning to a binding library--that stuff is the basis for communication between the two. Also, I use toLua++, which (like all binding libraries) has certain limitations. Since I know what I'm doing, I can alter the outputted bindings if I need something that toLua++ doesn't support.

Now about the binding libraries. There's no clear-cut best one. M_D_K on these forums uses SWIG. I use toLua++, and Trufun (of Golvellius 3D fame) uses LuaBind. I had originally looked into LuaBind for Elysian Shadows, but I found that it had a huge amount of overhead and additional dependencies (from the Boost library), so I opted to use toLua++. LuaBind produces bindings at compile-time using metatemplate programming (or whatever). This is kind of new to C++ (so the standard isn't really... standardized). GCC on the Dreamcast was too old to handle it.

Basically it boils down to:
LuaBind - Generates bindings at compile-time from your C++ code. Compiling takes longer, and there is a pretty hefty dependency, but it is pretty damn convenient.
toLua++ - Console-application that generates bindings from C++ headers that you pass to it. It outputs a <whatever>_bind.c that you link with your application. Very little overhead, very light weight.

Now to answer your final question about where to invoke Lua from the engine: it really depends. What are you using Lua to do? I'll give you an example in ES. Each level has a level.lua, which has any special scripting in the level. Maybe events triggered by picking up a key and opening a door, maybe an event triggered by the player walking on a certain tile, or maybe just updating some sort of animation. This script is invoked when the engine updates the level.

Then we have items with their own "use" scripts, which are more or less something like

Code: Select all

Player.hp = Player.hp + 20
for something like a healing potion. These are invoked when the player selects an item and hits use on the menu.

ES also has enemies/NPCs powered by AI scripts. These are invoked when each enemy/NPC's Update() is invoked.

Creating an engine that really takes advantage of scripting is an art. I've seen far too many people who implement Lua and basically brute force everything via Lua script while the engine does nothing but render and maybe do some collision checks. The engine is the thing powering the game--not Lua. Lua should serve as a logical extension, so that you aren't hard-coding content-specific logic into an engine. An engine relying on Lua for too many things is weaker than an engine that doesn't even support scripting.

Oh, and for the record, the Lua VM is not getting its own thread. There's really not a need to bother with multithreading (usually).

Re: wiring Lua into C++

Posted: Sat Dec 12, 2009 5:13 pm
by TheBuzzSaw
Thanks, Gyro. I had no idea that the libraries were just "helpers". I would certainly love to implement my own binding. I am confident that I could; I just need to know where to start. Thanks for the detailed response.
K-Bal wrote:The simple question is: what do you want to do with Lua?

Don't take it personally, but if you really were in the situation where you need something like Lua, you probably would be advanced enough to know how to apply it ;)
This makes no sense whatsoever. Replace "Lua" with "3D graphics" and you'd see how absurd that statement is. I know exactly what I want Lua to do, but I have never coded a scripting language combo before. I just wanted some external wisdom before I tackled the idea.

Re: wiring Lua into C++

Posted: Sat Dec 12, 2009 5:59 pm
by K-Bal
Sorry if my post was not what you were looking for. However, you gave us no information on what you are going to do. So the eternal question is: What do you want Lua for? ;)

Re: wiring Lua into C++

Posted: Sat Dec 12, 2009 8:38 pm
by TheBuzzSaw
K-Bal wrote:Sorry if my post was not what you were looking for. However, you gave us no information on what you are going to do. So the eternal question is: What do you want Lua for? ;)
I avoided answering the question because of the insulting implication that followed it. "If you know what you want, you should know how to do it already." Brilliant logic.

I understand how Lua works together with C++ to form a sweet combination. I don't know where to start in terms of wiring Lua into C++ to do anything. It really does not matter what I want to do with it at this point. Again, it's like you refusing to help someone start 3D graphics because they don't know what they want to do with the 3D graphics yet. You can't just teach them how to draw simple geometry in the meantime???

If I wanna share all the details behind my project, I'll do that. Until then, leave it alone.

Re: wiring Lua into C++

Posted: Sun Dec 13, 2009 3:59 am
by K-Bal
Okay, I just wanted to differentiate between these two:
Guy Nr.1: "I want to make the next awesome RPG. How do I use Lua?"
and
Guy Nr.2: "I'm not satisfied with the boss fights in my game. I want to make them more flexible and have the possibility to alter them without recompiling. etc."
The second guy has already come so far that he will certainly find a way for himself to implement scripting. He has touched the limits of a compiled language and therefore feels the need of integrating scripting.

Personally, I've used Luabind. If you are on Linux it's absolutely no problem to solve dependency issues because of the package management. I've also found it really easy to use. This tutorial teaches the basics: http://www.nuclex.org/articles/cxx/1-qu ... to-luabind.

Ciao,
Marius

Re: wiring Lua into C++

Posted: Thu Dec 17, 2009 9:29 pm
by thbp
has any one posted this yet? (the lua book?) http://books.google.com/books?id=ZV5hXZ ... &q=&f=true

Re: wiring Lua into C++

Posted: Sat Jan 23, 2010 5:02 pm
by DeltaFolee
thbp wrote:has any one posted this yet? (the lua book?) http://books.google.com/books?id=ZV5hXZ ... &q=&f=true
I purchased this book to learn the basics of Lua and the API. If you're going for binding everything yourself, this book does a great job of explaining the C API.

Re: wiring Lua into C++

Posted: Sun Feb 28, 2010 12:19 am
by TheBuzzSaw
I am reporting back with grand success. I still have much yet to learn, but I have become rather comfortable with Lua's virtual stack. The original C API seems to be the best for my needs. I actually hesitate to dig into any of the expanded libraries.

Gyro, if you're still reading this, can you tell me why you use toLua++? What did you gain by using this wrapper?

Re: wiring Lua into C++

Posted: Sun Feb 28, 2010 9:05 am
by Falco Girgis
TheBuzzSaw wrote:I am reporting back with grand success. I still have much yet to learn, but I have become rather comfortable with Lua's virtual stack. The original C API seems to be the best for my needs. I actually hesitate to dig into any of the expanded libraries.

Gyro, if you're still reading this, can you tell me why you use toLua++? What did you gain by using this wrapper?
Not having to spend hours wrapping things myself. It handles things very gracefully (including error checking), inheritance, encapsulation, polymorphism, and actually gives Lua the ability to use "classes" and other OO things.

Re: wiring Lua into C++

Posted: Sun Feb 28, 2010 11:35 am
by TheBuzzSaw
Cool.

In Elysian Shadows, how often are you calling Lua? Are you still sticking to your paradigm of calling it once per frame (in addition to triggers and whatnot)? In my project, I seem to be headed down that path.

Re: wiring Lua into C++

Posted: Mon Mar 01, 2010 7:31 pm
by Falco Girgis
TheBuzzSaw wrote:Cool.

In Elysian Shadows, how often are you calling Lua? Are you still sticking to your paradigm of calling it once per frame (in addition to triggers and whatnot)? In my project, I seem to be headed down that path.
Yes. There's a tradeoff, and my commercial experience has only made me more confident in my decisions (Marcel and I are working for a starting Game Development company as C# programmers with Unity).

Unity also uses this design pattern.

Having an Update() function, that gives Lua the ability to poll every frame ensures that you can do ANYTHING via scripting (even if there is no appropriate trigger to associate with an action). This is all-powerful.

Having triggers keeps the majority of the polling out of Lua, so that the engine can be doing the checks every frame THEN call Lua. This is going to be a huge performance increase over the Update() polling for everything. Ideally, the engine should be able to handle as many kinds of triggers as possible. Update() is for when this isn't possible.

So you're kind of compromising power with speed here.

Re: wiring Lua into C++

Posted: Wed Mar 03, 2010 12:00 pm
by TheBuzzSaw
The issue with my circumstance is that Lua is there mostly to enable outsiders to contribute. Basically, it is a fast-paced 2D fighter. I have to walk this fine line of keeping the engine fast while giving enough power to designers to create fighters with extremely quirky subtleties.