Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Native/Lua communication guide needed
Started by whammy Jun 28 2014 10:46 AM

50 replies to this topic
[TOPIC CONTROLS]
Page 1 of 3 1 2 3
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

How can I make CoronaCard to communicate with native environment?

 

I need to implement these native features:

 

- IAP

 

- Twitter score value sharing

 

- a Live Tile with the score value displayed on its back

 

But I don't know how to pass events/variables from one environment to another.

Is there a chance, that you could make a communication guide, similiar to the ones for iOS & Android CoronaCards, please?

 

Thanks,



[TOPIC: post.html]
#2

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

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.



[TOPIC: post.html]
#3

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

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.



[TOPIC: post.html]
#5

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

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.



[TOPIC: post.html]
#6

Lerg

[GLOBAL: userInfoPane.html]
Lerg
  • Contributor

  • 533 posts
  • Corona Staff

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.



[TOPIC: post.html]
#7

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

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.



[TOPIC: post.html]
#8

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

@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!

[TOPIC: post.html]
#9

Tapps - enterprise

[GLOBAL: userInfoPane.html]
Tapps - enterprise
  • Enthusiast

  • 75 posts
  • Corona SDK

Hello Everyone,

 

Is there an update about the native/lua communication ?



[TOPIC: post.html]
#10

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

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



[TOPIC: post.html]
#11

Tapps - enterprise

[GLOBAL: userInfoPane.html]
Tapps - enterprise
  • Enthusiast

  • 75 posts
  • Corona SDK

Thanks for the info Joshua !



[TOPIC: post.html]
#12

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

Joshua,

 

How's the bridge work going? Is there any update yet?

 

Thanks,



[TOPIC: post.html]
#13

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

I got derailed for a day last week.  I'm working on finishing it today.  Sorry about the delay.



[TOPIC: post.html]
#14

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

Sweet, thanks for the info!



[TOPIC: post.html]
#15

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

Sorry for being such a nag... but is there a real chance to get the bridge working soon? My postponed deadline comes in a week but I have to consider the windows store certification time as well.



[TOPIC: post.html]
#16

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

Sorry about the delay. I'm making several last minute design changes. Mostly because I think passing Object types to/from Lua has design issues.  Plus, having an object argument type doesn't make it obvious what the legal types are when pushing in arrays/lists and tables/dictionaries.  The more interesting case is what to do when an array of bytes is pushed in (something our network library does on our other platforms), which can be either pushed in as a Lua array of number or as a single Lua string.

 

The new design will be done by the end of tomorrow.



[TOPIC: post.html]
#17

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

I'm sure that the better design worth waiting, I just don't want to surpass my new deadline and that's why I keep asking.
Thanks for the details.

[TOPIC: post.html]
#18

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

I understand.  I definitely want your app to make it by the end of this month (or preferably earlier) too.  And if it's any consolation to you, I've been working 60 hour weeks trying to get Corona for WP8 to where you and everyone else needs it to be to.  It's just a lot of work.  :)



[TOPIC: post.html]
#19

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

I can imagine and I appreciate your work, really.



[TOPIC: post.html]
#20

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

Olaf,

 

The Native/Lua bridge is done.  Hopefully you'll get the newest version from David soon.  (We had some server problems today and yesterday... so hopefully everything is smoothed out now.)

 

Our newest WP8 .NET class framework documentation won't be uploaded to our website at the moment since we're gearing up for another release.  But that said, the zip file David will send you contains a help file documenting the newest APIs.  Plus, our Visual Studio integration will show all of our newest classes and their documentation via intellisense which should help you out.

 

Below is a quick example on how to communicate between C# and Lua.  It'll exercise some of its capabilities, hopefully giving you an idea on how to use it.

 

Here is an example MainPage.cs C# code that you can use to hook into the Corona runtime environment, listen in to the Corona runtime's lifecycle via its .NET events, add Lua style event listeners, and dispatch events to Lua (or even to other .NET event handlers) via a Lua like Runtime:dispatchEvent() call.  This code receives and dispatches Corona events.

