Lua language in the game development world

Source: Internet
Author: User

In reality, game development often faces two conflicting pressures: Testing and verifying new ideas, and fast development and delivery.Integrating the scripting language into a game project can improve the development efficiency of the team and expand the native compilation language capability. Lua has performed well in many basic fields of game development.

In the game development team, many members may use Lua to complete their work. Programmers are responsible for integrating Lua into the game development environment. Generally, they need to write some Lua code. Game designers are the main users of scripting languages because they directly deal with upper-layer game design and data. Artists often use Lua to perform work such as interface layout, design, and placement of various models in 3D scenarios.

Lua is a very powerful tool that can be used to complete the following tasks:

  • Edit the game user interface
  • Define, store, and manage basic game data
  • Manage real-time game events
  • Create and maintain a developer-friendly game storage and loading system
  • Compile the AI system of the game
  • Create a functional prototype and port it in a high-performance language.

1 game interface

The game interface is a medium for players to interact with your game. The game interface is the most basic part of a game because it is responsible for all interactions with players. Because of its importance, it needs to be able to run with high quality and high performance, and be tested and continuously improved to maximize the user experience.

Lua allows the UI Designer to quickly create all the main interface elements-for interface layout, user input management, and game data output.Using some of the core interface controls and control functions developed by game programmers, the interface designer can not only be responsible for the Visual View of the interface, but also control the interaction of the game. This not only saves programmers time, but also gives art designers more creative space and saves a lot of time for testing the interface design. Code List 3.2 shows how to create a text GUI Control Using Lua.

Code List 3.2 use Lua to create a text GUI Control

2. Manage game data

Managing game data has always been a challenge for game developers. Game data defines the parameters and features of all objects in the game world, such as the pulse gun upgrade fee and the driving speed of the air cushion. For data-intensive games, developers often use spreadsheet tools to input and store data, and then create parsing tools to convert it into formats available in the game. This method is generally used in data-intensive games, such as role-playing games (information about NPCs or non-gamers is stored in table structures) or a strategy game (the role unit information is stored in the data table ).

Lua makes the storage system simpler. It uses Lua files as the storage medium to allow the program to use the same data. By creating a simple data management system, variables and types can be defined in Lua and can be easily read.Because you don't need to care about the entire data processing process, game designers can modify, increase, and reduce game data as needed without the assistance of programmers. Because of the features of Lua syntax and the ability to add comments to scripts, data files are easy to read. If you need a conversion tool, it is also very easy to develop. Output Lua data to the Lua file and load it at runtime.

Lua itself does not have the ability to directly access external databases, but it can use C ++ to develop Database Access Components and then use the luaglue function to integrate the components.

In code list 3.3, we can see how to use the Lua sample file to directly store game data. In this example, you can use the luaglue function to write data to the data structure managed by the core code. For the output of code list 3.3, see Figure 3.1.

Code List 3.3 Use Lua to save data from the US presidential election simulation game (frontrunner)

 

 

Figure 3.1 real-time display of Lua data in the game frontrunner

3. Event Processing

Generally, most of the important processing tasks in a game are event-driven, either from the game role or from the interaction agents in the game. These events can be simple, for example, if the user presses the <W> key, it can also be complicated. For example, two game entities come to a certain place at the same time.

