Lua game Script Development initialization Lua

Source: Internet
Author: User

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:

 
 
  1. Extern "C"
  2. {
  3. Include "lua. h"
  4. Include "lauxlib. h" // This header file must be included; otherwise, the lua_open () function cannot be called.
  5. }

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:

 
 
  1. Int _ tmain (int argc, _ TCHAR * argv [])
  2. {
  3. // Initialize a Lua state and set the stack size to 1024
  4. Lua_State * pLuaState = lua_open (1024 );
  5. Return 0;
  6. }

After using Lua, call the following code to release the Lua object:

 
 
  1. lua_close(pLuaState); 

The complete procedure is as follows:

 
 
  1. Int _ tmain (int argc, _ TCHAR * argv [])
  2. {
  3. // Initialize a Lua state and set the stack size to 1024
  4. Lua_State * pLuaState = lua_open (1024 );
  5. // All Program Logic code is written here
  6. // Release the Lua state
  7. Lua_close (pLuaState );
  8. Return 0;
  9. }

Summary:Lua game scriptDevelopment InitializationLuaI 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.