Search Unity

Trouble casting between classes in Javascript

Discussion in 'Scripting' started by littlelingo, Dec 26, 2009.

  1. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    I am working on building an event structure and want to create custom events that implement a single base class. Then cast these in my handling function.

    My base class looks like this:

    Code (csharp):
    1. #pragma strict
    2.  
    3. class CustomEvent {
    4.    
    5.     public var type:String;
    6.     public var callbackFunction:Function;
    7.     public var weakReference:boolean = true;
    8.  
    9.     public function Event(){};
    10.    
    11. }
    My class that extends look like this:

    Code (csharp):
    1. #pragma strict
    2.  
    3. class LoadEvent extends CustomEvent {
    4.    
    5.     public var levelName:String;
    6.    
    7.     public function LoadEvent(callback:Function){
    8.         setType();
    9.         callbackFunction = callback;
    10.     }
    11.    
    12.     public function LoadEvent(lvlName:String){
    13.         setType();
    14.         levelName = lvlName;
    15.     }
    16.    
    17.     public function LoadEvent(lvlName:String, weakRef:boolean){
    18.         setType();
    19.         levelName = lvlName;
    20.         weakReference = weakRef;
    21.     }
    22.    
    23.     private function setType():void {
    24.         type = "LoadEvent";
    25.     }
    26.    
    27. }
    And my signature for the handling function looks like this:

    Code (csharp):
    1. public function broadcastEvent(listenerType:String, event:CustomEvent):void {}

    Instead of having to have a different signature for every event type I may have. I would like to pass the CustomEvent class and then cast "dynamically" based on the type of the event that is passed in. So for this example, the type is "LoadEvent" so I would want to cast the event in the handling function to LoadEvent.

    My initial thought here was to have an interface and cast to that but I couldn't get that to work as I was getting an error in Javascript when attempting to create the interface like this:

    Code (csharp):
    1. interface ICustomEvent {
    2.      //Stuff goes here
    3. }
    Any insight or help here would be greatly appreciated. Thanks in advance!
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Don't see a problem with the code you posted. No need to have interfaces, you can pass them in as CustomEvent and then cast them up to whatever they are.
     
  3. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    I guess the main issue I am having is how to cast from the string. I know normally I could do something like this:

    Code (csharp):
    1. var newEvent:LoadEvent = evt as LoadEvent;
    But what I am hoping to do is cast from the type property of the event being passed in (which is a string). Something like this:

    Code (csharp):
    1. var newEvent = evt as evt.type;
    or something along those lines where I use the string type and can eval that or something to the type I want to cast to.

    My goal is to not have to explicitly have a signature for every event that could get dispersed as well as not have to have some sort of hard look-up. Any thoughts? Thanks in advance!
     
  4. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    :oops:

    Okay, I got it. Sorry for the wasted bandwidth here. :)