How can I use CoronaLua.dispatchRuntimeEvent()?

I used it like below. But it doesn’t work.

What is my fault?


public static com.naef.jnlua.LuaState L;

public static void dispatchEventToCorona(final Hashtable<Object, Object> parameters)
{
    CoronaEnvironment.getCoronaActivity().runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            CoronaLua.newEvent(L, “nativeEvent”);

            CoronaLua.pushHashtable(L, parameters);

            CoronaLua.dispatchRuntimeEvent(L, 0);
        }
    });

    return;
}


How can I use it? :slight_smile:

What exactly doesn’t work? I tried writing small “plugin” using same functions, everything working for me. Here’s code:
 
App/android/plugin/src/main/java/plugin/timer/LuaLoader.java

package plugin.timer; import com.ansca.corona.CoronaEnvironment; import com.ansca.corona.CoronaLua; import com.naef.jnlua.JavaFunction; import com.naef.jnlua.LuaState; import com.naef.jnlua.NamedJavaFunction; import java.util.Hashtable; import java.util.Timer; import java.util.TimerTask; @SuppressWarnings("unused") public class LuaLoader implements JavaFunction { @Override public int invoke(final LuaState L) { NamedJavaFunction[] luaFunctions = new NamedJavaFunction[] { new NamedJavaFunction() { @Override public String getName() { return "start"; } @Override public int invoke(LuaState luaState) { final long period; if(L.isNumber(1)) { period = (long)(L.toNumber(1) \* 1000); } else { period = 500L; } final Hashtable\<Object, Object\> parameters = new Hashtable\<\>(); parameters.put("Hello", "World"); new Timer().schedule(new TimerTask() { @Override public void run() { CoronaEnvironment.getCoronaActivity().runOnUiThread(new Runnable() { @Override public void run() { CoronaLua.newEvent(L, "TimerEvent"); CoronaLua.pushHashtable(L, parameters); L.setField(-2, "parameters"); CoronaLua.dispatchRuntimeEvent(L, 0); } }); } }, 0, period); return 0; } }, }; String libName = L.toString( 1 ); L.register(libName, luaFunctions); return 1; } }

Lua Code:
 

local t = require "plugin.timer" local json = require "json" t.start(0.5) Runtime:addEventListener( "TimerEvent", function( e ) &nbsp; &nbsp; &nbsp;print("----------- TimerEvent received: ", json.encode(e)) end )

 
Only obvious problem with your code, is that you don’t assign pushed “parameters” table to anything: CoronaLua.pushHashtable would just push table on top of stack. You have to assign it to the field, as I do with L.setField(-2, “parameters”);
 
Hope it helps.
Cheers!

TL;DR:

Change 

            CoronaLua.newEvent(L, “nativeEvent”);

            CoronaLua.pushHashtable(L, parameters);

            CoronaLua.dispatchRuntimeEvent(L, 0);

 

to

 

            CoronaLua.newEvent(L, “nativeEvent”);

            CoronaLua.pushHashtable(L, parameters);
            L.setField( -2, “parameters”);
            CoronaLua.dispatchRuntimeEvent(L, 0);

Oh! Thank you very much! :slight_smile:

And I have a new question.

How can I use it on iOS? :slight_smile:

Corona::Lua::NewEvent(L, “nativeEvent”);

 

// What can I do for NSHashTable or NSDictionary?

 

Corona::Lua::DispatchRuntimeEvent( L, 0 );

CoronaLuaObjCHelper.h has CoronaLuaPushValue which accepts NS* objects, including NSDictionary.

Thank you! They worked! :slight_smile:

How can I use it? :slight_smile:

What exactly doesn’t work? I tried writing small “plugin” using same functions, everything working for me. Here’s code:
 
App/android/plugin/src/main/java/plugin/timer/LuaLoader.java

package plugin.timer; import com.ansca.corona.CoronaEnvironment; import com.ansca.corona.CoronaLua; import com.naef.jnlua.JavaFunction; import com.naef.jnlua.LuaState; import com.naef.jnlua.NamedJavaFunction; import java.util.Hashtable; import java.util.Timer; import java.util.TimerTask; @SuppressWarnings("unused") public class LuaLoader implements JavaFunction { @Override public int invoke(final LuaState L) { NamedJavaFunction[] luaFunctions = new NamedJavaFunction[] { new NamedJavaFunction() { @Override public String getName() { return "start"; } @Override public int invoke(LuaState luaState) { final long period; if(L.isNumber(1)) { period = (long)(L.toNumber(1) \* 1000); } else { period = 500L; } final Hashtable\<Object, Object\> parameters = new Hashtable\<\>(); parameters.put("Hello", "World"); new Timer().schedule(new TimerTask() { @Override public void run() { CoronaEnvironment.getCoronaActivity().runOnUiThread(new Runnable() { @Override public void run() { CoronaLua.newEvent(L, "TimerEvent"); CoronaLua.pushHashtable(L, parameters); L.setField(-2, "parameters"); CoronaLua.dispatchRuntimeEvent(L, 0); } }); } }, 0, period); return 0; } }, }; String libName = L.toString( 1 ); L.register(libName, luaFunctions); return 1; } }

Lua Code:
 

local t = require "plugin.timer" local json = require "json" t.start(0.5) Runtime:addEventListener( "TimerEvent", function( e ) &nbsp; &nbsp; &nbsp;print("----------- TimerEvent received: ", json.encode(e)) end )

 
Only obvious problem with your code, is that you don’t assign pushed “parameters” table to anything: CoronaLua.pushHashtable would just push table on top of stack. You have to assign it to the field, as I do with L.setField(-2, “parameters”);
 
Hope it helps.
Cheers!

TL;DR:

Change 

            CoronaLua.newEvent(L, “nativeEvent”);

            CoronaLua.pushHashtable(L, parameters);

            CoronaLua.dispatchRuntimeEvent(L, 0);

 

to

 

            CoronaLua.newEvent(L, “nativeEvent”);

            CoronaLua.pushHashtable(L, parameters);
            L.setField( -2, “parameters”);
            CoronaLua.dispatchRuntimeEvent(L, 0);

Oh! Thank you very much! :slight_smile:

And I have a new question.

How can I use it on iOS? :slight_smile:

Corona::Lua::NewEvent(L, “nativeEvent”);

 

// What can I do for NSHashTable or NSDictionary?

 

Corona::Lua::DispatchRuntimeEvent( L, 0 );

CoronaLuaObjCHelper.h has CoronaLuaPushValue which accepts NS* objects, including NSDictionary.

Thank you! They worked! :slight_smile: