Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why aren't UnityEngine.UIElements.*Event type ids constants?

Discussion in 'UI Toolkit' started by dyamanoha_, Jun 16, 2022.

  1. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    78
    Code (CSharp):
    1. protected override void ExecuteDefaultActionAtTarget(EventBase evt)
    2.         {
    3.             switch (evt.eventTypeId)
    4.             {
    5.                 case MouseEnterEvent.TypeId():
    6.                     {
    7.                         break;
    8.                     }
    9.             }
    10.             // Note: This can't be turned into a switch statement; Attempting to declare a case
    11.             // on *Event.TypeId() results in a compilation error. It complains that the 'type
    12.             // TypeId doesn't exist'. I assume the problem is that you need to switch on a constant
    13.             // or an expression that evaluates to a constant. Not sure why the error indicates a
    14.             // missing type problem.
    15.             if (evt.eventTypeId == MouseEnterEvent.TypeId())
    16.             {
    17.                 OnMouseEnter((MouseEnterEvent)evt);
    18.             }
    I have two questions about the above code snippet.
    1. The switch block doesn't compile (see the note). Can someone help me understand what the error is saying?
    2. The documentation encourages default action overrides rather than events for performance reasons. That makes sense. What was the reasoning behind the design choice to not make event type ids constants? Rather than being able to jump to handlers, a bunch of conditionals need to be evaluated at runtime?
     
  2. BillyWM

    BillyWM

    Joined:
    Dec 29, 2018
    Posts:
    14
    Like your comment speculates, it's because you're trying to use a runtime expression in your case statement. There are a few kinds of expressions you can switch over (including types) and it looks like the compiler just spit out a warning related to one of them among the possibilities, as compilers are wont to do.
     
  3. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    78
    Ah okay, I guess I didn't realize you could switch on a types. Makes sense.