Native/Lua communication guide needed

Happy to help!  Since you said you needed to upload your app by this Monday, we wanted to make sure that you had a reasonable amount of time to test it on your end.  Better to get it done early just in case.

And thanks for sharing a sneak peak of your app with me.  Can’t wait to see it in action for myself.  :)

Your app will be the first Corona SDK built app to make it onto the Windows Store.  So, this is pretty exciting from my perspective too.  :)

Oh and regarding the random crashes, if you find any more clues for me, then I’ll be more than happy to track it down.  I’m curious as to what that the real issue is too.

Sure, I’ll play with it under Managed mode and try to reproduce the crash. Although it happened very rarely. I’m thinking it could happen because the app couldn’t fetch IAPs when I tested the local build, maybe? I don’t encounter the crash on the beta submitted to windows phone store.

I’ll be happy to pass you a link when the game goes live. This will be after the App Campus QA Team let me unhide it from the store (assuming that Windows Store certification goes fine first).

I’ll be here anyway. Thanks for all!

I did a bit of research on your crash and that exit code you’re seeing matches the error code for a “SecurityException”, which can happen when accessing a file location or feature without the required permissions.  I assume that you have all of the required capabilities and requirements checked in your WMAppManifest file or else IAP wouldn’t have worked at all.  So, I’m not sure what might be causing this yet.  I’ll ask Microsoft about this later this week.

In any case, good luck with your submitted app.  I look forward to it showing up on the app store soon.  :slight_smile:

Thanks!

Hello Joshua,

I have just started testing the Native/Lua bridge and it seems to be working fine so far. Sending events to the Native side from Lua is pretty easy (register a listener and send the event to the lua Runtime object). But I have a question about sending events to Lua from the Native side.

In all your examples you register an enterFrame event listener in C# and use the RuntimeEnviroment that you get from the handler to dispatch the event to Lua. Is this the recommended way of getting access to the RuntimeEnviroment object ?

In my tests, I saved the RuntimeEnviroment object that I get from the OnCoronaRuntimeLoaded event listener and then use this object to dispatch my future events. Will I run into problems doing this (if, for example, this is not the only RuntimeEnviroment object or if it is not the same throughout the app lifecycle) ?

The “enterFrame” listener I’ve set up is just an example demonstrating that you can listen into standard Corona events that are normally delivered to Lua.  I don’t recommend that you set up an “enterFrame” listener on the C# side because that’ll cause needless overhead and may lower your framerate.  I recommend that you create your own event name on the Lua side instead.

Have a look at my “requestingSum” example here…

   http://forums.coronalabs.com/topic/49041-nativelua-communication-guide-needed/?p=257633

In that example, I dispatch a “requestingSum” event from Lua which is received from C#.  The C# code then sums the 2 parameters delivered via the event arguments and then returns the sum back to Lua as a return value.

Here are some more details if it helps you understand how this works.  Calling CoronaRuntimeEnvironment.DispatchEvent(), CoronaRuntimeEnvironment.AddEventLister(), and CoronaRuntimeEnvironment.RemoveEventListener() will literally call Lua functions Runtime:dispatchEvent(), Runtime:addEventListener(), and Runtime:removeEventListener() under the hood.  We’re merely bridging to these functions between .NET and Lua.
 
For example, the following dispatches and listens to a custom event purely on the Lua side.  This is what’s typically done with Lua libraries such as widgets, composer, etc.

-- Set up a custom event listener. local function onCustomEventReceived(event) print("Received custom event") end Runtime:addEventListener("customEvent", onCustomEventReceived) -- Dispatch the custom event. Runtime:dispatchEvent({name="customEvent"})

 
And here is the equivalent of the above completely written in C#. Note that this adds an event listener and dispatches an event completely on the C# side (nothing written on the Lua side). Not a practical example, but it demonstrates the concept of how our event dispatcher/listener system works.

void OnCoronaRuntimeLoaded(object sender, CoronaRuntimeEventArgs e) { // Add a custom event listener. e.CoronaRuntimeEnvironment.AddEventListener("customEvent", OnCustomEventReceived); // Dispatch the custom event. var eventProperties = CoronaLuaEventProperties.CreateWithName("customEvent"); var eventArgs = new CoronaLuaEventArgs(eventProperties); e.CoronaRuntimeEnvironment.DispatchEvent(eventArgs); } ICoronaBoxedData OnCustomEventReceived(CoronaRuntimeEnvironment sender, CoronaLuaEventArgs e) { System.Diagnostics.Debug.WriteLine("Received custom event"); }

 
Does this help any?

Okay, I got that part. But I still have one doubt. All functions to use events, for example CoronaRuntimeEnvironment.DispatchEvent(), are object methods, instead of static methods, right ? So you need access to a CoronaRuntimeEnvironment object, and in the example you just posted, this done using the e.CoronaRuntimeEnviroment property.

What I am wondering is if this object stays the same during the application lifetime. If it is, I could just save it and use it to send events later, something like this:

private CoronaRuntimeEnvironment  environment = null; private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e) {   environment = e.CoronaRuntimeEnvironment; } private void SendEventToLua() {   environment.DispatchEvent(); }

With the way the CoronaCards project template is made, you’ll only have 1 CoronaRuntimeEnvironment object for the lifetime of your WP8 app.  So, what you’re doing in your code example above will work fine.  In fact, you’ll find tat the Loaded event only gets raised once in your app.

