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.
- extern "C" {
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- #pragma comment(lib, "lualib.lib")
- #pragma comment(lib, "lua.lib")
- }
Then, we generally use a global LUA library to define this item.
- lua_State* g_Lua;
In addition, LUA is also initialized during the initialization of your agency.
- G_Lua = lua_open (); ** add this sentence
-
- /* Load Lua base libraries */some online textbooks
- Lua_baselibopen (g_Lua)
- Lua_tablibopen (g_Lua );
- Lua_iolibopen (g_Lua );
- Lua_strlibopen (g_Lua );
- Lua_mathlibopen (g_Lua );
Then declare the interface function and register the function.
- 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.
- Static int Func (lua_State * L)
- {
- A static function must contain a lua_State structure pointer.
- The returned value indicates the number of returned data, for example, return 2.
- Returns two integers, floating-point numbers or something, just like writing LUA scripts.
- I, j = Func (), which indicates that two return values can be obtained from the Func interface function.
-
- Return 0;
- }
RunScriptThe statement can read the file or directly read the function name.
We registered
- Lua_register (g_Lua, "Message", myMessage );
-
- Static int myMessage (lua_State * L)
- {
- OutputDebugString ("OK ");
- Return 0;
- }
-
- Lua_dofile (g_Lua, strCurPath); // read the file. The complete file path name must be provided.
- Lua_dostring (g_Lua, "Message ()"); // read the function directly.
Only write in the file
- Message()
You can.
BecauseScriptIt can process some complex logic, and usually runs the script in the thread.
- Extern lua_State * g_Lua;
-
- HANDLE ScriptThreadID = NULL;
-
- UINT _ stdcall DoScript (void * lPrarm)
- {
- CString strCurPath;
- GetModuleFileName (AfxGetInstanceHandle (), strCurPath. GetBuffer (MAX_PATH), MAX_PATH );
- StrCurPath. ReleaseBuffer ();
- Int nFind = strCurPath. ReverseFind ('\\');
- StrCurPathstrCurPath = strCurPath. Left (nFind + 1 );
- StrCurPath + = (char *) lPrarm;
-
- Lua_dofile (g_Lua, strCurPath );
- Lua_dostring (g_Lua, "Message ()"); // directly use the function name for execution
-
- _ Endthreadex (0 );
- Return 0;
- }
-
- Int DoLuaScript (const char * filename)
- {// Execute the script through a file
- If (ScriptThreadID)
- TerminateThread (ScriptThreadID, 0 );
- ScriptThreadID = (HANDLE) _ beginthreadex (NULL, 0, DoScript, (PVOID) filename, 0, 0 );
- Return 0;
- }
-
- Static int myMessage (lua_State * L)
- {
- OutputDebugString ("OK ");
- Return 0;
- }
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!