* *
MBYC Hosting
 
Community
Features
Articles
Information
Modding
Media

RSS Feeds
Active Forum Topics

Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/supremec/public_html/includes/navigation-left.php on line 77

Warning: include(http://app.feeddigest.com/digest3/2POTMKDFDN.html) [function.include]: failed to open stream: no suitable wrapper could be found in /home/supremec/public_html/includes/navigation-left.php on line 77

Warning: include() [function.include]: Failed opening 'http://app.feeddigest.com/digest3/2POTMKDFDN.html' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/supremec/public_html/includes/navigation-left.php on line 77

 

RSS 2.0
Feed Digest
Get Firefox!
Inside Supreme Commander
GameReplays
Supreme Commander Alliance
MetalGrind
SupComFiles Levels4You
GameCaptain Supreme Commander
RTSC
supcomlive



 
* *
*
LUA Scripting Tutorial 2: Getting Started with Lua

By Tony and Paige at www.tonyandpaige.com

Lua is a light-weight scripting language designed to extend existing C and C++ programs. Unfortunately, there are very few online tutorials covering this language. I learned how it works mainly by reading the source, and studying the programs included with the distribution. Hopefully, this page will make learning Lua a little easier for those following in my footsteps.

First, you need to download Lua. You can get the source from the Lua download page. If you're using Windows and would prefer precompiled binaries, you can check out Philippe Lhoste's Lua Page or scroll to the bottom of this page.

Now we need to install Lua. On Linux you should be able to just extract the files then type "make" and "make install" as root. See the INSTALL file if you need more help. On Windows download the binaries below and unzip them to a folder like "C:\Program Files\Lua-5.0".

Everything should be ready to go on Linux, but on Windows we'll need to configure Visual C++ so it can find the Lua files.

  1. Open Visual C++ then go to Tools, Options.
  2. Click the directories tab.
  3. Choose "Include files" and add a new path "C:\Program Files\Lua-5.0".
  4. Now choose "Library files" and add the same path.

You're now ready to compile your first Lua enabled C++ program.

 

Your first program with Lua

This program is short and straight forward, but I'll run through what's happening just in case it's not clear.

  1. A pointer to our Lua interpreter is created by calling lua_open().
  2. Lua libraries are loaded with lua_baselibopen(). This provides simple functions like print.
  3. Our script is executed with lua_dofile(). This reads the file and runs the script.
  4. Finally, Lua is closed properly with lua_close().

Save this file as luatest.cpp. If you'd like to use straight C instead of C++, just name the file luatest.c and remove the extern "C".

#include <stdio.h>

extern "C" {
	#include "lua.h"
	#include "lualib.h"
	#include "lauxlib.h"
}

/* the Lua interpreter */
lua_State* L;

int main ( int argc, char *argv[] )
{
	/* initialize Lua */
	L = lua_open();

	/* load Lua base libraries */
	lua_baselibopen(L);

	/* run the script */
	lua_dofile(L, "test.lua");

	/* cleanup Lua */
	lua_close(L);

	return 0;
}
      

Here's a simple Lua script, just to make sure that it's working. Save this as test.lua

-- simple test

print "Hello, World!"

 

Compiling

On Linux you can compile this program by typing this command:

g++ luatest.cpp -llua -llualib -o luatest

Then run the program by typing:

./luatest

If everything worked correctly, the program should print Hello, World!

In Visual C++ you'll need to follow these steps:

  1. Create a new Win32 Console Application Project.
  2. Add the "luatest.cpp" file to your project.
  3. Go to Project, Settings and click the Link tab.
  4. Add lua+lib.lib to the list of Object/library modules.
  5. At this point, you should be able to press F7 to Build the program.

Before we can run the program, you'll need to put the "lua+lib.dll" file where Windows can find it. Copy the file from "C:\Program Files\Lua-5.0" to your new Visual C++ project's folder. If your program compiled without errors, you can now press Ctrl+F5 to execute the program.

Downloads
  • lua-5.0-win32.zip - Lua 5.0 Headers, Libs, and DLLs for Windows development.
  • luatest.zip - Tutorial Source and Project files for Visual C++ 6 on Windows.
  • luatest.tar.gz - Tutorial Source and Makefile for Linux.
Older Versions

Here are the files for Lua 4.0.

If you've gotten this far then you are well on your way to using Lua in your own projects. The next tutorial will cover calling Lua functions with arguments and returning values from Lua to your C++ program.

*
* *