wiring Lua into C++

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

wiring Lua into C++

Post 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?
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: wiring Lua into C++

Post 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 ;)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: wiring Lua into C++

Post 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).
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: wiring Lua into C++

Post 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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: wiring Lua into C++

Post 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? ;)
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: wiring Lua into C++

Post 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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: wiring Lua into C++

Post 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
User avatar
thbp
Chaos Rift Regular
Chaos Rift Regular
Posts: 132
Joined: Tue Dec 08, 2009 5:32 pm
Current Project: Learn
Favorite Gaming Platforms: PC/PS/GC/DC
Programming Language of Choice: C(++)/Perl
Location: wrestling matts
Contact:

Re: wiring Lua into C++

Post by thbp »

has any one posted this yet? (the lua book?) http://books.google.com/books?id=ZV5hXZ ... &q=&f=true
XNA ========== eXtreme Nuclear Atomic
Image
Image
Image
Wrestlers have three motion: side 2 side , circle, and stalk
User avatar
DeltaFolee
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Sun Jun 07, 2009 3:23 pm
Favorite Gaming Platforms: PC, GameCube, PS4
Programming Language of Choice: C++
Location: University Park, PA
Contact:

Re: wiring Lua into C++

Post 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.
If only I could be so grossly incandescent
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: wiring Lua into C++

Post 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?
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: wiring Lua into C++

Post 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.
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: wiring Lua into C++

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: wiring Lua into C++

Post 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.
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: wiring Lua into C++

Post 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.
Post Reply