Lua game scriptDevelopment InitializationLuaIs the content to be introduced in this article, mainly to learnLUAUse andLUAFor more information about the development of games, see this article.
I recently learnedGame scriptProgramming. The first scripting language exposed is Lua. The Lua version I use is 5.1.
Unfortunately, the version used in the book I used to learn Script Programming is the old version of Lua. As Lua has changed many function calls in the new version, I found it difficult to compile the program successfully according to the introduction in the book. After research, we can use the basic functions provided by Lua. So I will write out my solution and share it with you. I hope it will be helpful to other friends who have trouble using the new Lua version.
After you download and install Lua, you can use it. I use the C ++ compiler of Visual Studio. NET 2003 to develop the Host application that uses the Lua script. First, we need to set the compiler so that it can find the header files and library files required to use Lua. The method is as follows:
1. Start Visual Studio. NET 2003.
2. Select "Tools"> "options" to open the "options" dialog box.
3. Select "project" in the options on the right, and then select "VC ++ directory" under it ".
4. Select "include files" in the "directory with the following content" drop-down box on the right of the dialog box ". Then addLuaIn my computer, the directory is "C:/Program Files/Lua/5.1/Include ".
5. Select "library files" in the "directory with the following content" drop-down box on the right of the dialog box ". Then addLuaThe directory where the library Files are used. My path is "C:/Program Files/Lua/5.1/Lib ".
6. Click "OK" to close the dialog box.
Now, the compiler has been set. We will use a Console program to demonstrateLuaInitialization:
1. Create a C ++ console project.
2. Select "project"> "properties", open the Properties dialog box of the project, and select the "command line" option under "connector, enter the library files lua51.lib and lua5.1.lib in the lower right corner. Click "OK ".
3. To useLuaFunction, we need to include the Lua header file-lua. h and lauxlib. h In the program's source code file. Because Lua is written in pure C code, we need to include the two header files to our C ++ program in the following way:
- Extern "C"
- {
- Include "lua. h"
- Include "lauxlib. h" // This header file must be included; otherwise, the lua_open () function cannot be called.
- }
To use Lua, initialize it first. Initializing a Lua is actually a stack used by Lua. The main program and game script communicate with each other through this stack. You only need to call the following code in the Main () function of the Main program to initialize a Lua Stack:
- Int _ tmain (int argc, _ TCHAR * argv [])
- {
- // Initialize a Lua state and set the stack size to 1024
- Lua_State * pLuaState = lua_open (1024 );
- Return 0;
- }
After using Lua, call the following code to release the Lua object:
- lua_close(pLuaState);
The complete procedure is as follows:
- Int _ tmain (int argc, _ TCHAR * argv [])
- {
- // Initialize a Lua state and set the stack size to 1024
- Lua_State * pLuaState = lua_open (1024 );
- // All Program Logic code is written here
- // Release the Lua state
- Lua_close (pLuaState );
- Return 0;
- }
Summary:Lua game scriptDevelopment InitializationLuaI hope this article will help you!