Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

typeof vs String ??

Discussion in 'Scripting' started by Bunzaga, Mar 1, 2013.

  1. Bunzaga


    Jan 9, 2009
    I am working on some game systems, such as event handling and a component - entity system, and I was wondering, what would be any disadvantages over using a System.type as Keys and 'parsing' it with typeof vs using a String identifier for the various components.

    I am using JavaScript, so I am not going to be using delegates, and my target platform is Android and IOS.

    For example, most component systems I have seen will pas a String identifier or name for each individual component, and then use a Dictionary to get the component based on the String passed in. What if I bypassed that, and instead just passed in the class and used typeof(class) instead?

    Take these two examples...
    Code (csharp):
    2. //pragma,  imports... etc
    4. private var cList.Dictionary<String, Component> = new Dictionary.<String, Component>();
    5. public function GetComponent( c:String ){
    6.   if(cList.ContainsKey(c)){
    7.     return cList[c];
    8.   }
    9. }
    Code (csharp):
    2. private var cList.Dictionary<Type, Component> = new Dictionary.<Type, Component>();
    3. public function GetComponent( c:Component ){
    4.   if(cList.ContainsKey(typeof(c))){
    5.     return cList[typeof(c)];
    6.   }
    7. }
    Is typeof slower than comparing Strings? Anyone have any advice or opinions about this?
  2. trooper


    Aug 21, 2009
    I think you'll find typeOf is faster.

    I just finished our entity system where every time the scene is saved it looks at the resources folder and finds all the prefabs, then it generates a code file using the the parent folder of the prefab (I wanted to make it find all folders and structure the code file accordingly but time vs effort prevailed).

    Code (csharp):
    2. public class EntitiesObjects{
    3.     public EntityInfo Garage = new EntityInfo("Garage","Objects/Garage");
    4. }
    5. public class EntitiesVehicles
    6. {
    7.     public EntityInfo ElitePrestigeR = new EntityInfo("ElitePrestigeR","Vehicles/ElitePrestigeR");
    8.     public EntityInfo ElitePrestigeR1 = new EntityInfo("ElitePrestigeR1","Vehicles/ElitePrestigeR1");
    9. }
    10. public partial class Entities
    11. {
    12. public static EntitiesObjects Objects = new EntitiesObjects();
    13. public static EntitiesVehicles Vehicles = new EntitiesVehicles();
    14. }
    In my generator I have to pass through a "EntityInfo" instead of a string.

    Code (csharp):
    1. Vehicle.Create(Entities.Vehicles.ElitePrestigeR);
  3. Bunzaga


    Jan 9, 2009
    I actually ran into a situation where a string would be better than a class type... For example if I wanted the event to be unique to a specific object instance. By using typeof, I am locking myself into that ONE event situation. Case in point:

    I have 4 doors, and I want to raise an event for when the user taps a door. If I use typeof and the 'DoorWasTapped' event, each door would have to register to listen for the event, and see if it was them, then act accordingly. On the other hand if I used a string, each door could register to listen for the '"_DoorWasTapped" event. When the door is tapped, the unique event ID is raised and only one door is effected.

    I thought about having a 'DoorSystem' intercept the event, and then pass it to the door in question using its own local dictionary, but eh. Any ideas on how to get around something like this?