AboutLUA ApplicationThis article describes the content of this article, mainly for understanding and learning.LUA Application. The interface in the game is written in LUA, which is exactly the same as WOW. Well, I will study it later. At the end of the year, save it first.
Lua language rules:
- Lua_State * L = lua_open (); // creates a LUA state machine.
- Luaopen_base (L); // start it
- Const char * buf = "print ('hello, world! ')";
- Lua_dostring (buf); // write the buf to lua and execute
- Lua_close (L); // close L
- Lua_pushstring (L, "var"); // puts the variable name to the stack]
- Lua_getglobal (L, "var"); // The value of the variable is now at the top of the stack.
- Int var = lua_tonumber (L,-1); // gets the element at the top of the stack.
- Lua_tostring (ls,-1); // gets the element at the top of the stack, which is generally used for parameter transmission.
- Lua_pushstring (ls, s_szPlayer); // pushes a string element into the stack and can be used for parameter output.
- Lua_pushnumber (L, 200); // press a digital element into the stack,
- Lua_register (L, "foo", foo );
- // Register the function foo written in C ++ with lua, so that the function can be called in the lua script.
In Lua, the function is equivalent to a variable, so you can obtain this function as follows:
Lua_getglobal (L, "main"); // The function is currently at the top of the stack.
Now, we can call this function and pass it to the correct parameters:
- Lua_pushnumber (L, 100); // press the parameter to stack
- Lua_pcall (L, 1, 1, 0); // call the function, there is a parameter, a return value // the return value is now at the top of the stack
- Int result = lua_tonumber (L,-1 );
Example:
- # Include "lua. h"
- # Include "lauxlib. h"
- # Include "lualib. h" int foo (lua_State * L ){
- // Obtain the parameter pushed into the stack when the script executes this function.
- // Assume that this function provides a parameter with two return values.
- // Get the first parameter const char * par = lua_tostring (L,-1 );
- Printf ("% s \ n", par); // push the first result lua_pushnumber (L, 100 );
- // Push the second result lua_pushnumber (L, 200 );
- // Return 2 result return 2;
- }
- Int main (int argc, char * argv []) {
- Lua_State * L = lua_open ();
- Luaopen_base (L );
- Luaopen_io (L );
- Lua_register (L, "foo", foo );
- Const char * buf = "r1, r2 = foo (" hello ") print (r1.. r2 )";
- Lua_dostring (L, buf );
- Lua_close (L );
- Return 0;
- }
Summary: AboutLUA ApplicationThe operations in this document are complete. I hope you can learn the LUA application content to help you!