- Home /
JS: Access var inside class
Im back with another problem in our spawning script. Were attempting to spawn units from the lists we created, but cant seem to figure out how to reference any vars inside these class.
Basically were trying to set it up so that everything works within the inspector.
Inside class composition we set the enemy types to be spawned, the position they spawn at and the amount to be spawned.
I'm not quite sure how to make it work though, the typical instantiate systems ive used before arnt working.
Also were not entirely sure how to go about making it work with the spawn count if anyone can help out with that too. For example if its set to 3 in the inspector, it should spawn 3 of those units at that spawn point.
Any help is much appreciated :)
var enemies1 : GameObject;
var enemies2 : GameObject;
var point1 : GameObject;
var point2 : GameObject;
var amountOfWaves : WaveAmount[];
class WaveAmount
{
var name : String;
var wavePushes : WavePushes[];
}
class WavePushes
{
var startTimeSinceLastPush : float;
var enemyBuild : Composition[];
}
class Composition
{
var Enemy1 : EnemyList;
var Spawn1 : SpawnPoint;
var spawn1Count : int;
var Enemy2 : EnemyList;
var Spawn2 : SpawnPoint;
var spawn2Count : int;
}
function Start()
{
var spawnEnemy1 = Instantiate(Enemy1);
spawnEnemy1.transform.position = new Vector3(Spawn1.transform.position, Quaternion.identity);
}
enum EnemyList
{
enemies1,
enemies2
}
enum SpawnPoint
{
point1,
point2
}
You need to follow the references you've set up. You have no direct reference from the main class to the Composition
class, ins$$anonymous$$d you have a reference from WavePushes
to Composition
. Following this logic all the way to amountOfWaves
, the way you can access Composition from your outer class is
amountOfWaves[waveNumber].wavePushes[pushNumber].enemyBuild[buildNumber].Enemy1;
I recommend 2 things:
1)make all variable names begin with lower case, this makes it impossible to mix them up with class names.
2) $$anonymous$$ake a getter in each class so ins$$anonymous$$d of the huge one-liner with ridiculous feature-envy you'd have, in your outermost class
function GetEnemy1(wave :int ,push :int, composition : int){
return amountOfWaves[wave].GetEnemy1(push,composition);
}
then, in WaveAmount, you'd have
function GetEnemy1(push :int, composition : int){
return wavePushes[push].GetEnemy1(composition);
}
etc. This way, if you ever make a change somewhere in this hierarchy, not everything will explode in your face at once.
With these getters set up, ins$$anonymous$$d of the huge one-liner, you'd have
var spawnEnemy = Instantiate(GetEnemy1(1,3,2));
Oh wow, this is a little over my head, but i think ive more or less got the jist of it, but im a little unsure on which class is the outermost. I think the order is waveAmount, wavePushes, Composition (outermost being the last one?) but the way your code and comments are written im guessing its not even in that order lol.
Either way im getting errors, really unsure what to do with all these changes :/ Im quite dense when it comes to these things.
Among others, i think the main Error is the return not recognizing the amountOfWaves. Ive tried to fill it in to the best of my ability though. Likely something extremely obvious staring me in the face lol
var enemies1 : GameObject;
var enemies2 : GameObject;
var point1 : GameObject;
var point2 : GameObject;
var amountOfWaves : waveAmount[];
class waveAmount
{
var wavePushes : wavePushes[];
function GetEnemy1(push :int, composition : int)
{
return wavePushes[push].GetEnemy1(composition);
}
}
class wavePushes
{
var enemyBuild : Composition[];
function GetEnemy1(wave :int ,push :int, composition : int)
{
// error on the amountOfWaves line below
return amountOfWaves[wave].GetEnemy1(push,composition);
}
}
class Composition
{
var enemy1 : enemyList;
var spawn1 : spawnPoint;
var spawn1Count : int;
var enemy2 : enemyList;
var spawn2 : spawnPoint;
var spawn2Count : int;
}
function Start()
{
var spawnEnemy = Instantiate(GetEnemy1(1,3,2));
// spawnEnemy1.transform.position = new Vector3(Spawn1.transform.position, Quaternion.identity);
}
enum enemyList
{
enemies1,
enemies2
}
enum spawnPoint
{
point1,
point2
}
There is only one level of nesting in this class.
--------------------------------------
| outermost class |
| | |
| -------------------------- |
| | | | |
| WaveAmout WavePushes Composition |
---------------------------------------
Outermost class being the script itself.
/* OUTER$$anonymous$$OST CLASS*/
var enemies1 : GameObject;
var enemies2 : GameObject;
var point1 : GameObject;
var point2 : GameObject;
var amountOfWaves : WaveAmount[];
function Start()
{
var spawnEnemy1;
if(GetEnemy1(0,0,0) == EnemyList.enemies1)
spawnEnemy1 = Instantiate(enemies1);
//ETC.
spawnEnemy1.transform.position = new Vector3(Spawn1.transform.position, Quaternion.identity);
//You'll need the same getter-system for deter$$anonymous$$ing the correct spawn point
}
function GetEnemy1(wave : int, push : int, composition : int) : EnemyList{
return amountOfWaves[wave].GetEnemy(push, composition);
}
/*NESTED CLASSES*/
class WaveAmount
{
var name : String;
var wavePushes : WavePushes[];
function GetEnemy1(push : int, composition : int) : EnemyList{
return wavePushes[push].GetEnemy1(composition);
}
}
class WavePushes
{
var startTimeSinceLastPush : float;
var enemyBuild : Composition[];
function GetEnemy1(composition : int) : EnemyList{
return enemyBuild[composition].enemy1;
}
}
class Composition
{
//these were the variables that were capitalized
var enemy1 : EnemyList;
var spawn1 : SpawnPoint;
var spawn1Count : int;
var enemy2 : EnemyList;
var spawn2 : SpawnPoint;
var spawn2Count : int;
}
/* ENU$$anonymous$$S */
enum EnemyList
{
enemies1,
enemies2
}
enum SpawnPoint
{
point1,
point2
}
This way, GetEnemy(0,0,0) called from Start will return enemy1 from the 1st wave's 1st push' first composition. There is still the problem of you trying to instantiate an enum value. You will need to deter$$anonymous$$e which gameObject to spawn based on the value retrieved from the enum.
Answer by iHunterKiller · Mar 05, 2014 at 03:42 AM
Thank you very very much Jamora :) Your a great help. It now spawns the correct amount input of the unit selected in the location selected.
Our only problem remaining, and i think it might be a really silly or small problem, is dumping the amountOfWaves : WaveAmount array value into the totalWaves variable, so that when we set more than 1 wave we can use that var to move between them.
Ive tried so many different ways to work this in without any luck at all.
Here is the resulting code now though, with the amazing professional help from Jamora its looking really good! =D
/* OUTERMOST CLASS*/
var totalWaves : int;
var currentWave : int = 0;
var totalPush : int;
var currentPush : int = 0;
var spawning : boolean = false;
var enemies1 : GameObject;
var enemies2 : GameObject;
var point1 : GameObject;
var point2 : GameObject;
var amountOfWaves : WaveAmount[];
function Update()
{
if(!spawning)
{
StartCoroutine("SpawnCheck");
spawning = true;
}
}
function GetEnemy1(wave : int, push : int, composition : int) : EnemyList
{
return amountOfWaves[wave].GetEnemy1(push, composition);
}
function GetSpawn1(wave : int, push : int, composition : int) : SpawnPoint
{
return amountOfWaves[wave].GetSpawn1(push, composition);
}
function GetSpawn1Count(wave : int, push : int, composition : int) : int
{
return amountOfWaves[wave].GetSpawn1Count(push, composition);
}
/*NESTED CLASSES*/
class WaveAmount
{
var name : String;
var wavePushes : WavePushes[];
function GetEnemy1(push : int, composition : int) : EnemyList
{
return wavePushes[push].GetEnemy1(composition);
}
function GetSpawn1(push : int, composition : int) : SpawnPoint
{
return wavePushes[push].GetSpawn1(composition);
}
function GetSpawn1Count(push : int, composition : int) : int
{
return wavePushes[push].GetSpawn1Count(composition);
}
}
class WavePushes
{
var startTimeSinceLastPush : float;
var enemyBuild : Composition[];
function GetEnemy1(composition : int) : EnemyList
{
return enemyBuild[composition].enemy1;
}
function GetSpawn1(composition : int) : SpawnPoint
{
return enemyBuild[composition].spawn1;
}
function GetSpawn1Count(composition : int) : int
{
return enemyBuild[composition].spawn1Count;
}
}
class Composition
{
var enemy1 : EnemyList;
var spawn1 : SpawnPoint;
var spawn1Count : int;
var enemy2 : EnemyList;
var spawn2 : SpawnPoint;
var spawn2Count : int;
}
/* ENUMS */
enum EnemyList
{
enemies1,
enemies2
}
enum SpawnPoint
{
point1,
point2
}
function SpawnCheck()
{
totalPush = GetSpawn1Count(0,0,0);
if (currentPush < totalPush)
{
yield WaitForSeconds(/*startTimeSinceLastPush*/1);
SpawnUnit();
currentPush += 1;
spawning = false;
}
}
function SpawnUnit()
{
if(GetSpawn1Count(0,0,0) >= 1)
{
var spawnEnemy1 : GameObject;
if(GetSpawn1(0,0,0) == SpawnPoint.point1)
{
if(GetEnemy1(0,0,0) == EnemyList.enemies1)
{
spawnEnemy1 = Instantiate(enemies1);
spawnEnemy1.transform.position = new Vector3(point1.transform.position.x, point1.transform.position.y, point1.transform.position.z);
}
}
}
}
Sorted it yay!
function Start()
{
totalWaves = amountOfWaves.length;
Debug.Log(totalWaves);
}