AboutLuaAutomaticBinding SystemThe question is what this article will introduce, becauseGamesContent is growing much faster than before, so programmers cannot completely control all code actions. They need otherGamesDeveloper help. Script Language inGamesHas been used for decades, but nowGamesHosts can better grasp their advantages to improve the player experience.
This essence highlightsLuaLanguageBind. This technology allows programmers to expose their C ++ classesLuaBut you do not need to understand thisSystem. The tool described here can be used not only in the C ++ language, but also in other languages. The core idea of this design is to drive the usability, efficiency, memory usage, and multithreading design goals.
1. Introduction
This article introducesBindYu XuLuaCreate, access, and use C ++ objects in the script. For example, a list of ENTITY-type strengths is saved in a singleton WORLD class, and the following script can be used to set the player's life value.
- local entity = WORLD:GetEntity("player")
- entity:SetHealth(50)
The binding used in this example is defined by the following declaration.
- //in.h and class definition
- SCRIPTABLE_DefineClass(WORLD)
- //in.cpp
- SCRIPTABLE_Class(WORLD)
- {
- SCRIPTABLE_ResultMethod1(GetEntity,ENTITY,std::string)
- }
It is so easy to bind a class. You don't need to take any other steps. It's that simple to allow programmers to expose C ++ classes and their own functions to Lua.
2. Features
The same design goal is as follows:
Low memory consumption;
Efficient binding;
Supports C ++ inheritance;
Easy to use;
Ensure thread usage security between scripts.
3. Function binding
Lua needs a special excuse to bind a function. The bound function must be the type defined in the following code. Lua binding is stack-based. lua_State contains all the parameters passed to that function. These parameters must be collected by using the stack index number lua_to. In this example, the first parameter accepted by this function is a string, the second parameter is a number, and the return value is a number. For more information about binding C functions, see the Lua manual [Ierusalimschy06. The binding of C functions is the only method that Lua binds to C/C ++ and is also the basis of this system.
- Int bingding_method (lua_State * state)
- {
- Const char * some_string;
- Double some_number, another_number;
- Some_string = lua_tostring (state, 1 );
- Some_number = lua_tonumber (state, 2 );
- // Here you can set the return value or do something you need to do.
- // Another number
- Lua_pushnumber (state, another_number );
- Return 1; // assume that we return 1
- }
4. Object-oriented in Lua
Lua is a widely used programming language with powerful functions. This article describes how to turn Lua into an object-oriented language. To help you make good use of this function, Lua's authors define a series of tools to help you with syntaxes (syntactic sugar ). The following is a list of the ones we use in the system. In this Code, the_object is an initial variable, which simulates the return of the this_call function.
- the_object:Test(5)==the_object["Test"]( the_object,5)
The object-oriented method can be implemented using this syntax. Objects are treated as relational arrays and indexed by function names. The returned results are the functions you need to call. There is a mechanism in Lua that allows any type of variables to interact with an array by using a metabase (this feature of Lua5.1. In Lua5.0, only tables and user data objects have metadata tables ). A meta-table is a table in Lua. It is assigned to an object, which contains special fields such as _ index and _ newindex ([Ierusalimschy06]). The function set in those special fields will be called as needed. When an object is accessed, _ index is called if it is an array access method. The following code illustrates how to set a table Meta to an object.
- Retriable = {}
- Retriable. _ index = function (table, key) return key end
- Setretriable (object, retriable)
- Test_return = object ["Test"] -- call the _ index function in the meta-table.
Lua's internal function types include numbers (double or float), string, table, nil, function (Lua or C), thread, and (light) user data. We use the last type to save objects in Lua. Light user data is slightly different from user data. The second type is completely a Lua object and can have a metadata table.
5. Bind a C ++ object to Lua
Binding requires several mechanisms: Re-Describe the C ++ object in Lua, save the binding function, and finally register the bound data for each C ++ object. In this essence, we will first introduce the overall technology to you. We will explain some special examples later.
Bind Data Structure
In the existing reality,BindIs saved directly inLuaAnd the bound data is stored in each script. However, if the system must support a large number of scripts, the bound data will be retained unnecessary. To avoid this, we decided to save the bound data in a class named SCRIPTABLE_BINDING_DATA in C ++. Each bound class is assigned an index value.
SCRIPTABLE_BINDING_DATA contains a record ID ing, Which is saved in CLassIndexTable. Each class has a ing to record each function name and the Corresponding binding function. MethodTable is the reorganization of this ing, which can be indexed based on the values in the ClassIndex Table. Because the delete operator has no name, its binding is stored in a separate array, which is called the Delete Table. Finally, Parent Table stores the fresh index of each class. If a class has no Parent class, the Parent Table entry is set to-1.
In the CD attached to this book, you can find a series of sub-Main functions that allow you to access these mappings. You can find them in the scriptable_bingding_data.h file.
- class SCRIPTABLE_BINDING_DATA
- {
- typedef int(* BINDING_FUNCTION) (lua_State *);
- std::map<std:string,int>
- ClassIndexTable;
- std::vector<std::map<std::string,BINDING_FUNCTION>*>
- MethodTable;
- std::vector<BINDING_FUNTION>
- DeleteTable;
- std::vector<int>;
- ParentTable;
- };
Point to thisBindThe Data Pointer and the Song class index are stored in lua_State. The data space is allocated by the LUAI_EXTRASPACE constant in luaconf. h.
Conclusion: ExploringLuaAutomaticBinding SystemAfter the problem is described, I hope that this article will help you!