Search Unity

Virtual and Override Functions

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

Thread Status:
Not open for further replies.
  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,741
    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
    Ryiah, akshatja8n and Kiwasi like this.
  3. Todd-Wasson

    Todd-Wasson

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

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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.
     
    Ryiah and Daniilbata12 like this.
  5. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    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
    marspot, Oelson, BlocCole and 21 others like this.
  7. mdeglobal

    mdeglobal

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

    nd_nomad

    Joined:
    Nov 18, 2023
    Posts:
    4
    Thank you for this Bankler! Still helpful for a newb 9 years later. :)
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    In the future, please use the Like button to show appreciation rather than necroing threads. This is from 2015!

    Thanks.
     
    Ryiah and Bunny83 like this.
Thread Status:
Not open for further replies.