How call methods of lua from objective-C?

Hi, I am iOS developer. Now, I use Corona Enterprise for iOS. My problem is I need call method of lua from Objective-C (.m), is it possible?

If is possible how? and if not possible, how implement delegate in PluginLibrary.mm?

Please help me.

[Aditional]

I found this link, but I does not work. http://bit.ly/1pibg6i

Any idea?

Example:

in Lua

function myLuaFunction()

    …

end

Runtime:addEventListener( “myLuaFunction”, listener )

===========================

in Objective C

@implementation MyCoronaDelegate
 

id<CoronaRuntime> runtimeObj;

  • (void)didLoadMain:(id<CoronaRuntime>)runtime
    {

    runtimeObj = runtime;
    lua_State *L = runtime.L;
}

  • (void)toCallLuaFunction

{

const char kNameKey[] = “name”;
const char kValueKey[] = “myLuaFunction”;
lua_newtable( L );
lua_pushstring( L, kValueKey ); // All events are Lua tables
lua_setfield( L, -2, kNameKey ); // that have a ‘name’ property

Corona::Lua::RuntimeDispatchEvent( L, -1 );

}

@end

But I don’t know how to pass parameter to Lua function from ObjectiveC.

I just store parameter in ObjC, then ask Lua to get back this parameter.

Thanks, sayhong. I solved this problem already.

Solved:

Include CoronaLua.h & CoronaRuntime.h in Objective-C

[Begin::Code in Objective-C]

CoronaLuaNewEvent( L, "methodName"); //.... Parameters .....// lua\_pushnumber(L, value); lua\_setfield(L, -2, "parameterName"); lua\_pushinteger(L, value); lua\_setfield(L, -2, "parameterName"); lua\_pushstring(L, value); lua\_setfield(L, -2, "parameterName"); . . CoronaLuaDispatchEvent(L, listener, 0);

[End::Code in Objective-C]

Example:

in Lua

function myLuaFunction()

    …

end

Runtime:addEventListener( “myLuaFunction”, listener )

===========================

in Objective C

@implementation MyCoronaDelegate
 

id<CoronaRuntime> runtimeObj;

  • (void)didLoadMain:(id<CoronaRuntime>)runtime
    {

    runtimeObj = runtime;
    lua_State *L = runtime.L;
}

  • (void)toCallLuaFunction

{

const char kNameKey[] = “name”;
const char kValueKey[] = “myLuaFunction”;
lua_newtable( L );
lua_pushstring( L, kValueKey ); // All events are Lua tables
lua_setfield( L, -2, kNameKey ); // that have a ‘name’ property

Corona::Lua::RuntimeDispatchEvent( L, -1 );

}

@end

But I don’t know how to pass parameter to Lua function from ObjectiveC.

I just store parameter in ObjC, then ask Lua to get back this parameter.

Thanks, sayhong. I solved this problem already.

Solved:

Include CoronaLua.h & CoronaRuntime.h in Objective-C

[Begin::Code in Objective-C]

CoronaLuaNewEvent( L, "methodName"); //.... Parameters .....// lua\_pushnumber(L, value); lua\_setfield(L, -2, "parameterName"); lua\_pushinteger(L, value); lua\_setfield(L, -2, "parameterName"); lua\_pushstring(L, value); lua\_setfield(L, -2, "parameterName"); . . CoronaLuaDispatchEvent(L, listener, 0);

[End::Code in Objective-C]

Hi Rkv,

I have a similar type of requirement.  I tried your solution but I am still stuck… I have a .m file in which image comparison takes place. I want to pass the results (“image name” ) back to Lua. Can you please tell me the series of steps which I should follow to implement this ?

Thanks in advance

I can pass string or int or double, do you want pass string to lua?

I implement a generic function, return string or number. But you have to add other data to return.

// OBJECTIVE C [code] ===[call]===\> LUA[Function] // Parameters: // name NSString lua function name // key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NSArray:[NSString] // values&nbsp;&nbsp; NSArray:[NSString or NSNumber(int or double)] // if you sent data to lua, it's possible. But you could implement. // Sample: // [self callbackLuaFunction:@"getImageName" //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key:[NSArray arrayWithObjects:@"name"] // &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value:[NSArray arrayWithObjects:@"superman.png"]]; - (void)callbackLuaFunction:(NSString \*)name key:(NSArray \*)keys value:(NSArray \*)values { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;CoronaLuaNewEvent( \_L, [name UTF8String] ); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if (keys != nil || values != nil) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for (int i=0; i \< [keys count]; i++) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;id val = [values objectAtIndex:i]; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if ([[values objectAtIndex:i] isKindOfClass:[NSNumber class]]) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;NSNumber \*n = (NSNumber \*)val; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (strcmp([n objCType], @encode(double)) == 0) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lua\_pushnumber(\_L, n.doubleValue); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} else { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lua\_pushinteger( \_L, n.intValue); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} else if ([[values objectAtIndex:i] isKindOfClass:[NSString class]]) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;NSString \*s = (NSString \*)val; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lua\_pushstring( \_L, [s UTF8String] ); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else if ([[values objectAtIndex:i] isKindOfClass:[NSData class]]) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Implement...... sent other type data &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lua\_setfield( \_L, -2, [(NSString \*)[keys objectAtIndex:i] UTF8String]); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;CoronaLuaDispatchEvent( \_L, \_fListener, 0 ); }

Hi Rkv,

I have a similar type of requirement.  I tried your solution but I am still stuck… I have a .m file in which image comparison takes place. I want to pass the results (“image name” ) back to Lua. Can you please tell me the series of steps which I should follow to implement this ?

Thanks in advance

I can pass string or int or double, do you want pass string to lua?

I implement a generic function, return string or number. But you have to add other data to return.

// OBJECTIVE C [code] ===[call]===\> LUA[Function] // Parameters: // name NSString lua function name // key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NSArray:[NSString] // values&nbsp;&nbsp; NSArray:[NSString or NSNumber(int or double)] // if you sent data to lua, it's possible. But you could implement. // Sample: // [self callbackLuaFunction:@"getImageName" //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key:[NSArray arrayWithObjects:@"name"] // &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value:[NSArray arrayWithObjects:@"superman.png"]]; - (void)callbackLuaFunction:(NSString \*)name key:(NSArray \*)keys value:(NSArray \*)values { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;CoronaLuaNewEvent( \_L, [name UTF8String] ); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if (keys != nil || values != nil) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for (int i=0; i \< [keys count]; i++) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;id val = [values objectAtIndex:i]; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if ([[values objectAtIndex:i] isKindOfClass:[NSNumber class]]) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;NSNumber \*n = (NSNumber \*)val; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (strcmp([n objCType], @encode(double)) == 0) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lua\_pushnumber(\_L, n.doubleValue); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} else { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lua\_pushinteger( \_L, n.intValue); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} else if ([[values objectAtIndex:i] isKindOfClass:[NSString class]]) { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;NSString \*s = (NSString \*)val; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lua\_pushstring( \_L, [s UTF8String] ); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else if ([[values objectAtIndex:i] isKindOfClass:[NSData class]]) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Implement...... sent other type data &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lua\_setfield( \_L, -2, [(NSString \*)[keys objectAtIndex:i] UTF8String]); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;CoronaLuaDispatchEvent( \_L, \_fListener, 0 ); }