lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


For what it's worth, this is the equivalent code in Kahlua, disregard it if you are already commited to using LuaJava:

    public void test() {
        LuaState state = new LuaState();
        Object init = state.getEnvironment().rawget("init");
       
        Object[] results = state.pcall(init);
        Boolean isOk = (Boolean) results[0];
        if (isOk) {
            LuaTable t = (LuaTable) results[1];
            Object value = t.rawget(3);
            System.out.println(value);
        }
    }

Which I personally think is easier to use. You don't have to worry about stack manipulation at all, and you get the real objects directly.
If you want to get the keys for the table, this is also pretty simple.
This is a snippet from my upcoming lua <-> java converter code, but you can of course run the same thing manually.

                Object key = null;
                while (true) {
                    key = table.next(key);
                    if (key == null) {
                        return map;
                    }
                    Object value = table.rawget(key);
                    map.put(key, value);
                }


To Robert G. Jakabosky:
I suspect the methods you mentioned are in LuaState because they could trigger further lua calls, what with metamethods involved all, but I haven't really investigated it - I just know I have made similar solutions in Kahlua.

On Tue, Jun 9, 2009 at 1:11 PM, Anselmo Junior <afajunior@gmail.com> wrote:
But its return me the length of the table right? But dont return me if the 2 is nill and key 3 has some value...

But i try this method to see what happen, and return me 0


        state.getGlobal("init");
        state.call(0, 1);
        System.out.println(state.objLen(-1));

when i use call method, the function 'init' will run and the return value is pushed on the stack. If i understand right, i can access this value with -1 index, where is the last element that be push on the stack.

But i left this subject for after, i really need to get what numeric keys is used. I dont know if its possible in LuaJava (or in LUA)

2009/6/9 Robert G. Jakabosky <bobby@sharedrealm.com>
On Tuesday 09, Anselmo Junior wrote:
> Oh yeah, and I recently try to do that and work. Only need get what key in
> the script is in use to get the values.

Method objLen in the LuaState class:
public int objLen(int idx)

Looks like you have to use the Lua stack idx of the table.

For strings it will return the string length, for tables it will return the
results of the length operator '#'...
See more here:
http://www.lua.org/manual/5.1/manual.html#lua_objlen

--
Robert G. Jakabosky