About LUA application pen operations

Source: Internet
Author: User

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:

 
 
  1. Lua_State * L = lua_open (); // creates a LUA state machine.
  2. Luaopen_base (L); // start it
  3. Const char * buf = "print ('hello, world! ')";
  4. Lua_dostring (buf); // write the buf to lua and execute
  5. Lua_close (L); // close L
  6. Lua_pushstring (L, "var"); // puts the variable name to the stack]
  7. Lua_getglobal (L, "var"); // The value of the variable is now at the top of the stack.
  8. Int var = lua_tonumber (L,-1); // gets the element at the top of the stack.
  9. Lua_tostring (ls,-1); // gets the element at the top of the stack, which is generally used for parameter transmission.
  10. Lua_pushstring (ls, s_szPlayer); // pushes a string element into the stack and can be used for parameter output.
  11. Lua_pushnumber (L, 200); // press a digital element into the stack,
  12. Lua_register (L, "foo", foo );
  13. // 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:

 
 
  1. Lua_pushnumber (L, 100); // press the parameter to stack
  2. 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
  3. Int result = lua_tonumber (L,-1 );
 

Example:

 
 
  1. # Include "lua. h"
  2. # Include "lauxlib. h"
  3. # Include "lualib. h" int foo (lua_State * L ){
  4. // Obtain the parameter pushed into the stack when the script executes this function.
  5. // Assume that this function provides a parameter with two return values.
  6. // Get the first parameter const char * par = lua_tostring (L,-1 );
  7. Printf ("% s \ n", par); // push the first result lua_pushnumber (L, 100 );
  8. // Push the second result lua_pushnumber (L, 200 );
  9. // Return 2 result return 2;
  10. }
  11. Int main (int argc, char * argv []) {
  12. Lua_State * L = lua_open ();
  13. Luaopen_base (L );
  14. Luaopen_io (L );
  15. Lua_register (L, "foo", foo );
  16. Const char * buf = "r1, r2 = foo (" hello ") print (r1.. r2 )";
  17. Lua_dostring (L, buf );
  18. Lua_close (L );
  19. Return 0;
  20. }

Summary: AboutLUA ApplicationThe operations in this document are complete. I hope you can learn the LUA application content to help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.