Now, I know I should probably just switch over to coding in C#, but as it happens i’ve got a rather large series of js scripts written here, attempting to use a lil’ class inheritance, and it doesn’t seem to be functioning properly. here’s what i’ve done:
script 1:
class TabObject{
function DrawMe(){
Debug.Log("This should not happen");
}
}
class TabObjectComplex extends TabObject{
function TabOjbectComplex(){
}
function DrawMe(){
Debug.Log("this should happen");
}
}
script 2 (the actual script that runs):
function Start(){
var obj : TabObject;
obj = new TabOjectComplex();
obj.DrawMe();
}
of course, i get “This should not happen” in the debug window.
The code complies fine, and when i check my obj variable with a Debug.Log(obj) it shows it as being of type TabObjectComplex – so it’s sort of HALF working…
am I doing something wrong, or is this type of thing just not possible?
thats likely the dynamic typing - dynamic type inference you see there.
unityscript can tell at runtime that it is of the extended class not base class
hmm interesting. so is that to say that unity can tell what i’m trying to do, but the language doesn’t have the functionality to actually support it? i’m not sure if i understand.
any way to properly implement something like this?
My guess is that Unity does the best job it can to convert the dynamic JavaScript language to an underyling statically typed language, whereas C# already being statically typed compiles perfectly?
Well I just ran into the same boat. I would like polymorphism in javascript!
Here’s my “Hacked” version:
class Turtle
{
function Turtle()
{
}
function move()
{
Debug.Log("TURTLE!");
}
};
class Goomba
{
function Goomba()
{
}
function move()
{
Debug.Log("GOOMBA!");
}
};
function Start()
{
var x : int = 0;
var myMonsters = new Array();
var myTurtle : Turtle = new Turtle();
var myGoomba : Goomba = new Goomba();
myMonsters.Push(myTurtle);
myMonsters.Push(myGoomba);
for(x = 0; x < myMonsters.length; x++)
{
myMonsters[x].move();
}
}
//Outputs:
//TURTLE!
//GOOMBA!
I just wrote this up and tested it. Not very graceful, but it works!