8 Replies - 2761 Views - Last Post: 20 October 2009 - 09:09 AM Rate Topic: -----

#1 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

LuaJava - Pass variable from lua to java

Posted 19 October 2009 - 01:03 PM

I'm trying to pass variables picked up in lua over to a java program but don't know where to start. LuaJava has a poorly documented API and I've guess and checked everything I can think of. Is anyone familiar with LuaJava that could tell me how to this, I feel like it should be a no brainer. Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: LuaJava - Pass variable from lua to java

#2 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: LuaJava - Pass variable from lua to java

Posted 20 October 2009 - 03:52 AM

Still can't figure it out. Heres my code so far:

import java.awt.AWTException;
import org.keplerproject.luajava.*;

public class Dinger
{
	private static _Dinger Dinger;

	public static void main(String[] args) throws AWTException
	{
		Dinger = new _Dinger();		
		
		Dinger.Delay(1);
		
		LuaState LuaState = LuaStateFactory.newLuaState();
		
		LuaState.openLibs();
	
	 	LuaState.LdoFile("Dinger.lua");
		
		double i = LuaState.getNumber(cursorX); // one of the many ideas I've tried, but doesn't work
		
		System.out.println(i);

	}
}


This program successfully runs and prints the word "Test" with the double i line removed.

and here is Dinger.lua:

print("Test")
local cursorX = 5



One thing I did figure out though is that luajava's LdoFile(""); has some limitations.

For example:

LuaState.LdoFile("Dinger.lua"); works --- when .lua in same folder as program
LuaState.LdoFile("lua/Dinger.lua"); works --- when .lua is in a sub-folder of the program
LuaState.LdoFile("C:/Users/Public/Games/World of Warcraft/Interface/Addons/Dinger.lua"); -- doesnt work -- for locations of file unless in sub-folder

Basically the only thing I want to know how to do is get the value of cursorX in Dinger.lua to the double i in Dinger.java

This post has been edited by bmcginnis: 20 October 2009 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: LuaJava - Pass variable from lua to java

Posted 20 October 2009 - 03:55 AM

Unless someone is familiar with LuaJava, it would be impossible to tell you unless you post what you feel is the appropiate method to do this from the api.
Was This Post Helpful? 0
  • +
  • -

#4 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: LuaJava - Pass variable from lua to java

Posted 20 October 2009 - 07:59 AM

unfortunatly the api is hardly documented, with most the methods having nothing more than just the method name and i cant figure out how to pass a variable, if anyone has ideas that might work it would be appreciated

This post has been edited by bmcginnis: 20 October 2009 - 08:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,035
  • Joined: 14-September 07

Re: LuaJava - Pass variable from lua to java

Posted 20 October 2009 - 08:17 AM

I'll look into it as I'm sure the LuaJava library is similar to the C API for Lua. Check this out, it might point you in the right direction. Scroll down for the C API functions.
Was This Post Helpful? 1
  • +
  • -

#6 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,035
  • Joined: 14-September 07

Re: LuaJava - Pass variable from lua to java

Posted 20 October 2009 - 08:28 AM

In C, I would do this:

#include <cstdio>
#include <cstring>

#ifdef __cplusplus
extern "C" {
#endif
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#ifdef __cplusplus
}
#endif

int main()
{
	char buff[256];
	int error;
	lua_State *L = luaL_newstate();			/*open a new lua state*/
	luaL_openlibs(L);						/*open standard libraries*/

	luaL_dofile(L, "test.lua");
	lua_getglobal(L, "xCursor");
	printf("%d\n\n", lua_tointeger(L,lua_gettop(L)));
	lua_close(L); /*clean up*/
	return 0;
}



test.lua is:

Quote

xCursor = 5


Output:

Quote

5


Basically is gets the global value, pushes it onto the stack. We then retrieve it (it's at index 1 of the stack, Lua does indexes differently).
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,035
  • Joined: 14-September 07

Re: LuaJava - Pass variable from lua to java

Posted 20 October 2009 - 08:45 AM

Here's how I did it in java:

public static void main(String[] args){
		LuaState L = LuaStateFactory.newLuaState();
		L.LdoFile("test.lua");
		L.getGlobal("xCursor");
		System.out.println("xCursor's value is:" + L.toInteger(L.getTop()));
	}



I was getting zero, but that was due to the lua file not being in the right directory lol.
Was This Post Helpful? 1
  • +
  • -

#8 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: LuaJava - Pass variable from lua to java

Posted 20 October 2009 - 08:57 AM

That example looks promising as the API is fairly similar. The problem where I can't seem to find the similarity is here:

lua_getglobal(L, "xCursor"); // from your C code.

L.getGlobal("cursorX"); // Java code, getGlobal only has 1 arg which is a string since it realizes the luastate from L. infront.



converted code as best I could (not working though):
	LuaState L = LuaStateFactory.newLuaState();
		
	L.openLibs();
	
	 L.LdoFile("Dinger.lua");
		
	L.getGlobal("cursorX");
	
	int i = L.toNumber();
		
	System.out.println(i);



By the way toNumber requires int idx as an arg, would this have to do with the push index?
Was This Post Helpful? 0
  • +
  • -

#9 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: LuaJava - Pass variable from lua to java

Posted 20 October 2009 - 09:09 AM

just refreshed and realized u had posted a response before mine. It worked well thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1