Hi, i have problems over classes here.
What I am doing now is I have a base class(Character), and how can I do for the choosing of classes?. and how do i call the variable class when I have chosen my class.
Thanks in advance!
class Character
{
var hero : GameObject;
var classNum : int;
function Character()
{
}
}
class Warrior
{
function Warrior()
{
}
function WarriorMeleeAttack()
{
//show warrior animation+particle
}
}
class Archer
{
function Archer()
{
}
function ArcherMeleeAttack()
{
//show archer animation+particle
}
}
function Start()
{
if(Character.classNum ==1)
{
//warrior?
}
else if(Character.classNum ==2)
{
//archer?
}
}
Hello!
Two things:
One of the design goals of object-oriented design is that you shouldn’t care which child class you have (at least 99% of the time). You should just perform actions (call methods) and let the classes figure out which version to call.
The problem with your design is you have each class using a different attack function name therefore you’re just using classes as a namespace and some data.
Think about doing something like this. Note I’m not familiar with JavaScript/UnityScript so this is pseudo-code. If JS has issues with class inheritence like this then ignore 
class Character
{
var object: GameObject;
function Character() {...}
function MeleeAttack(victim : Character)
{
// override in child class
}
}
class WarriorCharacter : Character
{
function WarriorCharacter() {}
function MeleeAttack(victim : Character)
{
Debug.Log("Warrior is attacking " + victim + "!");
}
}
class WizardCharacter : Character
{
function WizardCharacter() {}
function MeleeAttack(victim : Character)
{
Debug.Log("Wizard is attacking " + victim + "!");
}
}
// using
someCharacter.MeleeAttack(anotherChar);
Note that we don’t know what type of Character someCharacter is, as long as he’s a Character, the function will work and it will perform the right attack.
The second part:
Sometimes you still need a way to know what type of subclass something is (but hopefully this is very rare if you’ve designed your classes well). There are ways to do this. One way is try to cast it to the type:
if((WizardCharacter)someCharacter)
Another is to use type() if your language has it:
if type(someCharacter) == WizardCharacter
Another way is like you mentioned in your example which can work.
Again, I don’t know which one of those works well in Javascript or that language’s features but the concepts should still apply.
Hope that helps!
Timmer’s points are all exceptionally useful in terms of OO programming. Here are some code corrections for Unity’s brand of Javascript syntax:
class Character
{
var object : GameObject;
function Character() {...}
virtual function MeleeAttack(victim : Character)
{
// override in child class
}
}
class Warrior extends Character
{
function Warrior () { }
function MeleeAttack(victim : Character)
{
Debug.Log("Warrior is attacking " + victim + "!");
}
}
Note in particular the use of “virtual” in the base class’ MeleeAttack signature, and “extends Character” in the subclass’ class definition.
Thanks for giving me some classes tips.