Applying Lua in your game: USING Lua in CPP (basic data type, pointer and reference)

xiaoxiao2021-03-06  41

Using Lua in CPP (Basic Data Type, Pointer and Reference)

The previous introduction is a built-in basic data type in the CPP, however, even if this is the case, the situation will become complicated.

Using the macro lua_register_directclosure, which we have completed before, can only register the function of the parameter in the form of the Value. When there is a pointer and reference (once again, it is currently only for basic data types):

1. If it is a pointer, the intent usually implemented is to pass a result with this pointer. 2, if it is a reference, the same. 3, if it is a const pointer, usually only use const in face char *, the intent of the implementation function is that the contents of this parameter will not change. Other situations generally avoid using a Const pointer. 4. If it is a const reference, this is generally avoided for basic data types.

Each of Lua and CPP allows a function to return multiple values ​​in some way. For CPP, multiple return values ​​are returned by the first and second cases described above, and multiple return values ​​can be directly returned for Lua. :

- in luafunction swap (x, y) TMP = xx = yy = TMP

Return X, Yend

X = 100Y = 200

X, Y = swap (x, y)

Print (x..y)

Program output: 200100

Similarly, in the host program, we can also return multiple values ​​to lua:

INT SWAP (Lua_State * L) {// acquisition two parameters int x = GET (TypeWrapper (), L, -1); int y = GET (TypeWrapper (), L, -2);

// Exchange value int TMP = x; x = y; y = tmp; // Return value Push (L, X) to Lua; Push (L, Y);

/ / Tell Lua how many of the values ​​we returned Return 2;

Now we can call this function in Lua:

X = 100Y = 200

X, Y = swap (x, y)

In our register_proxy function, you can only valid for the basic data type by value, depending on our above analysis, if we can know in the compile period, for a template parameter T: 1, this is a basic data type, or one User-defined data type? 2. Is this a normal pointer or an item? 3, is this a reference? 4. Is this a constant pointer? 5, is this a const reference?

If we can know these, then according to our above analysis, we hope: (only for basic data types) 1. If this is a pointer, we want to return the content referred to by the pointer to Lua. 2, if this is a reference, we want to return the referenced finger to Lua. 3. If this is a const pointer, we want to pass the parameters obtained from the Lua stack to the call function. 4. If this is a const reference, we also want to pass the parameters from the Lua Stack to the call function.

转载请注明原文地址:https://www.9cbs.com/read-68737.html

New Post(0)