rkv
May 23, 2014, 5:28am
1
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;
}
{
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.
rkv
June 6, 2014, 1:50am
4
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;
}
{
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.
rkv
June 6, 2014, 1:50am
7
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]
altafr
March 4, 2015, 7:08am
8
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
rkv
March 5, 2015, 12:00am
9
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 NSArray:[NSString] // values NSArray:[NSString or NSNumber(int or double)] // if you sent data to lua, it's possible. But you could implement. // Sample: // [self callbackLuaFunction:@"getImageName" // key:[NSArray arrayWithObjects:@"name"] // value:[NSArray arrayWithObjects:@"superman.png"]]; - (void)callbackLuaFunction:(NSString \*)name key:(NSArray \*)keys value:(NSArray \*)values { CoronaLuaNewEvent( \_L, [name UTF8String] ); if (keys != nil || values != nil) { for (int i=0; i \< [keys count]; i++) { id val = [values objectAtIndex:i]; if ([[values objectAtIndex:i] isKindOfClass:[NSNumber class]]) { NSNumber \*n = (NSNumber \*)val; if (strcmp([n objCType], @encode(double)) == 0) { lua\_pushnumber(\_L, n.doubleValue); } else { lua\_pushinteger( \_L, n.intValue); } } else if ([[values objectAtIndex:i] isKindOfClass:[NSString class]]) { NSString \*s = (NSString \*)val; lua\_pushstring( \_L, [s UTF8String] ); } else if ([[values objectAtIndex:i] isKindOfClass:[NSData class]]) { // Implement...... sent other type data } lua\_setfield( \_L, -2, [(NSString \*)[keys objectAtIndex:i] UTF8String]); } } CoronaLuaDispatchEvent( \_L, \_fListener, 0 ); }
altafr
March 4, 2015, 7:08am
10
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
rkv
March 5, 2015, 12:00am
11
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 NSArray:[NSString] // values NSArray:[NSString or NSNumber(int or double)] // if you sent data to lua, it's possible. But you could implement. // Sample: // [self callbackLuaFunction:@"getImageName" // key:[NSArray arrayWithObjects:@"name"] // value:[NSArray arrayWithObjects:@"superman.png"]]; - (void)callbackLuaFunction:(NSString \*)name key:(NSArray \*)keys value:(NSArray \*)values { CoronaLuaNewEvent( \_L, [name UTF8String] ); if (keys != nil || values != nil) { for (int i=0; i \< [keys count]; i++) { id val = [values objectAtIndex:i]; if ([[values objectAtIndex:i] isKindOfClass:[NSNumber class]]) { NSNumber \*n = (NSNumber \*)val; if (strcmp([n objCType], @encode(double)) == 0) { lua\_pushnumber(\_L, n.doubleValue); } else { lua\_pushinteger( \_L, n.intValue); } } else if ([[values objectAtIndex:i] isKindOfClass:[NSString class]]) { NSString \*s = (NSString \*)val; lua\_pushstring( \_L, [s UTF8String] ); } else if ([[values objectAtIndex:i] isKindOfClass:[NSData class]]) { // Implement...... sent other type data } lua\_setfield( \_L, -2, [(NSString \*)[keys objectAtIndex:i] UTF8String]); } } CoronaLuaDispatchEvent( \_L, \_fListener, 0 ); }