This site's article is originally written by Li huaming himi. The reposted article must be clearly stated: (Author: Sina Weibo: @ Li huaming himi)
Reproduced from [Black Rice gamedev block] original link: http://www.himigame.com/lua1/1343.html
☞Click to subscribe☜The latest developments in this blog! Notify you of the latest blog in time!
When using Cocos2d-x, it is inevitable that C/C ++ calls the Lua function, data or Lua calls the C/C ++ function, this article describes data and function interaction between C/C ++ and Lua in detail.
First, let's take a look at several Lua API functions:
Int lual_dofile (lua_state * l, const char * filename ):
Load and run the specified file. No error is returned. 0 is returned.
Void lua_settop (lua_state * l, int index ):
The parameter allows the input of any acceptable index and 0. It sets the top of the stack as the index. If the new stack is larger than the original one, the new elements in the excess part will be filled with nil. If the index is 0, all elements on the stack are removed.
Void lua_getglobal (lua_state * l, const char * Name ):
Press the value in the global variable name into the stack.
Void lua_pop (lua_state * l, int N ):
Pop-up from StacknElements. It is equivalent to clearing!
Void lua_pushstring (lua_state * l, const char * s ):
Press the zero-ending string pointed to by the pointer s to the stack. Lua performs a memory copy (or reuse a copy) on this string. Therefore, after the function is returned in S, it can be released or reused for other purposes. The string cannot contain zero characters. The first zero character is considered as the end of the string.
For more api reference: http://www.codingnow.com/2000/download/lua_manual.html
I have learned about the above functions. To facilitate the use of shoes, himi directly posts the encapsulated class hclcdata, which mainly includes the following functions:
1. C/C ++ calls the Lua global variable
2. C/C ++ calls a Lua global table element.
3. C/C ++ calls Lua global table
4. C/C ++ calls the Lua Function
5. Lua calls the C/C ++ Function
The following code is directly pasted: hclcdata. h
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |