How to call JAVA methods in LUA

Source: Internet
Author: User

InLUAHow to callJAVA methodIs the content to be introduced in this article, mainly to learnLUACallingJAVA method, In useLuajavaIn open-source projects, how can we call java methods in lua and pass java objects as parameters to lua functions. The following is an example:

Create a loadScript class with the following code:

 
 
  1. public class LoadScript {  
  2. LuaState L;  
  3. LoadScript(final String filename){  
  4. this.L = LuaStateFactory.newLuaState();  
  5. this.L.openLibs();  
  6. this.L.LdoFile(filename);  
  7. }  
  8. void closeScript(){  
  9. this.L.close();  
  10. }  
  11. void runScriptFunction(String functionName,Object obj){  
  12. this.L.getGlobal(functionName);  
  13. this.L.pushJavaObject(obj);  
  14. this.L.call(1,0);  
  15. }  

Write a java object as follows:

 
 
  1. class LoadTest{  
  2. String a;  
  3. LoadScript script;  
  4. public LoadTest(String script){  
  5. this.script = new LoadScript(script+".lua");  
  6. this.script.runScriptFunction("create",this);  
  7. }  
  8. public String getA() {  
  9. return a;  
  10. }  
  11. public void setA(String a) {  
  12. this.a = a;  
  13. }  

The following is the test code:

 
 
  1. public static void main(String[] args) {  
  2. LoadTest test = new loadTest("function");  
  3. System.out.println(call.test.getA());  

In this way, we can pass the test object to the following lua code:

 
 
  1. function.lua:  
  2. function create(M)  
  3. M:setA("this is a")  
  4. end 

Summary: InLUAHow to callJAVA methodI hope this article will help you!

Related Article

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.