Search Unity

Overridable functions

Discussion in 'Scripting' started by Andre Gregoire, Aug 31, 2009.

  1. Andre Gregoire

    Andre Gregoire

    Joined:
    Aug 28, 2009
    Posts:
    32
    Why Update or Start functions in C# class scripts that are chidren of MonoBehaviour ( of course) can't be overrided (keyword override C#), when the functions are declared as overridable?
    Is it automatically overided like in C++ when you declare/implement a function that is overridable?
     
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    hi
    there is a really good book about C# for programmers "programming C# 3" by jessie liberty publisher: oreilly

    this is implicitly done in unity. i am not sure but i
     
  3. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Unity doesn't use virtual functions to implement the callback system for MonoBehaviours. It uses reflection to see which functions are implemented and will only perform callbacks on those functions it can find.

    This is also why your Start function gets called even if it is defined as private.
     
  4. remz

    remz

    Joined:
    Apr 21, 2009
    Posts:
    16
    if you still do want to have such behaviour (which I would expect too btw) in your scripts you could achieve this prety easily with a little level of indirection in your scripts. something along those lines :

    Code (csharp):
    1.  
    2.                 public class VirtualBehaviour : MonoBehaviour {
    3.                    
    4.                     void Update(){
    5.                         doUpdate();
    6.                     }
    7.                     protected virtual void DoUpdate(){}
    8.                    
    9.                     void Wake(){
    10.                         DoWake();
    11.                     }
    12.                     protected virtual void DoWake(){}
    13.                    
    14.                     void Start(){
    15.                         DoStart();
    16.                     }
    17.                     protected virtual void DoStart(){}
    18.                 }
    19.  
     
  5. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    The reason Unity uses reflection over virtual functions, is that with the latter it has to call Update, LateUpdate, FixedUpdate and all the other callback on every single MonoBehaviour every frame (or at whatever frequency the function is supposed to be called). For large projects with large numbers of objects, that may result in significant performance overhead as most MonoBehaviours don't need every possible callback. In fact, in most scripts I write don't implement more than 3 or 4 of the close to 50 possible MonoBehaviour callbacks.

    I wouldn't recommend working against Unity by forcing everything to be virtual. There is no advantage to doing that, only drawbacks.
     
  6. remz

    remz

    Joined:
    Apr 21, 2009
    Posts:
    16
    thanks for the insight. I didn't know what was unity's reason to use reflexion. It's good to know.
     
  7. Andre Gregoire

    Andre Gregoire

    Joined:
    Aug 28, 2009
    Posts:
    32
    Thx alot guys, it's good to understand the mechanics!