Basic usage of parsing LUA script language

Source: Internet
Author: User

LUA scriptThe basic language usage is what we will introduce in this article, mainly to learnLua scriptThe usage of the language is detailed in this article. I just used it for one day. It is really simple, but it is very powerful, and it can make your program very flexible. If you want to learn it for one or two times, it will be very useful. Come together.LUAGo to the Internet to download the library. I will not upload it here.

Add the following statements to the header file in VC, which is generally added to StdAfx. h.

 
 
  1. extern "C" {   
  2. #include "lua.h"   
  3. #include "lualib.h"   
  4. #include "lauxlib.h"   
  5. #pragma comment(lib, "lualib.lib")   
  6. #pragma comment(lib, "lua.lib")   
  7. }  

Then, we generally use a global LUA library to define this item.

 
 
  1. lua_State* g_Lua;  

In addition, LUA is also initialized during the initialization of your agency.

 
 
  1. G_Lua = lua_open (); ** add this sentence
  2.  
  3. /* Load Lua base libraries */some online textbooks
  4. Lua_baselibopen (g_Lua)
  5. Lua_tablibopen (g_Lua );
  6. Lua_iolibopen (g_Lua );
  7. Lua_strlibopen (g_Lua );
  8. Lua_mathlibopen (g_Lua );

Then declare the interface function and register the function.

 
 
  1. lua_register(g_Lua, "Message",    myMessage);  

Okay. The Initialization is complete. Let's take a look at the method of interface functions.

The function must be written in this format.

 
 
  1. Static int Func (lua_State * L)
  2. {
  3. A static function must contain a lua_State structure pointer.
  4. The returned value indicates the number of returned data, for example, return 2.
  5. Returns two integers, floating-point numbers or something, just like writing LUA scripts.
  6. I, j = Func (), which indicates that two return values can be obtained from the Func interface function.
  7.  
  8. Return 0;
  9. }

RunScriptThe statement can read the file or directly read the function name.
 
We registered

 
 
  1. Lua_register (g_Lua, "Message", myMessage );
  2.  
  3. Static int myMessage (lua_State * L)
  4. {
  5. OutputDebugString ("OK ");
  6. Return 0;
  7. }
  8.  
  9. Lua_dofile (g_Lua, strCurPath); // read the file. The complete file path name must be provided.
  10. Lua_dostring (g_Lua, "Message ()"); // read the function directly.

Only write in the file

 
 
  1. Message()  

You can.

BecauseScriptIt can process some complex logic, and usually runs the script in the thread.

 
 
  1. Extern lua_State * g_Lua;
  2.  
  3. HANDLE ScriptThreadID = NULL;
  4.  
  5. UINT _ stdcall DoScript (void * lPrarm)
  6. {
  7. CString strCurPath;
  8. GetModuleFileName (AfxGetInstanceHandle (), strCurPath. GetBuffer (MAX_PATH), MAX_PATH );
  9. StrCurPath. ReleaseBuffer ();
  10. Int nFind = strCurPath. ReverseFind ('\\');
  11. StrCurPathstrCurPath = strCurPath. Left (nFind + 1 );
  12. StrCurPath + = (char *) lPrarm;
  13.  
  14. Lua_dofile (g_Lua, strCurPath );
  15. Lua_dostring (g_Lua, "Message ()"); // directly use the function name for execution
  16.  
  17. _ Endthreadex (0 );
  18. Return 0;
  19. }
  20.  
  21. Int DoLuaScript (const char * filename)
  22. {// Execute the script through a file
  23. If (ScriptThreadID)
  24. TerminateThread (ScriptThreadID, 0 );
  25. ScriptThreadID = (HANDLE) _ beginthreadex (NULL, 0, DoScript, (PVOID) filename, 0, 0 );
  26. Return 0;
  27. }
  28.  
  29. Static int myMessage (lua_State * L)
  30. {
  31. OutputDebugString ("OK ");
  32. Return 0;
  33. }

Haha, I have finished writing, I don't know literature, and I just want to write it around.

Summary: AnalysisLUA scriptThe basic usage of the language has been introduced. I hope this article will help you!

Related Article

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.