LUA calls the Java procedure

Source: Internet
Author: User

In the COCOS2DX framework, there are inherited good luaj files to facilitate us to use LUA call Java code, note: Luaj can only be used under the Android platform, if used under the platform, there will be errors,

Therefore, before using the platform to judge, the method is as follows:

Local Luaj = require ("Src/cocos/cocos2d/luaj")--Introduction of Luaj Library

Luaj.callstaticmethod ("org/cocos2dx/lua/class file Name", method name, {parameter}, signature)

For example:

The Text.java file code in Java is as follows:

void Showtext ()

{

....

}

Lua calling code

Luaj.callstaticmethod ("Org/cocos2dx/lua/test", "Showtext", {}, "() V") pass parameters need to be stored through table, no parameter can write an empty table, The last parameter signature is to determine the type of the parameter to be passed and the return data type.

The "()" bracket in the signature is filled with the parameter type, the signature of the parameter type can be opened Luaj.lua file view, the argument after the parentheses is the return type. "V" means no return type,

If the parameter is Boolean, returns a Boolean, the signature can be written as "(Z) z",

Post the Luaj.lua code as follows

Local Luaj = {}

Local Calljavastaticmethod = Luajavabridge.callstaticmethod

Local function checkarguments (args, SIG)

If Type (args) ~= "table" then args = {} end

If Sig then return args, Sig end

sig = {"("}

For I, V in Ipairs (args) do

Local T = Type (v)

If T = = "number" Then

sig[#sig + 1] = "F"

ElseIf T = = "Boolean" Then

sig[#sig + 1] = "Z"

ElseIf T = = "function" Then

sig[#sig + 1] = "I"

Else

sig[#sig + 1] = "ljava/lang/string;"

End

End

sig[#sig + 1] = ") V"

return args, Table.concat (SIG)

End

function Luaj.callstaticmethod (className, MethodName, args, SIG)

Local args, sig = Checkarguments (args, SIG)

--echoinfo ("Luaj.callstaticmethod" (\ "%s\", \n\t\ "%s\", \n\targs,\n\t\ "%s\" ", ClassName, MethodName, SIG)

Return Calljavastaticmethod (ClassName, MethodName, args, SIG)

End

Return Luaj

As you can see from the inside, if the incoming signature is empty, a signature is generated automatically, but note that LUA does not have an shaping data type, and if the parameters received in Java are shaped, a signature of type float is passed in

An error occurs, so if you want to pass in the shaping data into the Java code, you need to manually write the signature yourself, and the signature of the shaping is "I".

Reprint please indicate the source, from the blog Park Hemjohn

LUA calls the Java procedure

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.