Lua scripting for C ++ objects

Source: Internet
Author: User

The biggest advantage of scripted programming is simplicity and flexibility. In addition, it is hot update, which is widely used in online games. in online games, engines (c/C ++) are usually used) + in the lua/python architecture, the SDK-type code is placed in the engine. These codes are usually stable and rarely modified after the game is launched, the real game logic is created at the script layer. There are two advantages: 1. The bug at the script layer will basically not cause the crash of the program, because it is a sandbox. 2. For running code, you can easily use hot updates to fix bugs. C interacts with lua. If you want to write logic code at the script layer, the most important thing is to expose the objects, functions, and global variables in the engine layer C ++ to the script layer for access, of course, sometimes the C ++ layer must be able to easily access the global variables of the script layer. Recently, I have carefully explored the interaction knowledge between C ++ and Lua. I will give a summary and share it with the people who need it. In C, you can write code to access and call lua (functions, variables...). You can also write code in C so that the lua can access c. In this case, c is to start the program, then call the variables in lua, and then access the variables in the called lua. In either case, the code for interaction between C and lua is written in c, which is called the c api of lua. We need to use c api to do three things: www.2cto. com1. execute a LUA Script 2. obtain the global variable of a LUA. 2. register a C variable or function to a specified specification so that the registered C variables and functions can be called in LUA. If three tasks can be completed, all these approaches can be completed. Draw out common functions and interactive APIs of c api in a graphical way: C and lua pass through a virtual stack (globally there is also a local function) to achieve mutual access. 1. execute a LUA Script 2. obtain the global variable of a LUA. 2. register a C variable or function to a specified specification so that the registered C variables and functions can be called in LUA. The related implementation methods can be found in these three points, but these methods are all written for C. For the C ++ engine, we usually want to implement object-oriented access at the script layer. In general, for example, the C ++ layer has a class C, and C has an excuse to Get (), we hope that at the script layer we can easily write code such as c = newClassC () and c: Get (). This is the LUa script of the C ++ object discussed in this article, this is very important in the scripting programming of the C ++ engine. The Lua scripting of the C ++ object assumes that there is a class MyCClass. I implemented some of its methods in the C ++ layer, such as SetI (int) and GetI, I want to script this class to the Lua layer for object-oriented access. Below are some of my brief implementations -- New: defines the function int MyCClass: NewMyCClass (lua_State * L) in C ++. This function can use c = NewMyCClass () in lua () the actual implementation of NewMyCClass is: int MyCClass: NewMyCClass (lua_State * L) {// create a usrdata instance and upload it to size_t bytes = sizeof (MyCClass) in the stack ); myCClass * c = (MyCClass *) lua_newuserdata (L, bytes); // create a metadata table for usrdata to implement the object-oriented method. Here we add a metadata table to the object c, in the original table, the index GetI is assigned to the MyCClass: GetI function int r = lual_newretriable (L, "MyCClassMeta"); lua_push Value (L,-1); // copy -- index table lua_pushcfunction (L, MyCClass: GetI); lua_setfield (L,-2, "GetI "); // reg func lua_setfield (L,-2, "_ index"); // set _ index table lua_setretriable (L,-2); //-2? Return 1;} Of course, at the beginning of the program, register this NewMyCClasslua_register (L, "NewMyCClass", MyCClass: NewMyCClass); -- the member function GetI: Can Be In The lua layer c: GetI () int MyCClass: GetI (lua_State * L) {MyCClass * c = (MyCClass *) lua_touserdata (L, 1); int r = c-> GetI () lua_pushinteger (L, r); return 1 ;}-- the member function SetI can be in the lua layer c: SetI (100) int MyCClass: SetI (lua_State * L) {MyCClass * c = (MyCClass *) (lua_touserdata (L, 1); int p1 = luaL_checkint (L, 2); int r = c-> SetI (p1) re Turn 0;} This is a very simple implementation of accessing the C ++ object at the lua level. Optimization 1. the complete version may include MyCClass: Del In the meta-table assignment to delete c: Del (). However, all the destructor of Some architectures may be completed at the engine layer, it is not exposed to the script layer (or even the new operation). 2. the above code writes these functions for each class written in c ++, for example, for MyCClass: SetI (int I) you need to write a static int MyCClass: SetI (lua_State * L) for lua to call, because the call function for lua is always of this type and must be global. In the construction of the actual framework, we usually use the macro function method, such as defining a macro REG_CLASS_FUNC (class, class_func) to automatically generate this lua called function according to certain rules. You can also define a macro function REG_CLASS to automatically register the lua_register (L, "NewMyCClass", MyCClass: NewMyCClass) of a class to lua.

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.