[COCOS2DX-LUA Script Development 11] C/C ++ and Lua between data function interaction and solve the "panic: unprotected error in call to Lua API (attempt T)

Source: Internet
Author: User

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

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.