RPG Anvil: The Lua Tutorial Part 1

Published June 26, 2009
Advertisement


Forgive the grandiose title; this post should really be entitled "A Quick Tutorial on Integrating Lua as a Simple Scripting Language into a Windows C++ RPG Engine". But that's too long and boring, so I've gone with "The Lua Tutorial" even though there are many far better Lua Tutorials (http://lua-users.org/wiki/TutorialDirectory) available.

So, for those who haven't been following, I'm working on an RPG, which by necessity has a game engine behind it. The game engine is entitled SENG, and supports single-player party-based RPG play. The engine is Windows only, using DirectX and C++ as the foundations. Part of the philosophy of the SENG engine is that most of the story and level logic is data driven; that is, run from scripts within the level files. For instance, when your party has a conversation with someone, the conversation is all driven through scripts. My initial work on the SENG engine had a LISP-like scripting engine, except minus the parentheses. It was dirt-easy to create and support in the engine, but the power of the scripts was pretty limited, and the authoring of scripts was cumbersome and error prone.

I set out to look for some alternative, such as beefing up my scripting engine to be more LISP-like, or using XML syntax, but I kind of wanted to get out of the business of authoring scripting languages. Not to mention that having a custom scripting language would be a big pain if anyone but me ever wants to author scripts. After some investigation, I settled on Lua (http://www.lua.org/) as my scripting language.

From the Lua manual (http://www.lua.org/manual/5.1/manual.html):

Quote:Lua is an extension programming language designed to support general procedural programming with data description facilities. It also offers good support for object-oriented programming, functional programming, and data-driven programming. Lua is intended to be used as a powerful, light-weight scripting language for any program that needs one. Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++).


From my perspective, the winning features of Lua:

  • Small, easy to integrate.

  • C-like syntax.

  • Full featured, robust logic.


As I've done the integration, all of these features have been borne out.

During this work, I've looked through various documentation and tutorials, all of which have been pretty good. However, I was struck by the absence of a simple tutorial or document, "Here are the basic steps to integrate Lua with your C++ engine". So, that's the point of this tutorial. I'm not going to get into the details of writing Lua scripts at all, nor any advanced usage; see the official documentation http://www.lua.org/docs.html for that.

The feature I'm going to demonstrate in this tutorial is simple. The game has a script, stored in a string, that it wants to call to determine whether the player is allowed to unlock a door or not. The script, in turn, calls back into the game to see if the player's Lockpick Skill Level is greater than 7, and returns TRUE if it is, or FALSE otherwise.

Notes: How the script has been loaded into the string is completely irrelevant; in my case they are included in XML files but that doesn't matter for this tutorial. Obviously the example script is very simple (comparing a number to 7), but once you've gone through the tutorial, it should be clear how to incorporate more complex logic, like also allowing the player to unlock the door if he has a key, or has completed some quest. Further, it should also be apparent that the script can be called from the game engine for other actions beyond trying to open a door; my engine does for conversation, object activation, and quest logic.

Next: How to get Lua and link it with your game.
0 likes 2 comments

Comments

sprite_hound
Hmm. Interesting. I'm curious to see how you go about the integration... specifically how much / which parts of your engine code you expose to lua.

Are you using luabind or some other library? or just plain lua?
June 26, 2009 10:58 PM
gdunbar
Quote:Original post by sprite_hound
Hmm. Interesting. I'm curious to see how you go about the integration... specifically how much / which parts of your engine code you expose to lua.


I'm not really going to get that far in the tutorial, but I'm happy to discuss. I'll make a post at the end describing how I'm using it, OK?

Quote:Are you using luabind or some other library? or just plain lua?


Plain Lua... as an incoming Lua novice, I found the C++ binding libraries confusing enough that I didn't want to use them; I didn't know enough to use them effectively (or maybe even correctly). Now that I know (more) what I'm doing, I find my needs are simple enough that I don't really need them.


June 27, 2009 10:32 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement