- Home /
JS: Access sections of an array, without activating the rest
Hey guys, were on the final hurdle in our inspector spawning script. The problem we are having now is cycling through the list and activating that wave or push.
At the moment, only the first push of the first wave does anything - which is great. But we have been trying to do it in a way that once that first push has finished (currentSpawned is the same value as totalToSpawn) then it moves onto the next push within that wave, and upon completing the final push it moves to the next wave and repeats.
We think we came pretty close using a switch, but it got overly complicated for us as the code was enormous. Does anyone know what we need to do to make certain waves and pushes within that wave active and how to progress through it?
Any help is so dearly appreciated. Thank you for your time. Here is the current code :)
/* OUTERMOST CLASS*/
var totalWaves : int;
var currentWave : int = 0;
var totalPush : int;
var currentPush : int = 0;
var totalToSpawn : int;
var currentSpawned : int = 0;
var spawning : boolean = false;
var enemies1 : GameObject;
var enemies2 : GameObject;
var point1 : GameObject;
var point2 : GameObject;
var amountOfWaves : WaveAmount[];
function Start()
{
totalWaves = amountOfWaves.length;
Debug.Log(totalWaves);
}
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()
{
if(currentWave < totalWaves)
{
totalToSpawn = GetSpawn1Count(0,0,0);
if (currentSpawned < totalToSpawn)
{
yield WaitForSeconds(/*startTimeSinceLastPush*/1);
SpawnUnit();
currentSpawned += 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);
}
}
}
}
Answer by Jamora · Mar 05, 2014 at 12:01 PM
You need arrays of GameObjects in Composition
. The enums aren't really needed, because you can just set the enemy & spawn GameObjects directly to the composition via the inspector.
I can see the getters for a single enemy and spawn point were a mistake, because we're not really interested in the single enemies. What we should've done is just spawn the pushes. So instead of the getter, you have a Spawn method
//in wavePushes
function SpawnComposition(composition : int){
enemyBuild[composition].Spawn();
}
class Composition
{
var enemies : GameObject[];
var spawnPoints : GameObject[];
var spawnCount : int[];
function Spawn(){
//Instantiate spawnCount amount of each enemy at its spawnpoint.
//Make sure the indexes match.
}
}
Doing it like this will allow you to just call a function from the parent class to spawn any wave.
function SpawnCheck(){
for(var wave=0;wave< amountOfWaves.Length;wave++){
for(var push=0;push<GetAmountOfPushes(wave);push++){
for(var composition=0;composition<GetAmountOfCompositions(wave,push);composition++){
SpawnComposition(wave,push,composition);
yield WaitForSeconds(GetWaitTime(wave,push));
}
}
}
}
Hey Jamora, thanks for helping out again :)
Once more your code seems to push the boundaries of our knowledge, its genius. Thank you very much :)
Weve been trying to work this into our code for over an hour now - but perhaps were doing things wrong.
First off, we removed all the previous functions within the class (GetEnemy1, GetSpawn1 & GetSpawn1Count). Next we replaced the original class Composition and function SpawnCheck with your updated versions.
We tried to add in some spawning systems to get things rolling, but we ran into a few errors and with the state of $$anonymous$$d were in atm (fried brains) were stumped for answers.
Weve rolled the code back to its most stable and clear state and edited in a few comments detailing what we think is going on (and yay! its a much smaller section of code now .. we think we were supposed to delete the other bits anyway, but we wernt 100% sure)
/* OUTER$$anonymous$$OST CLASS*/
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;
}
}
/*NESTED CLASSES*/
class WaveAmount
{
var name : String;
var wavePushes : WavePushes[];
}
class WavePushes
{
var startTimeSinceLastPush : float;
var enemyBuild : Composition[];
function SpawnComposition(composition : int)
{
enemyBuild[composition].Spawn();
}
}
class Composition
{
var enemies : GameObject[];
var spawnPoints : GameObject[];
var spawnCount : int;
var spawnAmmount : int = spawnCount;
function Spawn()
{
//Instantiate spawnCount amount of each enemy at its spawnpoint.
//$$anonymous$$ake sure the indexes match.
//////// Sort out spawning once we sort out errors. :)
}
}
function SpawnCheck()
{
for(var wave=0; wave < amountOfWaves.Length; wave++) //I think this adds all waves set in inspector to the var wave.
{
for(var push=0; push < GetAmountOfPushes(wave); push++) //$$anonymous$$ identifier: 'GetAmountOfPushes'.
{
for(var composition=0; composition < GetAmountOfCompositions(wave,push); composition++) //$$anonymous$$ identifier: 'GetAmountOfCompositions'.
{
SpawnComposition(wave,push,composition); // $$anonymous$$ identifier: 'SpawnComposition'.
yield WaitForSeconds(GetWaitTime(wave,push)); // $$anonymous$$ identifier: 'GetWaitTime'.
}
}
}
}
@iHunter$$anonymous$$iller You get the $$anonymous$$ identifier error because you have not defined anything with that name. (hint: they should be functions)I think the Unity Learn scripting modules will help you a lot. For this, check out #6, Loops.
Okay hmm. Commented in thoughts and errors again.The void part of the errors make us think weve done something obviously silly .. but we dont know what. As far as we can tell its using the same setup that Jamora gave us the last time .. we think at least :/
/* OUTER$$anonymous$$OST CLASS*/
var spawning : boolean = false;
var amountOfWaves : WaveAmount[];
function Update()
{
if(!spawning)
{
StartCoroutine("SpawnCheck");
spawning = true;
}
}
function SpawnComposition(wave : int, push : int, composition : int)
{
amountOfWaves[wave].SpawnComposition(push, composition);
}
/*NESTED CLASSES*/
class WaveAmount
{
var name : String;
var wavePushes : WavePushes[];
function SpawnComposition(push : int, composition : int)
{
wavePushes[push].SpawnComposition(composition);
}
}
class WavePushes
{
var name : String;
var startTimeSinceLastPush : float;
var enemyBuild : Composition[];
function SpawnComposition(composition : int)
{
enemyBuild[composition].Spawn();
}
}
class Composition
{
var enemies : GameObject[];
var spawnPoints : GameObject[];
var spawnCount : int;
var spawnAmmount : int = spawnCount; //doesnt work .. results in a 0 ... not updating .. perhaps add to update function?
function Spawn()
{
//Instantiate spawnCount amount of each enemy at its spawnpoint.
//$$anonymous$$ake sure the indexes match.
Debug.Log("Hello");
}
}
function SpawnCheck()
{
for(var wave=0; wave < amountOfWaves.Length; wave++)
{
for(var push=0; push < SpawnComposition(wave); push++) // The best overload for the method 'test.SpawnComposition(int, int, int)' is not compatible with the argument list '(int)'. & the below error on this line too.
{
for(var composition=0; composition < SpawnComposition; composition++) // Operator '<' cannot be used with a left hand side of type 'int' and a right hand side of type 'function(int, int, int): void'.
{
SpawnComposition(wave,push,composition);
yield WaitForSeconds(amountOfWaves.wavePushes.startTimeSinceLastPush(wave,push));
}
}
}
}
@iHunter$$anonymous$$iller It's not exactly the same as I gave you. The void error means that the compiler expects to get a value from the function, but you aren't returning anything.
The SpawnComposition
function is meant to spawn a composition. It doesn't make much sense to use that when trying to deter$$anonymous$$e how many pushes to spawn in the for loop.
You should make functions GetAmountOfPushes(wave)
, GetWaitTime(wave,push)
and GetAmountOfCompositions(wave,push)
, which, like their names imply, return how many pushes a wave has, how long one has to wait after each push in a wave and how many compositions each push in a given wave has. Example:
GetAmountOfPushes(wave : int) : int {
return amountOfWaves[wave].wavePushes.length;
}
Furthermore, your line 45 doesn't make any sense. Ins$$anonymous$$d, have an int array, like I showed you:
var spawnCount : int[];
In index 0 you have how many spawns you have of the enemy in its index 0, then spawn all of them at the spawn point in the spawnpoint array at index 0. This is what I meant by index matching. By having everything in an array, you can easily loop over all of them using a for loop.
@iHunter$$anonymous$$iller Looks like it could work. I should still point out that your SpawnCheck
is in a bit of a funny place logically; all the other functions of the outer class are at the top of the page. Also, I personally prefer having getters in each of the nested classes ins$$anonymous$$d of those huge one-liners at the beginning. (why? encapsulation and feature envy.)
Good luck!
Answer by JayeshModi · Nov 02, 2021 at 09:23 AM
Refer below resource on encapsulation in java https://www.flowerbrackets.com/encapsulation-in-java-programs/
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Show Top 10 Of 1 GameType 2 Answers
JS: Access var inside class 1 Answer
How to reference a multidimensional list? 1 Answer
A node in a childnode? 1 Answer