Today, how to get Lua and link it with your game.
The previous installment of this tutorial is here:
- RPG Anvil: The Lua Tutorial Part 1 - https://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=503512&reply_id=3480420
Lua is available at http://www.lua.org/. As of this writing (June 25, 2009), the latest version is 5.1.4. There are various binary packages available, however, I recommend that you just download the source code (lua-5.1.4.tar.gz for me). Unpack this using your favorite unpacking utility; I put mine into C:\dev\lua-5.1.4.
Now, we'll make a project for this tutorial:
- Fire up Visual Studio. I'm using Visual Studio 2005; your steps will likely vary somewhat if you're using a different version.
- Pick a "Visual C++"/Win32/"Win32 Console Application". Call it luatest.
- In "Application Settings", be sure we're doing "Console Application".
- You can turn off ATL and MFC if you want to, though it shouldn't matter.
- If you build and run at this point, you should get a Console Application that opens a console window, and then exits.
Next, we need to add the Lua files. Open the project properties, and go to "Configuration Properties"/"C/C++"/General. Add the Lua directories to "Additional Include Directories"; something like:
One more annoying step in the project properties. Lua uses the C-library string function calls like strcpy, and Microsoft doesn't like those anymore. The easiest way to deal with this is in the project properties, under "Configuration Properties"/"C/C++"/Preprocessor. Under there, add to "Preprocessor Definitions":
The easiest way to include the Lua sources are just to add the files. You can fool around with building Lua as an additional library (or even DLL), but I wouldn't bother. To do this, add the Lua source files to your project's "Source Files". There is a list of the files in C:\dev\lua-5.1.4\etc\all.c; you want all of those files except for lua.c.
And, even though we're not going to call into Lua yet, let's include the header files just for kicks. To your luatest.cpp, just under:
At this point your luatest.cpp should look something like this:
// luatest.cpp : Defines the entry point for the console application.
int _tmain(int argc, _TCHAR* argv)
This should build cleanly. You can run your Lua-enabled app, which should fire up a console window, do nothing, and then exit. Congratulations!
Next: Calling a Lua script.