* *
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 3: Calling Lua Functions
By Tony and Paige at www.tonyandpaige.com

This is the second in my series of tutorials covering the Lua scripting language. The first tutorial deals with setting up Lua on Windows and Linux and compiling your first "Hello, World!" program. If you haven't been through the first tutorial yet, please read it now.

This tutorial will show you how to define a funcion in a Lua script and call it from your C or C++ program. We will cover passing arguments, returning values, and deal with global variables.

Your first function in Lua

Defining functions in Lua is very simple. Start with the word "function", followed by the function name, and a list of arguments. Function definitions end with the word "end". Functions in Lua can accept multiple arguments and return multiple results.

Here's a simple Lua function that adds two numbers and returns their sum. Save this file as add.lua

-- add two numbers

function add ( x, y )
	return x + y
end
      

In the previous tutorial, the call to lua_dofile() would execute the script. Since the Lua script in this tutorial only defines a function, calling lua_dofile() simply makes the function available to our program.

As I said earlier, functions in Lua can accept multiple arguments and return multiple results. This is done using a stack.

To call a Lua funcion, we first push the function onto the stack. The function is followed by the arguments, in order. Then we call the function with lua_call(). After the function call, the return value is available on the stack. All of these steps are demonstrated in the luaadd() function below.

  1. The call to lua_getglobal() pushes the add() function onto the stack.
  2. The first argument, x, is pushed onto the stack with lua_pushnumber().
  3. The second argument, y, is pushed onto the stack with lua_pushnumer().
  4. Then the function is called with lua_call(). We specify two arguments and one return value.
  5. Now we retrieve the return value from the top of the stack with lua_tonumber().
  6. Finally, we remove the value from the stack with lua_pop().

Save this file as luaadd.cpp. If you'd like to use straight C instead of C++, just name the file luaadd.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 luaadd ( int x, int y )
{
	int sum;

	/* the function name */
	lua_getglobal(L, "add");

	/* the first argument */
	lua_pushnumber(L, x);

	/* the second argument */
	lua_pushnumber(L, y);

	/* call the function with 2
	   arguments, return 1 result */
	lua_call(L, 2, 1);

	/* get the result */
	sum = (int)lua_tonumber(L, -1);
	lua_pop(L, 1);

	return sum;
}

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

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

	/* load the script */
	lua_dofile(L, "add.lua");
	
	/* call the add function */
	sum = luaadd( 10, 15 );

	/* print the result */
	printf( "The sum is %d\n", sum );

	/* cleanup Lua */
	lua_close(L);

	return 0;
}
      

Compiling

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

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

Then run the program by typing:

./luaadd

If everything worked correctly, the program should print "The sum is 25"

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

  1. Create a new Win32 Console Application Project.
  2. Add the "luaadd.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.

Global Variables

Global variables are also easy to deal with in Lua. As we've seen, lua_getglobal() will push the value of a Lua global onto the stack. If our Lua script contained a global variable named z, for example, this piece of code would get it's value:

	lua_getglobal(L, "z");
	z = (int)lua_tonumber(L, 1);
	lua_pop(L, 1);

There is also a corresponding lua_setglobal() function that sets the value of a global variable. This snippet of code demonstrates setting the value of the global Lua variable z to 10:

	lua_pushnumber(L, 10);
	lua_setglobal(L, "z");

Note that it is not necessary to explicitly define global variables in your Lua script. Setting a value with lua_setglobal() will create the global for you if it doesn't already exist.

Downloads
  • luaadd.zip - Tutorial Source and Project files for Visual C++ 6 on Windows.
  • luaadd.tar.gz - Tutorial Source and Makefile for Linux.
Older Versions

Here are the files for Lua 4.0.

  • luaadd-4.zip - Tutorial Source and Project files for Visual C++ 6 on Windows.
  • luaadd-4.tar.gz - Tutorial Source and Makefile for Linux.

You should now be able to define functions in Lua and call them from your C++ programs. The next tutorial will cover calling C++ functions from Lua.

*
* *