Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

C# to Lua Dispatch Event - How to - outside of onCoronaRunTimeStarted
Started by doubleslashdesign Aug 20 2015 04:33 PM

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

doubleslashdesign

[GLOBAL: userInfoPane.html]
doubleslashdesign
  • Contributor

  • 727 posts
  • Corona SDK

Here is the code from the Documentation

private void OnCoronaRuntimeStarted(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)
{
var eventProperties = CoronaLabs.Corona.WinRT.CoronaLuaEventProperties.CreateWithName("requestingSum");
eventProperties.Set("x", 5);
eventProperties.Set("y", 10);
 eventProperties.Set("message", "Hello. Can you add these 2 numbers please?");

 // Dispatch the event to Lua.
    var eventArgs = new CoronaLabs.Corona.WinRT.CoronaLuaEventArgs(eventProperties);
    var result = e.CoronaRuntimeEnvironment.DispatchEvent(eventArgs);
}

my issue is that you are using e.CoronaRuntimeEnvironment.DispatchEvent(eventArgs);

 

within the onCoronaRunTimeStarted method.

 

I would like to send notifications to LUA based on other windows phone events.

 

so lets say i want to send a time tick ( not really but good example ).

 

If i had a timer of some kind on the windows side and every tick i wanted to let lua know to animate a clock based on the above it looks like I have to pass ( e - which is CoronaRuntimeEventArgs ) that is not available in other windows events so do you have an example of doing that?

 

thanks

 

Larry

 

 

 

 

 

Attached Files



[TOPIC: post.html]
#2

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

You'll need to keep a reference to the CoronaRuntimeEnvironment object as a member variable so that you can use it later.
 
The best time to grab a hold of the CoronaRuntimeEnvironment object reference is via the "Loaded" event.  And you should also null out that member variable reference during the "Terminating" event.
 
Here is an example...

public partial class MainPage : PhoneApplicationPage
{
	/// <summary>
	///  <para>Reference to the Corona runtime environment that is currently active.</para>
	///  <para>Will be null if a Corona runtime has not been created yet or has been destroyed.</para>
	/// </summary>
	private CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment fCoronaRuntimeEnvironment = null;

	/// <summary>Timer used to test C# to Lua communications.</summary>
	private System.Windows.Threading.DispatcherTimer fDispatcherTimer;


	/// <summary>Creates and initializes the main page which hosts the CoronaPanel XAML control.</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;

		// 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);

		// Create a timer used to dispatch an event from C# to Lua every 1 second.
		fDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
		fDispatcherTimer.Interval = TimeSpan.FromSeconds(1.0);
		fDispatcherTimer.Tick += OnTimerTick;
		fDispatcherTimer.Start();

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

	/// <summary>
	///  Called when a new CoronaRuntimeEnvironment has been created/loaded,
	///  but before the "main.lua" has been executed.
	/// </summary>
	/// <param name="sender">The CoronaRuntime object that raised this event.</param>
	/// <param name="e">Event arguments providing the CoronaRuntimeEnvironment that has been created/loaded.</param>
	private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)
	{
		// Keep a reference to the Corona runtime environment that was just created/loaded.
		fCoronaRuntimeEnvironment = e.CoronaRuntimeEnvironment;
	}

	/// <summary>Called when the CoronaRuntimeEnvironment is about to be terminated.</summary>
	/// <param name="sender">The CoronaRuntime object that raised this event.</param>
	/// <param name="e">Event arguments providing the CoronaRuntimeEnvironment that is about to be terminated.</param>
	private void OnCoronaRuntimeTerminating(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)
	{
		// Let go of the Corona runtime environment reference.
		fCoronaRuntimeEnvironment = null;
	}

	/// <summary>Called every time "fDispatcherTimer" elapses.</summary>
	/// <param name="sender">The dispatcher timer that raised this event.</param>
	/// <param name="e">Empty event arguments.</param>
	private void OnTimerTick(object sender, EventArgs e)
	{
		// Do not continue if a Corona runtime is not running.
		if (fCoronaRuntimeEnvironment == null)
		{
			return;
		}

		// Dispatch a "timerTicked" event to Lua.
		var eventProperties = CoronaLabs.Corona.WinRT.CoronaLuaEventProperties.CreateWithName("timerTicked");
		fCoronaRuntimeEnvironment.DispatchEvent(new CoronaLabs.Corona.WinRT.CoronaLuaEventArgs(eventProperties));
	}
}


[TOPIC: post.html]
#3

doubleslashdesign

[GLOBAL: userInfoPane.html]
doubleslashdesign
  • Contributor

  • 727 posts
  • Corona SDK

Wonderful, this will get me exactly what I needed, I figured this is how I would have to do it but I did not know what impact that would have on corona itself.... This is awsome, I am working on a little project that I hope will be very cool.

 

Thanks so much :)

 

Larry




[topic_controls]
[/topic_controls]