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:

ActionScript
< view plain text >
  1. /**
  2.  * Adds a new function to the list of available socket function calls.
  3.  *
  4.  * @param   sFName          Socket function call string reference name.
  5.  * @param   funcReference   A Function reference to the actual function.
  6.  * @param   thisObject      A reference to the object that should be the "this" object when this function is executed.
  7.  *
  8.  * @usage   RegisteredSocketFunctions.registerFunction("myTestFunction", myTestFunction, this);
  9.  */
  10. public static function registerFunction(sFName:String, funcReference:Function, thisObject:Object):void
ActionScript
< view plain text >
  1. /**
  2.  * Removes a function from the list of registered socket function calls.
  3.  *
  4.  * @param   sFName  The socket function call that should be removed.
  5.  *
  6.  * @usage   RegisteredSocketFunctions.unregisterFunction("myTestFunction");
  7.  */
  8. public static function unregisterFunction(sFName:String):void
ActionScript
< view plain text >
  1. /**
  2.  * Executes a Function that was previously registered using <code>registerFunction</code> with the name <code>sFName</code>.
  3.  *
  4.  * @param   sFName      The string reference name of the function that is to be executed.
  5.  * @param   argArray    The optional list of parameters that should be executed with the function.
  6.  * @param   thisObject  The optional object to which the Function referenced by sFName is applied. The "this" operator for the executed function will be <code>thisObject</code> if <code>thisObject</code> is non-null; otherwise, it will be the <code>thisObject</code> specified when registerFunction was called.
  7.  *
  8.  * @return  Returns whatever value (if any) is returned by the function being called.
  9.  *
  10.  * @usage   RegisteredSocketFunctions.registerFunction("myTestFunction", [var1, var2]);
  11.  */
  12.  public static function executeFunction(sFName:String, argArray:Array = null, thisObject:Object = null):*

Example:

Let’s say you have the following class

ActionScript
< view plain text >
  1. package
  2. {
  3.     import com.natejc.utils.communication.RegisteredSocketFunctions;
  4.    
  5.     public class RSFTestClass
  6.     {
  7.         private var _sName:String;
  8.         private var _nAge:uint;
  9.        
  10.        
  11.         public function TestClass()
  12.         {
  13.             RegisteredSocketFunctions.registerFunction("testFunc", testFunc, this);
  14.         } // END CONSTRUCTOR
  15.  
  16.        
  17.         public function testFunc(sName:String, nAge:uint):String
  18.         {
  19.             this._sName = sName;
  20.             this._nAge = nAge;
  21.            
  22.             trace("Hey " + _sName + ", I can't believe you're " + String(_nAge) + " years old!";
  23.             return sName + String(nAge);
  24.         } // END FUNCTION testFunc
  25.  
  26.     } // END CLASS TestClass
  27.    
  28. } // END PACKAGE

Then any other class in any other function could execute testFunc by doing the following:

ActionScript
< view plain text >
  1. var s:String = RegisteredSocketFunctions.executeFunction("testFunc", ["Joe Mama", 90]);
  2. trace(s);
  3. // which traces:
  4. //   Hey Joe Mama, I can't believe you're 90 years old!
  5. //   Joe Mama90

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.