Event-driven programming is no stranger to users who are familiar with Windows Development (events are the basis of Windows GUI operations.In C ++'s well-developed Event System, Lua is used to receive and handle these events, you can create a clear feedback process between the game's internal operating mechanism, the advanced Lua function, and user input.A simple example is to obtain the keyboard input, send the event to the Lua event processor, and display the input value. This concept will continue throughout this book-events will return the event type ID and the Object ID that drives the event, refer to code list 3.4.

Code List 3.4 example of getting a key event

4. Save and read the game status

Saving and reading player data is one of the most challenging tasks in game development projects. Because players sometimes need to leave the game temporarily, a method is required to save the game progress. Players also need to save the game status before some new attempts and challenges, so that they can restore the game progress if the attempt fails. Similarly, during development and testing, developers often need to read the specific game status to verify the game function or to confirm whether the bug has been fixed.

If you use Lua to save your core game data, you can use Lua as the system to save and read the current game status. In Lua, the game Progress File is a simple executable Lua code text file. Programmers or designers can use progress files to get the current game status (or modify it). Reading the game progress is as simple as executing the Lua script.Writing a function using Lua's standard input/output function to save game data to an executable Lua script is the most direct method. The advantage of this system is that designers can edit and modify this function based on the game data growth and deletion during the development process (no special function reading is required ).At the final stage of development, you can also use the script compilation function to encrypt game data.

5. Artificial Intelligence

Artificial intelligence (AI) is critical in today's games-players need savvy and challenging opponents and feel like real people. Game developers understand that real-world AI does not completely imitate how people play and respond, but creates this feeling for players. Over the years, developers have been arguing about the advantages and disadvantages of computer rivals "cheating" (computer opponents can access more game data than players. This debate is best spent elsewhere. However, almost all developers agree that player perception of AI behavior is more important than AI simulation.

As far as AI determination is developed, Lua is a very efficient tool. There are many AI components, such as path search, which should be left to the underlying language for implementation. Path searching (path searching for computer-controlled objects in the virtual world) is a big task of data computing. Computers need to repeatedly test possible paths to find the shortest or most direct path. (It is best to integrate path search into the upper-layer luaglue function to control related parameters, but we will introduce a Lua implementation in the following sections .) Another example is the movement determination implemented using the maximum and minimum values method. It is generally used in computer chess games to predict the movement of the next few steps and try to calculate the optimal movement step. Generally, "thinking functions" require a large amount of mathematical calculations, such as navigation tree or error method operations, which are best left to the underlying code. Artificial intelligence that relies on limited datasets and parameters is more suitable for Lua.The advantage of Lua is that designers can write simple models for trial and error, and quickly verify and iterate ideas without having to bother programmers. To use Lua to efficiently implement AI, You need to carefully design functions (c Functions) so that Lua scripts can access and interact with game data. Using Lua as an event management system also enables AI designers to respond to changes in game development and develop ai systems with flexible responses.

In code list 3.5, you can see that Lua is used to evaluate the possible travel destinations of the campaign. The evaluation is based on the number of electoral votes in the State and the State's support rate.

Code List 3.5 evaluate the Lua artificial intelligence function of the State to which the virtual U. S. Presidential candidate should go

6. quickly build a prototype

Commercial games must provide players with a high-performance experience. Frame drop and processing latency are not allowed in today's highly competitive market. Profiling is an effective method for determining performance bottlenecks, but it must be performed only when all functions are developed and correctly performed. Generally, prototype development and performance improvement cannot be performed at the same time.

Lua is a good tool for building portable core game function prototypes. Because Lua can integrate components developed in the native language, porting a separate Lua function to C ++ does not need to be changed for other Lua functions.It allows designers to use underlying languages to implement functions that require high performance when building prototypes. Because the algorithm structure of the program already exists, both the C function and luaglue call can be highly-performing and seamlessly integrated into the project.

7. Summary

In the game development field, Lua and C ++ are powerful combinations of functions. For game development tasks such as GUI development, event management, data storage and acquisition, game progress storage and loading, and AI, you must be able to quickly build prototypes, tests, and iterative designs, these are exactly what Lua is good. Frequent changes in core underlying language development projects will lead to system instability and prevent developers from focusing on more important development tasks. The Lua environment allows designers and programmers to design, test, and implement game functions quickly and securely using scripts.

-----------------------------------------------

This article is excerpted from Lua Game Development Practice Guide. Its original title is:Game Development with LuaBy Paul schuytema & Mark manyen, Translator: Tian Jian.

This book is the result of more than 10 years of work experience and wisdom by senior Lua game development engineers. The founder of Lua language personally recommends this book. It is one of the most practical and representative works in the Lua game development field. It not only describes in detail the various technical details, methods, skills, and best practices of using Lua in game development, it also explains how to use Lua as the main tool to convert game design into code implementation. In addition, it focuses on the core elements of the Lua language. The most important thing is that this book contains a large number of well-designed cases and provides complete source code, which is highly operable.

Watercress collection: http://book.douban.com/subject/20392269/

PDF example download: http://vdisk.weibo.com/s/ix8ok

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.