Superclass and Subclass in Unity Javascript

Hi,

Could someone please explain how to make and manipulate Superclasses and Subclasses in Unity Javascript?

What I want to do is:

I have a game that has multiple waves of enemies per level. When all the enemies of Wave1 are killed, the game switches to Wave2 and changes many parameters.

I started by making two classes, Wave1Values and Wave2Values. Then I have a variable waveIs that should change from Wave1Values to Wave2Values when the WaveSwitch() happens. But of course, it can’t be changed because that would mean it changes it’s class which is not possible. Example:

class Wave1Values{ // this shows conveniently in the inspector

var maxEnemies = 4;
var minTime : float = 1.0;
var maxTime : float = 5.0;
var frequency : float = 1;
var defaultMinTime : float = 2;

}

var wave1 : Wave1Values = Wave1Values();

class Wave1Values{ // this also shows in the inspector :slight_smile:

var maxEnemies = 8;
var minTime : float = 3.0;
var maxTime : float = 6.5;
var frequency : float = 2;
var defaultMinTime : float = 3;

}

var wave1 : Wave1Values = Wave1Values();

// then I have Start(), Update() and all sorts of stuff

//and somewhere during the game this function gets called:

function SwitchWaves() {

var waveIs : Wave1Values = wave1;

switch (waveNo){

    case (1):
		waveIs = wave1;
	break;
	
    case (2):
		waveIs = wave2;
	break;
}

//I actually tried to do this by making an array of WaveValues 
//and getting them directly by number
//and not by switch, but it was problematic.
//like an array of [wave1, wave2, wave3 ... etc]
//and then get values by waveIs = array[waveNo];
//would that be possible if they are all different subclasses of one superclass?
	
maxEnemies = waveIs.maxEnemies;
minTime = waveIs.minTime;
maxTime = waveIs.maxTime;
frequency = waveIs.frequency;
defaultMinTime = waveIs.defaultMinTime;

}

But as I said, this won’t work since waveIs is already of Wave1Values class :frowning:

So I thought of making a superclass WaveValues and make Wave1Values and Wave2Values it’s subclasses. But I don’t know how to do it despite searching the web and finding this: How to reassign a variable's class - Questions & Answers - Unity Discussions

That answer looks nice but it won’t work when I put it in my code. I get some errors about same names, ambiguous stuff etc. I tried a lot of variations. The only thing that I haven’t tried is to put the code with subclasses and superclass in another script that isn’t attached to any object in the game but that would be inconvenient because it wouldn’t show in the inspector. It would greatly help if someone could solve this so anyone who wants superclassing in Unity Javascript has a working example available at Unity Answers.

Thanx.

Well if you’re looking for javascript and class extensions you could do:

#pragma strict


class WaveBase {

	var maxEnemies : int;
	var minTime : float;
	var maxTime : float;
	var frequency : float;
	var defaultMinTime : float;    
}


class Wave1 extends WaveBase{

	function Wave1(){
		maxEnemies = 4;
		minTime = 1.0;
		maxTime  = 5.0;
		frequency = 1;
		defaultMinTime = 2;	
	}    
}


class Wave2 extends WaveBase{

	function Wave2(){
		maxEnemies = 8;
		minTime = 3.0;
		maxTime  = 6.5;
		frequency  = 2;
		defaultMinTime = 3;
	}    
}

And this can be set aside in your scripts folder. I tested it with a small javascript on a game object:

#pragma strict
var myWave : WaveBase;

function Start () {
	myWave = new Wave1();
	Debug.Log(myWave.maxTime); //prints 5.0;
	
	Invoke ("SwitchWaves", 1);	//invoke after a second for testing
	yield WaitForSeconds(2);
	Debug.Log(myWave.maxTime); //prints 6.5;
}



function SwitchWaves(){
	myWave = new Wave2();	
}

This makes switching waves fairly clean, but it may be more complex than you’d need, since you’re only setting parameters. Why not use a single class, with a function index?

For example:

class Wave{
	var makeWave : Function[] = [
		Wave1,
		Wave2,
		Wave3	
	];
	var maxEnemies : int;
	var minTime : float;
	var maxTime : float;
	var frequency : float;
	var defaultMinTime : float;

	function Wave(n : int){
		makeWave[n-1]();
	}
	
	
	function Wave1(){
	
		maxEnemies = 4;
		minTime = 1.0;
		maxTime  = 5.0;
		frequency = 1;
		defaultMinTime = 2;    	
	}
	
	function Wave2(){
		maxEnemies = 8;
		minTime = 3.0;
		maxTime  = 6.5;
		frequency  = 2;
		defaultMinTime = 3;   	
	}
	
	
	function Wave3(){	
		//more parameters	
	}
	
	
}

and again tested with :

#pragma strict
var myWave : Wave;

function Start () {
	myWave = new Wave(1);	
	Debug.Log(myWave.maxTime);	//5.0
	
	SwitchWaves();
	

	Debug.Log(myWave.maxTime);	// 6.5

}



function SwitchWaves(){

	myWave = new Wave(2);
	
}

This version makes the calling of waves a little more intuitive.