- Home /
General question about functions, cofunctions and wait timers
Hey,
I'm new to coding and so I'm having trouble wrapping my head around some of the concepts. I'm trying to do everything as "lean" as possible although I'm taking more and more shortcuts and my code starts getting out of hand.
I'm currently writing the spawn script for Tower Defence based game. I want to use random generated wave-types (boss, normal, speed etc.) so I built a WaveController function to keep track of it. The first problem I encountered was how to keep the different wavetypes differentiated in a nice way, I just mashed everything into nested ifs - very bad, but I couldn't come up with anything better atm. The second thing was to set spawnintervals so that everything doesn't spawn at once. I figured that I needed to use Coroutines to use WaitForSeconds but that kinda turned my plan upside down as the functions doesn't wait until a Coroutine has been excecuted until it continues with the rest I want to do. Now I have to time everything and it just seems I've painted myself into a corner using coroutines. What is it that I am not getting here? Continuing down this path seems like a really bad idea, please halp! =/
Maybe I'm not making sense at all, if so I'm terribly sorry!
Textboxes are only there to fault search, ignore them
I use many public variables, I started out like this to test other functionality first. Aiming to clean up later.
Code Below:
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour {
//Need to take arguments from menu about difficulty, settings etc; later!
// The Monster that should be spawned; Should be randomed
public GameObject monsterPrefab;
// Spawn Delay in seconds
//private float spawninterval;
//private float waveinterval = 10f;
public int waveNumbers;
private int[] waves= new int[15];
private int waveCount = 1;
private int monsteramount;
private int wavetype;
public string stringToEdit;
public string stringToEdit1;
public string stringToEdit2;
public string stringToEdit3;
public string stringToEdit4;
//Need to set different wavetypes;
//1 = normal : monsteramount 15, speedmod 3, interval 3, sizemod 1, healthmod 1
//2 = mass : monsteramount 30, speedmod 3, interval 1, sizemod 0.5, healthmod 0.5
//3 = speed : monsteramount 15, speedmod 5, interval 3, sizemod 1, healthmod 0.7
//4 = boss : monsteramount 1, speedmod 3, interval 3, sizemod 2, healthmod 15
//Need to improve mobs as spawns continue; tiers? 1,2,3,4,5?
// Use this for initialization
void Start () {
waveNumbers=5;
//Need to random the waves first
int count = waveNumbers / 5;
//stringToEdit4 = "outside loop";
for (int i=0;i<count;i++) {
//stringToEdit4 = "inside loop";
waves[0+i*5]=1; //first wave in a serie of 5 is always normal
int k = 0;
while (k<3){
waves[k+1+i*5]=Random.Range(1,3);
k++;
}
waves[4+i*5]=4; //last wave in a serie of 5 is always a boss
}
//stringToEdit4 = (waves[0]).ToString();
//need to start spawning
//stringToEdit = "start wave controller";
StartCoroutine(WaveController ());
}
IEnumerator WaveController () {
//controls the waves
//stringToEdit1 = "inside wavecontroller";
int monsteramount;
float speedmod;
float interval;
float sizemod;
float healthmod;
foreach (int wave in waves) {
stringToEdit = wave.ToString();
if (wave==1){ //if normal
monsteramount = 15;
stringToEdit3 = monsteramount.ToString();
speedmod = 3f;
interval = 3f;
sizemod = 1f;
healthmod = 1f;
stringToEdit2 = "Round done, spawning normal in 15";
yield return new WaitForSeconds(15);
StartCoroutine(WaveSpawner(monsteramount, speedmod, interval, sizemod, healthmod));
yield return new WaitForSeconds(40);
}
else {
if (wave==2){ //if mass
monsteramount = 30;
speedmod = 3f;
interval = 1.5f;
sizemod = 0.5f;
healthmod = 0.5f;
stringToEdit2 = "Round done, spawning mass in 15";
yield return new WaitForSeconds(15);
StartCoroutine(WaveSpawner(monsteramount, speedmod, interval, sizemod, healthmod));
yield return new WaitForSeconds(40);
}
else {
if (wave==3){ //if speed
monsteramount = 15;
speedmod = 5f;
interval = 3f;
sizemod = 1f;
healthmod = 0.7f;
stringToEdit2 = "Round done, spawning speed in 15";
yield return new WaitForSeconds(15);
StartCoroutine(WaveSpawner(monsteramount, speedmod, interval, sizemod, healthmod));
yield return new WaitForSeconds(40);
}
else {
//if (wave==4){ //if boss
monsteramount = 1;
speedmod = 3f;
interval = 3f;
sizemod = 2f;
healthmod = 15f;
stringToEdit2 = "Round done, spawning boss in 15";
yield return new WaitForSeconds(15);
StartCoroutine(WaveSpawner(monsteramount, speedmod, interval, sizemod, healthmod));
yield return new WaitForSeconds(40);
//}
}
}
}
}
}
IEnumerator WaveSpawner (int amount, float speed, float time, float size, float health) {
int monstercount = 0;
yield return new WaitForSeconds(time);
stringToEdit3 = "start to spawn";
for (int i=0;i<amount;i++) {
stringToEdit4 = "inside spawnloop";
GameObject newMonster = Instantiate(monsterPrefab, transform.position, Quaternion.identity) as GameObject;
newMonster.GetComponent<Monster> ().monsterNumber = monstercount;
newMonster.GetComponent<Monster> ().transform.localScale.Set(size,size,size);
newMonster.GetComponent<NavMeshAgent> ().speed = speed;
stringToEdit1 = monstercount.ToString();
monstercount = monstercount + 1;
yield return new WaitForSeconds(time);
}
}
void OnGUI() {
GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
GUI.TextField(new Rect(10, 30, 200, 20), stringToEdit1, 25);
GUI.TextField(new Rect(10, 50, 200, 20), stringToEdit2, 25);
GUI.TextField(new Rect(10, 70, 200, 20), stringToEdit3, 25);
GUI.TextField(new Rect(10, 90, 200, 20), stringToEdit4, 25);
}
}
Your answer
Follow this Question
Related Questions
How to create a delay in a function being called in update? 2 Answers
Need Help with a Null Reference 1 Answer
SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers
C# WaitForSeconds isn't waiting for any seconds 2 Answers
SetTrigger animation won't wait for correct time before continuing code 1 Answer