public partial class MainPage : PhoneApplicationPage
{
	/// <summary>Construstor. Initializes member variables and the user interface.</summary>
	public MainPage()
	{
		// Initialize this page's components that were set up via the UI designer.
		InitializeComponent();

		// Set up Corona to automatically start up when the control's Loaded event has been raised.
		// Note: By default, Corona will run the "main.lua" file in the "Assets\Corona" directory.
		//       You can change the defaults via the CoronaPanel's AutoLaunchSettings property.
		fCoronaPanel.AutoLaunchEnabled = true;

		// Add Corona event handlers.
		fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded;
		fCoronaPanel.Runtime.Terminating += OnCoronaRuntimeTerminating;

		// Set up the CoronaPanel control to render fullscreen via the DrawingSurfaceBackgroundGrid control.
		// This significantly improves the framerate and is the only means of achieving 60 FPS.
		fCoronaPanel.BackgroundRenderingEnabled = true;
		fDrawingSurfaceBackgroundGrid.SetBackgroundContentProvider(fCoronaPanel.BackgroundContentProvider);
		fDrawingSurfaceBackgroundGrid.SetBackgroundManipulationHandler(fCoronaPanel.BackgroundManipulationHandler);
	}

	/// <summary>
	///  Called when the CoronaRuntimeEnvironment has been loaded/created and just before it executes the "main.lua".
	/// </summary>
	/// <param name="sender">Reference to the CoronaRuntime object that raised this event.</param>
	/// <param name="e">Provides access to the CoronaRuntimeEnvironment object that was just created/loaded.</param>
	private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)
	{
		// Set up Corona to call OnCustomCoronaEventReceived() when an event named "customEvent" gets dispatched.
		var eventHandler = new CoronaLabs.Corona.WinRT.CoronaLuaEventHandler(OnCoronaCustomEventReceived);
		e.CoronaRuntimeEnvironment.AddEventListener("customEvent", eventHandler);

		// You can also hook into an existing Corona event if you want to.
		eventHandler = new CoronaLabs.Corona.WinRT.CoronaLuaEventHandler(OnCoronaEnterFrameEventReceived);
		e.CoronaRuntimeEnvironment.AddEventListener("enterFrame", eventHandler);
	}

	/// <summary>Called when the CoronaRuntimeEnvironment is about to be destroyed.</summary>
	/// <param name="sender">Reference to the CoronaRuntime object that raised this event.</param>
	/// <param name="e">Provides access to the CoronaRuntimeEnvironment involved in this event.</param>
	private void OnCoronaRuntimeTerminating(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)
	{
		// This method gets called when backing out of the app or when the CoronaPanel control
		// has been unloade/removed from the application page.
	}

	/// <summary>Called when a Corona event named "customEvent" has been dispatched.</summary>
	/// <param name="sender">The Corona runtime environment the event was dispatched from.</param>
	/// <param name="e">Provides the event table's properties/fields.</param>
	private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnCoronaCustomEventReceived(
		CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e)
	{
		// Fetch the Corona event name.
		var coronaEventName = e.EventName;

		// Fetch the custom event's message field.
		var boxedMessage = e.Properties.Get("message") as CoronaLabs.Corona.WinRT.CoronaBoxedString;
		if (boxedMessage != null)
		{
			string myMessageFromLua = boxedMessage.ToUtf16String();
		}

		// Return a string back to whoever dispatched this event.
		return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("Hello from C#.");
	}

	/// <summary>
	///  <para>Called when Corona's "enterFrame" event hsa been dispatched by Corona.</para>
	///  <para>This is just an example. Not recommended since this might reduce framerate a bit.</para>
	/// </summary>
	private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnCoronaEnterFrameEventReceived(
		CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e)
	{
		// Create a custom Corona event named "dotNetEvent" and dispatch it.
		var eventProperties = CoronaLabs.Corona.WinRT.CoronaLuaEventProperties.CreateWithName("dotNetEvent");
		eventProperties.Set("message", "This event came from my C#.");
		eventProperties.Set("isCool", true);
		eventProperties.Set("powerLevel", 9000);
		var result = sender.DispatchEvent(new CoronaLabs.Corona.WinRT.CoronaLuaEventArgs(eventProperties));
		var boxedMessage = result.ReturnedValue as CoronaLabs.Corona.WinRT.CoronaBoxedString;
		if (boxedMessage != null)
		{
			var returnedMessage = boxedMessage.ToUtf16String();
		}

		// Corona's "enterFrame" event ignores return values. So, just return null.
		return null;
	}
}

And here is main.lua code that goes with the above code.  It will receive and dispatch events to/from C#.

-- Called when an event named "dotNetEvent" has been dispatched from C#.
local function onDotNetEventReceived(event)
	-- Let's send an event named "customEvent" back to C#.
	local myNewEvent =
	{
		name = "customEvent",
		message = "This event came from Lua.",
	}
	local result = Runtime:dispatchEvent(myNewEvent)
	print("The 'customEvent' handler returned " .. tostring(result))

	-- We can return a value to the dispatcher if we want.
	return "Hello from Lua!"
