Sending data from Lua to Obj-C

Hello,

i think i better open a new topic.

Recently i am testing coronacards with trial licence (nearly expire), the view sample projects work fine, then i test send data between Lua and Obj-C, sending from Obj-C to Lua is ok, but i get stuck on the last one of Lua to Obj-C, following the steps in the document but cannot make it, is some code in Obj-C missed? appreciate if there is a sample project to demonstrate it.

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

Thanks.

druid - is there a specific error you are seeing? Or can you post some of the code you are using that is causing the problem?

Based on the SimpleView project, paste the example code in ViewController.mm as follow (other files not modified):

// // ViewController.mm // SimpleView // // Copyright (c) 2014 Corona Labs Inc. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CoronaView \*view = (CoronaView \*)self.view; [view run]; // Send message to Lua NSDictionary \*event = [NSDictionary dictionaryWithObjectsAndKeys: @"pauseEvent", @"name", @"pause", @"phase", @"Message from Obj-C to Lua", @"message", nil]; [view sendEvent:event]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (id)coronaView:(CoronaView \*)view receiveEvent:(NSDictionary \*)event { NSLog( @"Logging properties of the event: %@", event ); // self.myLabel.text = [event objectForKey:@"message"]; return @"Nice to meet you CoronaCards!"; } @end

Similarly add the sample code in main.lua as follow:

--------------------------------------------------------------------------------------- -- A simple physics example in 8 lines! --------------------------------------------------------------------------------------- local physics = require( "physics" ) physics.start() local sky = display.newImage( "sky.png", 160, 195 ) local ground = display.newImage( "ground.png", 160, 445 ) local crate = display.newImage( "crate.png", 180, -50 ) crate.rotation = 5 physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } ) physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } ) local myText = display.newText( "Hello World!", display.contentWidth / 2, display.contentHeight \* 0.3, native.systemFont, 16 ) -- Receive message from Obj-C local function handlePause( event ) if ( event.phase == "pause" ) then print( event.name ) --\> pauseEvent print( event.message ) --\> message --handle pause implementation here myText.text = event.message end return true end Runtime:addEventListener( "pauseEvent", handlePause ) -- Send message to Obj-C local function pressMe( event ) if ( event.phase == "ended" ) then local event = { name="coronaView", message="Hello from CoronaCards!" } print( "press crate" ) --dispatch the event to the global Runtime object local result = Runtime:dispatchEvent( event ) print( result ) --\> Response: Nice to meet you CoronaCards! end return true end crate:addEventListener( "touch", pressMe )

Run the program in iPhone simulator, the message “Message from Obj-C to Lua” shown on screen and log indicate sending from Obj-C to Lua works. Press the crate and in the log shown “press crate” and “false” (the return message). i think i have not register the delegate yet in Obj-C code but just have no idea how to do it.

Thanks for help.

The delegate pattern is a fundamental Obj-C pattern. Lots of good online resources if this is new for you.

In your case, I believe you forgot to set the CoronaView’s ‘coronaViewDelegate’ property so that it knows your ViewController instance will respond to delegate messages. You need something like the following in your ‘viewDidLoad’:

view.coronaViewDelegate = self;

Thank you! It works now!

Note that “ViewController.h” also need to be modified a bit, although non-newbies should know that i bet. :slight_smile:

#import "CoronaCards/CoronaCards.h" @interface ViewController : CoronaViewController \<CoronaViewDelegate\> @end

druid - is there a specific error you are seeing? Or can you post some of the code you are using that is causing the problem?

Based on the SimpleView project, paste the example code in ViewController.mm as follow (other files not modified):

// // ViewController.mm // SimpleView // // Copyright (c) 2014 Corona Labs Inc. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CoronaView \*view = (CoronaView \*)self.view; [view run]; // Send message to Lua NSDictionary \*event = [NSDictionary dictionaryWithObjectsAndKeys: @"pauseEvent", @"name", @"pause", @"phase", @"Message from Obj-C to Lua", @"message", nil]; [view sendEvent:event]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (id)coronaView:(CoronaView \*)view receiveEvent:(NSDictionary \*)event { NSLog( @"Logging properties of the event: %@", event ); // self.myLabel.text = [event objectForKey:@"message"]; return @"Nice to meet you CoronaCards!"; } @end

Similarly add the sample code in main.lua as follow:

--------------------------------------------------------------------------------------- -- A simple physics example in 8 lines! --------------------------------------------------------------------------------------- local physics = require( "physics" ) physics.start() local sky = display.newImage( "sky.png", 160, 195 ) local ground = display.newImage( "ground.png", 160, 445 ) local crate = display.newImage( "crate.png", 180, -50 ) crate.rotation = 5 physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } ) physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } ) local myText = display.newText( "Hello World!", display.contentWidth / 2, display.contentHeight \* 0.3, native.systemFont, 16 ) -- Receive message from Obj-C local function handlePause( event ) if ( event.phase == "pause" ) then print( event.name ) --\> pauseEvent print( event.message ) --\> message --handle pause implementation here myText.text = event.message end return true end Runtime:addEventListener( "pauseEvent", handlePause ) -- Send message to Obj-C local function pressMe( event ) if ( event.phase == "ended" ) then local event = { name="coronaView", message="Hello from CoronaCards!" } print( "press crate" ) --dispatch the event to the global Runtime object local result = Runtime:dispatchEvent( event ) print( result ) --\> Response: Nice to meet you CoronaCards! end return true end crate:addEventListener( "touch", pressMe )

Run the program in iPhone simulator, the message “Message from Obj-C to Lua” shown on screen and log indicate sending from Obj-C to Lua works. Press the crate and in the log shown “press crate” and “false” (the return message). i think i have not register the delegate yet in Obj-C code but just have no idea how to do it.

Thanks for help.

The delegate pattern is a fundamental Obj-C pattern. Lots of good online resources if this is new for you.

In your case, I believe you forgot to set the CoronaView’s ‘coronaViewDelegate’ property so that it knows your ViewController instance will respond to delegate messages. You need something like the following in your ‘viewDidLoad’:

view.coronaViewDelegate = self;

Thank you! It works now!

Note that “ViewController.h” also need to be modified a bit, although non-newbies should know that i bet. :slight_smile:

#import "CoronaCards/CoronaCards.h" @interface ViewController : CoronaViewController \<CoronaViewDelegate\> @end

I have the same problem as OP had, but on Android. Communication from Java to Lua works, but from Lua to Java doesn’t (I am following the docs at http://docs.coronalabs.com/coronacards/android/communication.html)..)

Is it supposed to work (given Android version is in beta)?

Is there a working sample I can learn from?

Thanks, Denis

false alarm, look like I’ve figured it out.

I have the same problem as OP had, but on Android. Communication from Java to Lua works, but from Lua to Java doesn’t (I am following the docs at http://docs.coronalabs.com/coronacards/android/communication.html)..)

Is it supposed to work (given Android version is in beta)?

Is there a working sample I can learn from?

Thanks, Denis

false alarm, look like I’ve figured it out.

This info should be on github samples and in the docs http://docs.coronalabs.com/daily/coronacards/ios/communication.html

This info should be on github samples and in the docs http://docs.coronalabs.com/daily/coronacards/ios/communication.html