This article was reproduced in: http://blog.csdn.net/liangliang103377/article/details/43269693simple solution for Ccluaobjcbridge-lua and OBJECTIVE-C interoperabilityAt the beginning of the month, I sent an article aboutLua-Java Interop articles, which mentions the Luajavabridge tool I created. Now, the latest Lua and OBJECTIVE-C interoperability tools are also coming out. Because it is specifically for Cocos2d-x, it is named Ccluaobjcbridge.
PS: The former Luajavabridge will also be renamed to Ccluajavabridge, and refer to the implementation of Ccluaobjcbridge now, made a lot of improvements, will be released after the completion.
The function of Ccluaobjcbridge (hereafter referred to as LUAOC) is to drop the "arbitrary Objective-c class method" directly from Lua. With this feature, it's a fortress to encapsulate a variety of iOS libraries, which is the artifact of Cocos2d-x Lua game development ^_^
The main characteristics of LUAOC
- You can call Objective-c Class Method from Lua
- Supports Int/float/boolean/string/lua Function/lua table six parameter types when calling the Objective-c method
- You can pass LUA function as an argument to objective-c and have objective-c save a reference to the LUA function
- You can call LUA's global functions from objective-c, or invoke the LUA function that references point to
The main features and Luaj are the same, but compared to the old version Luaj made some adjustments for objective-c.
LUAOC usage Examples
The following code shows how to invoke the "SDK":
Lua Code:
123456789-ten-19 |
--Register callback function to display a message when the player leaves the 91 platform interface--The ps:91 SDK's payment interface does not return the status directly to the client, and the payment needs to be verified on the serverLocalfunctionCallback(Event) IfEvent=="Sdkndcom_leave_platform "Then Print("The recharge is in progress and you will receive the coins later. ") EndEndLuaoc.Callstaticmethod("Sdkndcom ","Registerscripthandler ",{Listener=Callback})--Enter 91 of the payment interfaceLocalArgs={ OrderId="order-00001001001 ", Coins=1000,}Localokret = luaoc.< span class= "n" >callstaticmethod ( "sdkndcom" span class= "s2" > "payforcoins" args) if not ok then print (string.format "sdkndcom.payforcoins ()-Call API failure, error code:%s" tostring (ret end
|
The following code is the middle layer written by objective-c, used to communicate the SDK and Lua code.
Objective-c Code:
123456789Ten-20 All in all |
+(Nsdictionary*)Payforcoins:(Nsdictionary*)Dict{ NSString*OrderId=[DictObjectforkey:@ "OrderId"]; If(!OrderId||[OrderIdLength]==0) { CfuuidrefTheuuid=Cfuuidcreate(Null); CfstringrefString=Cfuuidcreatestring(Null,Theuuid); Cfrelease(Theuuid); OrderId=[(NSString*)StringAutorelease]; } IntCoins=0; If([DictObjectforkey:@ "Coins"]) { Coins=[[DictObjectforkey:@ "Coins"]Intvalue]; } NSString*Description=[DictObjectforkey:@ "description"]; If(!Description) { Description=@""; } IntRet=[[Ndcomplatformdefaultplatform ndunipayforcoin:orderIdneedpaycoins:coins Paydescription:description]; return [nsdictionary Dictionarywithobjectsandkeys:orderid@ "OrderId" , [nsnumber numberwithint:< span class= "n" >ret@ "error" nil ; } /span>
|
Since the parameter number and type of the Objective-c method cannot be obtained directly, if you want to receive Lua-passed parameters from the Objective-c method, it can only be in the form of nsdictionary.
In the code above, the Lua side passes a table with orderId and coins as a parameter. This table is automatically converted to the Nsdictionary object by LUAOC and passed into the Payforcoins: (nsdictionary*) method.
However, when you return a value from Objective-c to Lua, you can return values of various types. The currently supported value types are int/float/bool/nsstring/nsdictionary. Especially if you can return the Nsdictionary type, the Lua code gets the data from the objective-c side much easier.
Calling Lua from Objective-c
Since Ccluaobjcbridge is already integrated into the quick-cocos2d-x (a cocos2d-x-based Lua game engine), calling Lua from Objective-c is also easier and more flexible:
123456789 |
FunctionId is the reference ID for Lua function, refer to the description in the Luajavabridge article1. Put the LUA function corresponding to the reference ID into the LUA stackCcluaobjcbridge::Pushluafunctionbyid(FunctionId);2. Put the parameters that need to be passed to Lua function into the LUA stackCcluavaluedictItem;Item["Title"]=Ccluavalue::StringValue("Hello");Item["Coins"]=Ccluavalue::Intvalue(1000);Item["Success"]=Ccluavalue::Booleanvalue(TRUE);ccluaobjcbridge::getstack () ->pushccluavaluedict (item//3. Execute Lua function::getstack () -> executefunction (1 //4. Release references Idccluaobjcbridge ::releaseluafunctionbyid (callbackid
|
Ccluaobjcbridge::getstack () Returns a pointer to a Ccluastack object.
Ccluastack is a new object introduced by Quick-cocos2d-x, encapsulating some of the common operations of the Lua stack. For example, putting values into Lua stacks provides the following methods to support various data types:
123456789 |
voidPushint(IntIntvalue);voidPushfloat(FloatFloatvalue);voidPushboolean(boolBoolvalue);voidPushstring(ConstChar*StringValue);voidPushstring(ConstChar*StringValue,IntLength);voidPushnil(void);voidPushccobject(Ccobject*objectvalueconst char* typename); void pushccluavalue (const span class= "n" >ccluavalue& value); void pushccluavaluedict (const Ccluavaluedict& dict); void pushccluavaluearray (const Ccluavaluearray& array);
|
It is easier to use and transfer data than Luajavabridge,ccluaobjcbridge. Later Luajavabridge will also be renamed to Ccluajavabridge and use the same value-passing mechanism.
OC calls the LUA global method directly:
cocos2d::ccluaengine::defaultengine()-executeglobalfunction(" Luaglobalfunctiononpause ");
The communication mechanism between Cocos2dx-lua and object