end
Runtime:addEventListener("dotNetEvent", onDotNetEventReceived)

I hope this helps!



[TOPIC: post.html]
#21

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

Joshua,

 

Thanks so much for this example, when I get the new CoronaCards version from David I test it out! 

One noob question, correct me if I'm wrong:

 

To hook up a Corona button without using enterFrame listener in C# I should just call this from inside a button:

Runtime:dispatchEvent({name = "customEvent"})

and then in C# in a "customEvent" listener I should define a "dotNetEvent" (right now it's defined only in an "enterFrame" listener):

var eventProperties = CoronaLabs.Corona.WinRT.CoronaLuaEventProperties.CreateWithName("dotNetEvent");

to receive back a "dotNetEvent" event in Lua?

 

OR

 

it's not necessary to add a "dotNetEvent" listener Lua, nor to define an event in C# if I'just to call this from the button:

local result = Runtime:dispatchEvent({name = "customEvent")

and this will just return a value I need from "customEvent" listener in C#?



[TOPIC: post.html]
#22

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

If your C# custom event listener is able to return a value immediately, then yes, I recommend that you return a value immediately.  For example...

local result = Runtime:dispatchEvent({name="requestingSum", x=2, y=2})
private void OnCoronaRuntimeLoaded(object sender, CoronaRuntimeEventArgs e)
{
   var handler = new CoronaLuaEventHandler(OnRequestingSum);
   e.CoronaRuntimeEnvironment.AddEventListener("requestingSum", handler);
}

private ICoronaBoxedData OnRequestingSum(CoronaRuntimeEnvironment sender, CoronaLuaEventArgs e)
{
   double sum = 0;
   try
   {
      double x = ((CoronaBoxedNumber)e.Properties.Get("x")).Value;
      double y = ((CoronaBoxedNumber)e.Properties.Get("y")).Value;
      sum = x + y;
   }
   catch (Exception) {}
   return new CoronaBoxedNumber(sum);
}

If you have to a non-blocking async fetch, then you'll need to create a separate event to be dispatched by C# later.  I hope what I said makes sense.

 

You can actually name the events whatever you want.  You don't have to use the names I used in the example above.  For example, I think the following names might be good for IAP in your app...

 

For example, these might be some good IAP event names on the Lua side...

- "requestingPurchasedProducts"  -- Return all products already purchased? (Not sure if you can do a blocking return.)

- "requestingPurchaseX"  -- Attempt to purchase product X.

- "requestingPurchaseY"  -- Attempt to purchase product Y.

 

And perhaps these might be some good event names on the C# side...

- "receivedPurchaseXResult"  -- The result of purchasing product X

- "receivedPurchaseYResult"  -- The result of purchasing product X

 
I don't recommend that you hook into the "enterFrame" event in C# because that would likely hurt your framerate a bit.  I was just trying to show an example that you can listen to any Runtime events; those that already exist in Corona and also the events that you can create and dispatch for yourself.


[TOPIC: post.html]
#23

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

So probably for IAPs I'd have to dispatch events from C#.

I've just received the new bits from David so I'll soon let you know how it works for me.

Thanks!



[TOPIC: post.html]
#24

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

I've got one more tip to make things simpler for you.  You don't have to wrap your .NET method within a handler class like I shown above.  For example, you can change the following...

var handler = new CoronaLuaEventHandler(OnRequestingSum);
e.CoronaRuntimeEnvironment.AddEventListener("requestingSum", handler);

...to the below, which works just as well...

e.CoronaRuntimeEnvironment.AddEventListener("requestingSum", OnRequestingSum);


[TOPIC: post.html]
#25

whammy

[GLOBAL: userInfoPane.html]
whammy
  • Contributor

  • 202 posts
  • Corona SDK

Thanks, I'll change that!

 

OK, I started testing the bridge and it's working, yay!  :) I can open url pages and I'm receiving events back in Lua.

 

I have a problem with screenshot sharing though. When I try to capture the screenshot using

WriteableBitmap save = new WriteableBitmap(480, 800);
save.Render(this, null);

I get a black screen captured. What argument should I pass here instead of 'this' to capture CoronaCards view?

Tried fCoronaPanel but didn't work for me neither.

 

Also - is there a way to make the CoronaCards view modal for a while?

So when I display a native popup, no clicks would go through?




[topic_controls]
Page 1 of 3 1 2 3
 
[/topic_controls]