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

Virtual and Override Functions

Discussion in 'Scripting' started by TheImperial, Mar 30, 2015.

  1. TheImperial

    TheImperial

    Joined:
    Mar 17, 2015
    Posts:
    72
    what's the concept of the virtual and override Function, what's their Features ???

    Code (CSharp):
    1. public virtual int TakeDamage(int damage)
    2. {
    3.     return HitPoints - damage;
    4. }
    5.  
    6. public override int TakeDamage(int damage)
    7. {
    8.     return HitPoints - damage;
    9. }
    10.  
    11.  
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    They are for inheritance and polymorphism. If it is virtual you can override it in a class that derives from your main class.

    Override just means you are overriding a virtual method or implmenating a abstract method.
     
    Last edited: Mar 30, 2015
    akshatja8n and Kiwasi like this.
  3. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    Something tells me that explanation won't be sufficient. ;)
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Well the information won't matter to him tell he learns about polymorphism, and there is tons of information on the web about it since it is a general object oriented programming concept.
     
    Daniilbata12 likes this.
  5. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    I didn't mean to criticize your answer, it was a good one no doubt. I just had an image of somebody reading that and throwing their hands in the air having no idea what it meant. That made me chuckle. :)
     
    Daniilbata12 likes this.
  6. Bankler

    Bankler

    Joined:
    Apr 10, 2012
    Posts:
    66
    Let's say you have two kinds of enemies. One standard enemy (called Enemy) and one really badass enemy (called Badass). They should share almost everything, with the exception that Badass will laugh at you when you shoot him instead of actually taking damage. Then it might be a possible approach (not the only one, but ONE appoach) to have the Badass derive from Enemy (this concept is called inheritence).

    In the Enemy class, you should mark the functions that can be overriden by any subclass as virtual. In the subclass, mark any functions that are overriding as override.

    Code (CSharp):
    1.     class Enemy
    2.     {
    3.       public virtual int HitByBullet(int damage)
    4.       {
    5.         return HitPoints - damage;
    6.       }
    7.     }
    Code (CSharp):
    1.     class Badass : Enemy
    2.     {
    3.       public override int HitByBullet(int damage)
    4.       {
    5.        audio.PlayOneShot(m_laughSfx);
    6.       }
    7.     }
    Overcourse (well, not really): If you want mr Badass to laugh AND take damage, a good way is to call the base function from the overriding function.

    Code (CSharp):
    1. public override int HitByBullet(int damage)
    2. {
    3.    audio.PlayOneShot(m_laughSfx);
    4.    return base.HitByBullet(damage);
    5. }
    Hope this may have helped a little. There are tons of info regarding inheritence in C# on the internet as well if you want to dig deep. Good luck!
     
    Last edited: Mar 30, 2015
  7. mdeglobal

    mdeglobal

    Joined:
    Feb 22, 2015
    Posts:
    6
    Good response, Bankler! ;)
     
    Daniilbata12 and SRMUFA07 like this.