How to use Lua Extended CC ++ Application Series 2

xiaoxiao2021-03-05  55

Author: buxiu Transfer: Lua Chinese website (www.luachina.net)

This article, "Hello, World!", Describes how to define a function in Lua and then call him in C / C , which will involve parameter delivery, return value, global variable, etc. The definition function in Lua is very simple, starting with the keyword function, follow the function name, parameter list, function body, and function definition at the end of END. LUA functions can pass multiple parameters or multiple returns. There is a simple Lua function below, and the two numbers are added to both. - Add Two Numbers

Code:

Function add (x, y)

Return X Y

end

In the previous one, we told the call lua_dofile () will load and run the Lua code. Our lua scripts in this section only define a function, call the Lua_DOFile () function, make our C / C program can access Lua Function.

The function in Lua can accept multiple parameters or multiple return values. This is done by stack. When you call a function, first place the function on the top, then place the parameters of the function in order, when we call the function lua_call (), the return value of the function has been placed on the top, these steps in LuaAdd () It can be seen in the function.

Description:

1. Call lua_getglobal () Press the add () function into the top of the stack.

2. The first parameter X is in the stack by calling Lua_PushNumber ().

3. Call Lua_PushNumber again () in the stack.

4. Use lua_call () to call the Lua function.

5. Call Lua_tonumber () get the return value of the function from the top of the stack.

6. Lua_POP () removes the value of the top of the stack.

Save the following code as LuaAdd.cpp, if you like to use C instead of C , you need to save the file as Luatest.c and remove extern.

Code:

#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 ();

/ * 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;

}

Compile:

Linux platform, type command:

Code:

G LuaAdd.cpp-lua-lulualib -o LuaAdd

Run the program:

Code:

./luaadd

If the normal call should be displayed: "The Sum IS 25"

If you are not a Linux operating system, use the VC compiler, you need:

1. Create a new Win32 console application project.

2. Add file LuaAdd.cpp to your project.

3. Go to Project, settings Click the Link page.

4. Add Lua lib.lib to the Object / Library Modules list.

5. Press F7 compiler.

Before running the program, you need to make sure that the Lua Lib.dll file is placed in Windows, copy this file from C: / Program Files / Lua-5.0 to the Visual C Project directory, if there is no error, now Ctrl F5 runs the program.

About global variables:

As we can see above, Lua_GETGLOBAL () places a global variable of Lua to the top, if our script contains a global variable z, the following code will get the value of z:

Code:

Lua_GETGLOBAL (L, "Z");

z = (int) Lua_tonumber (L, 1);

Lua_POP (L, 1);

The corresponding Lua_SETGLOBAL () is used to set the value of a global variable of Lua. The following code sets the value of the global variable z to 10.

Code:

Lua_Pushnumber (L, 10);

Lua_SETGLOBAL (L, "Z");

Note that you don't need to explicit global variables in your lua script. If the global variable does not exist, Lua_SETGLOBAL () will create a new global variable.

转载请注明原文地址:https://www.9cbs.com/read-32742.html

New Post(0)