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.
LuaJava - Pass variable from lua to javaanyone familiar?
Page 1 of 1
8 Replies - 2761 Views - Last Post: 20 October 2009 - 09:09 AM
Replies To: LuaJava - Pass variable from lua to java
#2
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:
This program successfully runs and prints the word "Test" with the double i line removed.
and here is Dinger.lua:
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
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
#3
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.
#4
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
#5
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.
#6
Re: LuaJava - Pass variable from lua to java
Posted 20 October 2009 - 08:28 AM
In C, I would do this:
test.lua is:
Output:
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).
#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).
#7
Re: LuaJava - Pass variable from lua to java
Posted 20 October 2009 - 08:45 AM
Here's how I did it in java:
I was getting zero, but that was due to the lua file not being in the right directory lol.
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.
#8
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):
By the way toNumber requires int idx as an arg, would this have to do with the push index?
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?
#9
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|