Name
PerformSelector -- perform selector (V7.0)
Synopsis
[ret] = PerformSelector(s$[, ...])
Platforms
iOS only

Function
This function can be used to perform a selector of your application's delegate, i.e. it allows you to make calls from your Hollywood script into native code. The name of the selector has to be passed as a string in s$.

The selector specified by s$ will then be run with an NSMutableArray as its sole argument. Inside that array, index 1 will contain the lua_State and index 2 will contain a pointer to a hwPluginAPI structure, allowing you to access all public APIs and especially the Lua VM. Indices 3 and 4 contain the UIViewController and UIView, respectively. Upon return, you must set index 0 to an NSValue containing an int which specifies the return code of your function. This is all very similar to the way functions in Hollywood plugins are executed. So please see the Hollywood SDK documentation for more details (especially the chapters concerning writing library plugins).

You need to implement the desired selector in your application's delegate in native code. This is what a custom selector might look like in Objective C:

 
- (void)MyTestSelector:(NSMutableArray *) args
{
   // get essential pointers from Hollywood
   lua_State *L = (lua_State *)
       [((NSValue *) [args objectAtIndex:1]) pointerValue];
   hwPluginAPI *hwcl = (hwPluginAPI *)
       [((NSValue *) [args objectAtIndex:2]) pointerValue];

   // we return 1 because we push one string
   int retval = 1;

   // print string at stack index 2
   printf("%s\n", hwcl->LuaBase->luaL_checklstring(L, 2, NULL));

   // push return value
   hwcl->LuaBase->lua_pushstring(L, "Test return value");

   // set return value
   [args replaceObjectAtIndex:0 withObject:[NSValue value:&retval
       withObjCType:@encode(int*)]];
}

This selector will print the string argument that is passed to the PerformSelector() call in argument 2. It will then return the string "Test return value" to the Hollywood script. You could run this selector from your Hollywood script like this:

 
DebugPrint(PerformSelector("MyTestSelector", "Test"))

This code will pass the string "Test" to the MyTestSelector method. DebugPrint() will print "Test return value" because that is the return value of MyTestSelector.

Keep in mind that your selector function will not be run on the main (UI) thread but on Hollywood's VM thread. So when accessing UIKit functionality (or other frameworks that need to run on the main thread) you need to delegate the respective code to the main thread first.

Inputs
s$
name of selector to run
Results
ret
optional: return values of your selector function

Show TOC