- Home /
Javascript Inheritance; how to not override functions?
Hey fellas, quick question. I have a class called Weapon...
// Weapon.js
class Weapon extends MonoBehavior
{
function Awake()
{
print( "This is a weapon..." );
}
}
...and I have a class called Ballistic Weapon...
// Ballistic_Weapon.js
class Ballistic_Weapon extends Weapon
{
function Awake()
{
print( "...that shoots projectiles!" );
}
}
...and what I want is for both Awake() functions to be called, so the result of a Ballistic_Weapon script would be:
"This is a weapon..."
"...that shoots projectiles!"
But as it stands, all I get is:
"...that shoots projectiles!"
...so it would seem my Weapon_Ballistic's Awake() function is overriding the Awake() function of the Weapon class. I searched around and everyone is saying to define Awake() as 'virtual', but when I do that I get a warning that says that 'virtual' has been deprecated, as all functions are now 'virtual'... but Weapon_Ballistic's Awake() function is still overriding Weapon's Awake() function.
Whudoido?
Thanks fellas. <3
Answer by gregzo · Jul 29, 2013 at 07:01 AM
Hi.
It seems you want to call the base class's Awake in the derived class's Awake :
class Ballistic_Weapon extends Weapon
{
function Awake()
{
super.Awake (); //or base.Awake in c#
print( "...that shoots projectiles!" );
}
}
Take a look at this other thread on the subject.
Great, precisely what I needed. Thank you Gregzo!