Now, here’s a quick summary of the lifetime of a CoronaRuntimeEnvironment instance.  When you call the fCoronaPanel.Runtime.Run() method (or when it is auto-launched), it’ll terminate the last CoronaRuntimeEnvironment instance (if any) and then create/load a new instance.  That CoronaRuntimeEnvironment instance will continue to be active until it has been terminated via a call to the fCoronaPanel.Runtime.Terminate() method or when the fCoronaPanel has been removed/unloaded from the page.  The “Terminating” event will be raised in this case, which is your last chance to do anything in Corona/Lua before the internal Lua state gets destroyed.  Afterwards, the CoronaRuntimeEnvironment instance will be in a terminated state and calling its methods such as DispatchEvent() will do nothing (ie: no-op).

Note that the CoronaRuntimeEnvironment.RuntimeState property will indicate the current state of your project such as Running, Suspended, Resumed, Terminating, Terminated, etc.

   http://docs.coronalabs.com/daily/native/wp8/html/html/P_CoronaLabs_Corona_WinRT_CoronaRuntimeEnvironment_RuntimeState.htm

   http://docs.coronalabs.com/daily/native/wp8/html/html/T_CoronaLabs_Corona_WinRT_CoronaRuntimeState.htm

This might interest you too:

   http://docs.coronalabs.com/daily/native/wp8/html/html/E_CoronaLabs_Corona_WinRT_CoronaRuntime_Terminating.htm

It’s okay to use a member variable to hold on to a CoronaRuntimeEnvironment object reference like you are doing.  If you do happen to terminate the runtime yourself and create a new one, then the Loaded event will end up replacing your last CoronaRuntimeEnvironment reference.

Thanks for the info Joshua!

I suspected that it would be a single object, but wanted to make sure before I started to write code around it.

Right now, there is no means of communicating between native code and Lua on WP8.  We have not implemented a bridging layer yet.  So, it’s currently impossible to implement IAP and networking.  (This is still an early beta version after all.)

That said, the reason we’re releasing this early version now is that some Corona developers’ apps don’t require these features and the core features that we have now (rendering and audio) is enough for them to port their apps to WP8.  There is no reason to make other developers wait if they don’t require these features.

Would you be willing to exclude these features for now?

You can always add these features later once we’ve implemented them later.

Joshua,

I will fill you in with my background if David haven’t done this. The thing is that I am obliged to deliver to Microsoft the game with In App Purchases, Twitter sharing and a Live Tile on July 1st. I was told back in February that Corona would supports WP8 with these features until April. Forgive me if the tone of voice in my post might not be super cheerful, but I feel a bit stood up and very worried about the consequences of this delay.

My deadlines are: Release Candidate version on July 1st , and Realase version on August 1st, which were very reasonable hearing that the WP8 support would be ready in April/May. If I won’t deliver the game with implemented In App Purchases, a Live Tile, and Twitter sharing (maybe they could stand the app without the latter one), then I will lose the grant I was awarded in February. This is the fact. 

But let’s leave that alone for the moment, and let’s focus on the solution. I realise that your team won’t implement the communication bridge in a day, but could you please share with me what’s the implementation date for making it work?

Thanks.

Olaf

I estimate that creating that bridging layer will take 2-3 weeks… and that’s actually an aggressive schedule.  This is because we have to create the C++/CX WinRT Lua bridging layer ourselves from scratch.  Part of the reason that WP8 development has been slow is because a lot of the cross-platform libraries that we’ve been depending on are not supported on WP8, meaning that we either have to port it ourselves (such as with the Lua library and its socket library)  or create our implementation from scratch (such as with pthreads and OpenAL).

I think the thing to do here is re-negotiate with Microsoft on the release date.  I’ll talk to David about this.  We talk to Microsoft regularly, and bottom line, they just want to see more app on the Windows Phone app store.

If you’d manage to make it real in that timespan, that would be super great!

I’m aware I’ll need to talk with Microsoft and AppCampus. I just wanted to know where we actually are for today.

Thanks for the update.

The docs page says that socket.* lib is supported. Is it?

If so, you can do some networking with it’s help.

As for communication between native part and lua part you can use temporary files. In native part check every second or two if a local file exists, if so - read it and execute it according to it’s content. The other way around is also possible.

I’m currently thinking of implementing a different approach to bridging Lua and .NET on WP8 that should be able to implement much much faster.  The iOS and Android version CoronaCards provides a means of passing hash-tables/dictionaries between native code and Lua as documented here…

   http://docs.coronalabs.com/daily/coronacards/android/communication.html

It provides a much simpler means of communications, but should work good enough for your purposes.  Plus, I can save time by avoiding creating my own WinRT C++/CX wrapper around the Lua library.  I will still eventually have to create that wrapper to get Corona’s plugin model working, but that’s something I can put off to later at the moment.  So, I’ll look into providing this feature next in my queue.

@Joshua,
Yes, I think that’s a great idea. Passing sth like hashtables would be sufficient for the features I need to implement for now. Looking forward for it! Thank you!

@Lerq
As for checking every second if a file exist in a native part - I’m afraid it might reduce the game performance. It might do the job in a business app though. Thanks for the idea anyway!

Hello Everyone,

Is there an update about the native/lua communication ?

We’re working on it this week.  We’ll let you all know when the next WP8 Corona update supports it.

Thanks for the info Joshua !