There is good sample for ios where we have a function [cpp] Corona::Lua::RuntimeDispatchEvent( lua_State, int );[/cpp] , but I could not find the same function for android. The fact is that I use the Samsung S Pen SDK which provide the possible to determine pressure level and I need to add relevant field to event object. I tried to call manually [lua]dispatchEvent[/lua] lua function from java with [java]LuaState.call (int, int)[/java]. It works, but application crashes with error after some time. [import]uid: 103808 topic_id: 33865 reply_id: 333865[/import]
Have a look at the following sample project on how to call a Lua function from another thread on Android…
[lua]./Android/SampleProjects/ExtendingLua/src/com/coronalabs/corona/sample/extendinglua/AsyncCallLuaFunction.java[/lua]
Note that it is crashing on you because the Corona runtime does not run on the main UI thread on Android. Manipulating Corona’s LuaState from another thread can cause race conditions to occur and ultimately crash your app. The sample code mentioned above shows you have to get back on to the Corona thread via our [lua]CoronaRuntimeTaskDispatcher[/lua] class so that you can access Corona’s LuaState safely. That sample code also shows you how to store a Lua function to the Lua registry so it can be called later after an async operation has completed.
[import]uid: 32256 topic_id: 33865 reply_id: 134679[/import]
Thank you very much, it helped [import]uid: 103808 topic_id: 33865 reply_id: 134852[/import]
Will the Android Enterprise API get the equivalent to Corona::Lua::RuntimeDispatchEvent in the short term? It would be very nice to have functional parity so I don’t have to solve issues like this twice (once for iOS and once for Android) [import]uid: 122310 topic_id: 33865 reply_id: 134860[/import]
Alternately, can Corona provide a sample that actually deals with the event dispatch case (make a table and post it to Corona runtime)? It does so for the iOS samples, and this is a pretty common but tricky case that is requiring a lot more tech skill than I expected. [import]uid: 122310 topic_id: 33865 reply_id: 134861[/import]
Alternately, can Corona provide a sample that actually deals with the event dispatch case (make a table and post it to Corona runtime)? It does so for the iOS samples, and this is a pretty common but tricky case that is requiring a lot more tech skill than I expected. [import]uid: 122310 topic_id: 33865 reply_id: 134862[/import]
Happy to help!
Yes, we plan on adding an equivalent [lua]RuntimeDispatchEvent[/lua] to Android in the future. It just so happened that this functionality was snuck into iOS just after we finished working on Android for Enterprise at the time. But hey, we did go out of our way to provide a lot of nice enterprise sample projects with detailed descriptions on Android to help make development easier. You should check out the “ExtendingUI” sample, because that one is particularly nice. [import]uid: 32256 topic_id: 33865 reply_id: 134874[/import]
Have a look at the following sample project on how to call a Lua function from another thread on Android…
[lua]./Android/SampleProjects/ExtendingLua/src/com/coronalabs/corona/sample/extendinglua/AsyncCallLuaFunction.java[/lua]
Note that it is crashing on you because the Corona runtime does not run on the main UI thread on Android. Manipulating Corona’s LuaState from another thread can cause race conditions to occur and ultimately crash your app. The sample code mentioned above shows you have to get back on to the Corona thread via our [lua]CoronaRuntimeTaskDispatcher[/lua] class so that you can access Corona’s LuaState safely. That sample code also shows you how to store a Lua function to the Lua registry so it can be called later after an async operation has completed.
[import]uid: 32256 topic_id: 33865 reply_id: 134679[/import]
Thank you very much, it helped [import]uid: 103808 topic_id: 33865 reply_id: 134852[/import]
Will the Android Enterprise API get the equivalent to Corona::Lua::RuntimeDispatchEvent in the short term? It would be very nice to have functional parity so I don’t have to solve issues like this twice (once for iOS and once for Android) [import]uid: 122310 topic_id: 33865 reply_id: 134860[/import]
Alternately, can Corona provide a sample that actually deals with the event dispatch case (make a table and post it to Corona runtime)? It does so for the iOS samples, and this is a pretty common but tricky case that is requiring a lot more tech skill than I expected. [import]uid: 122310 topic_id: 33865 reply_id: 134861[/import]
Alternately, can Corona provide a sample that actually deals with the event dispatch case (make a table and post it to Corona runtime)? It does so for the iOS samples, and this is a pretty common but tricky case that is requiring a lot more tech skill than I expected. [import]uid: 122310 topic_id: 33865 reply_id: 134862[/import]
Happy to help!
Yes, we plan on adding an equivalent [lua]RuntimeDispatchEvent[/lua] to Android in the future. It just so happened that this functionality was snuck into iOS just after we finished working on Android for Enterprise at the time. But hey, we did go out of our way to provide a lot of nice enterprise sample projects with detailed descriptions on Android to help make development easier. You should check out the “ExtendingUI” sample, because that one is particularly nice. [import]uid: 32256 topic_id: 33865 reply_id: 134874[/import]
Hey Joshua
Can you please provide an example of how you would replicate the functionality of this ios utility method in java for android: CoronaLuaNewEvent( L, “eventname”)
I’m trying to recreate the following obj-c code for android:
::DispatchEvent(bool isError) const
{
lua\_State \*L = fRuntime.L;
CoronaLuaNewEvent( L, CoronaEventAdsRequestName() );
lua\_pushstring( L, kProviderName );
lua\_setfield( L, -2, CoronaEventProviderKey() );
lua\_pushboolean( L, isError );
lua\_setfield( L, -2, CoronaEventIsErrorKey() );
CoronaLuaDispatchEvent( L, fListener, 0 );
}
Problem being the CoronaLuaNewEvent and what that does?
[import]uid: 63706 topic_id: 33865 reply_id: 137335[/import]
We don’t have an equivalent “DispatchEvent” function in Java yet. This means that you’ll have to create the event table and call a Lua function yourself.
Have a look at sample project “SimpleLuaExtensions” (aka: “ExtendingLua” if you are using an older Corona Enterprise build).
See the following file on how to call a Lua function immediately from the same thread.
(This allows you to create blocking functions that call a Lua function.)
- CallLuaFunction.java
See the following file on how to call a Lua function from another thread…
- AsyncCallLuaFunction.java
See the following file on how to create a Lua table in Java and add fields to it…
- GetRandomTableLuaFunction.java
[import]uid: 32256 topic_id: 33865 reply_id: 137413[/import]
Hi,
Kim ended up storing the listener and dispatcher in 2 static variables to be able to asynchronously call back to the lua listener passed in when the plugin is inited. Works fine so far. Any reason not to do it like that? We also thought about creating a singleton for it. [import]uid: 21746 topic_id: 33865 reply_id: 137421[/import]
Haakon,
There is one thing you have to watch out for on Android that behaves differently compared to iOS.
The CoronaRuntime and its LuaState is tied to the CoronaActivity’s lifecycle and not the Application. That is, when you launch a new CoronaActivity, then a new CoronaRuntime and LuaState object gets created. When you tap “back” to exit the CoronaActivity, the CoronaRuntime and its LuaState are disposed of and dereferenced to be garbage collected, but your app’s process, its Application object, and all of its static variables remain alive. So, the next time you tap your app icon to launch a new CoronaActivity (while your app process is still running in the background), a new CoronaRuntime and LuaState object gets created.
The thing you got to watch out for then is that the CoronaRuntimeTaskDispatcher can only send tasks to the CoronaRuntime that you fed into its constructor. Once that CoronaRuntime has been disposed of, which happens when you back out of a CoronaActivity, then the dispatcher will stop working and no-op when you call its send() method from then on, which is by design.
Note: You can determine if the dispatcher’s target is still available (ie: not disposed of) by calling the dispatcher’s isRuntimeAvailable() method.
http://docs.coronalabs.com/native/android/html/com/ansca/corona/CoronaRuntimeTaskDispatcher.html
This means that the next time a CoronaActivity gets launched, you’ll need to replace your dispatcher object with a new one that target’s the CoronaActivity’s new CoronaRuntime object. So, it’s okay to store the dispatcher object to a static variable. Just make sure to update it.
Setting up a global CoronaRuntimeListener like you’re doing is the easiest way to handle runtime events. It’s onLoaded() method will be called every time a new CoronaRuntime object has been created, so that is where you need to create a new dispatcher and replace your old one.
Anyways, I hope this helps!
[import]uid: 32256 topic_id: 33865 reply_id: 137458[/import]
Joshua,
that helps a lot, it confirms the things we had guessed, thank you. [import]uid: 21746 topic_id: 33865 reply_id: 137486[/import]
Hey Joshua
Can you please provide an example of how you would replicate the functionality of this ios utility method in java for android: CoronaLuaNewEvent( L, “eventname”)
I’m trying to recreate the following obj-c code for android:
::DispatchEvent(bool isError) const
{
lua\_State \*L = fRuntime.L;
CoronaLuaNewEvent( L, CoronaEventAdsRequestName() );
lua\_pushstring( L, kProviderName );
lua\_setfield( L, -2, CoronaEventProviderKey() );
lua\_pushboolean( L, isError );
lua\_setfield( L, -2, CoronaEventIsErrorKey() );
CoronaLuaDispatchEvent( L, fListener, 0 );
}
Problem being the CoronaLuaNewEvent and what that does?
[import]uid: 63706 topic_id: 33865 reply_id: 137335[/import]
We don’t have an equivalent “DispatchEvent” function in Java yet. This means that you’ll have to create the event table and call a Lua function yourself.
Have a look at sample project “SimpleLuaExtensions” (aka: “ExtendingLua” if you are using an older Corona Enterprise build).
See the following file on how to call a Lua function immediately from the same thread.
(This allows you to create blocking functions that call a Lua function.)
- CallLuaFunction.java
See the following file on how to call a Lua function from another thread…
- AsyncCallLuaFunction.java
See the following file on how to create a Lua table in Java and add fields to it…
- GetRandomTableLuaFunction.java
[import]uid: 32256 topic_id: 33865 reply_id: 137413[/import]