Now that you know how to dynamically cast variables at runtime, we can create a very cool static class I dub RegisteredSocketFunctions. The purpose of this class is to allow any function to be “registered,” which, by doing so, allows global access to the function directly.

The goals of the RegisteredSocketFunctions class:

  • Any function in any class can be very easily “registered.”
  • Once registered, any function in any class can be executed, maintaining all parameter functionality.
  • Any implied or direct usage of the “this” expression in the executed function will be interpreted properly.

Here’s the API:
[as]
/**
* Adds a new function to the list of available socket function calls.
*
* @param sFName Socket function call string reference name.
* @param funcReference A Function reference to the actual function.
* @param thisObject A reference to the object that should be the “this” object when this function is executed.
*
* @usage RegisteredSocketFunctions.registerFunction(“myTestFunction”, myTestFunction, this);
*/
public static function registerFunction(sFName:String, funcReference:Function, thisObject:Object):void
[/as]

[as]
/**
* Removes a function from the list of registered socket function calls.
*
* @param sFName The socket function call that should be removed.
*
* @usage RegisteredSocketFunctions.unregisterFunction(“myTestFunction”);
*/
public static function unregisterFunction(sFName:String):void
[/as]

[as]
/**
* Executes a Function that was previously registered using registerFunction with the name sFName.
*
* @param sFName The string reference name of the function that is to be executed.
* @param argArray The optional list of parameters that should be executed with the function.
* @param thisObject The optional object to which the Function referenced by sFName is applied. The “this” operator for the executed function will be thisObject if thisObject is non-null; otherwise, it will be the thisObject specified when registerFunction was called.
*
* @return Returns whatever value (if any) is returned by the function being called.
*
* @usage RegisteredSocketFunctions.registerFunction(“myTestFunction”, [var1, var2]);
*/
public static function executeFunction(sFName:String, argArray:Array = null, thisObject:Object = null):*
[/as]

Example:

Let’s say you have the following class
[as]
package
{
import com.natejc.utils.communication.RegisteredSocketFunctions;

public class RSFTestClass
{
private var _sName:String;
private var _nAge:uint;

public function TestClass()
{
RegisteredSocketFunctions.registerFunction(“testFunc”, testFunc, this);
} // END CONSTRUCTOR

public function testFunc(sName:String, nAge:uint):String
{
this._sName = sName;
this._nAge = nAge;

trace(“Hey ” + _sName + “, I can’t believe you’re ” + String(_nAge) + ” years old!”;
return sName + String(nAge);
} // END FUNCTION testFunc

} // END CLASS TestClass

} // END PACKAGE
[/as]

Then any other class in any other function could execute testFunc by doing the following:
[as]
var s:String = RegisteredSocketFunctions.executeFunction(“testFunc”, ["Joe Mama", 90]);
trace(s);
// which traces:
// Hey Joe Mama, I can’t believe you’re 90 years old!
// Joe Mama90
[/as]

Caution:

This class is intended to allow execution of functions across a network (which we will be discussing further later). Although this may seem like a cool, time saving way to allow classes all over your code interact with each other, doing so is definitely poor coding practice. Just like in real life, a child should never have command over its parent ;) . Having said that, if your project is due in 1 hour and you don’t have time to write all of the event handling that you should write, well, congratulations; you just found a class that will encourage your bad habits…

Download: RegisteredSocketFunctions.as

This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 3.0 United States License.