- Home /
Set up spawn seqence based on specified timing
So I'm attempting to make a rhythm game in which enemies are spawned from certain locations based off of the song's rhythm. I know calculating the rhythm is a whole different subject, but let's pretend I have a set 'beat' calculated. How would I go about spawning enemies according to that 'beat'? I currently have gameobjects set up as spawners and they spawn prefabs of enemies.
The most logical way I could think to do this and maintain the ability to 'craft' the level and spawnings is to create a file and read from the file on 'beat'.
E.G. File Contents: xxxxxTxxxLxxxxB
On a beat> one character is read from the file and that corresponds to what happens (x does nothing, T spawns prefab 1, etc.
File and Song are started at the same time (and the file length is predetermined to make sure there are enough 'beats' marked.
So I guess my question is: Is this a 'good' way to go about this? And how exactly could I accomplish sequentially reading characters based on a timer of some sort?
@Draze, I don't know the is your method is proper or not. but you can read file contain at specific interval by using InvokeRepeating. this call the function at regular interval. you can put there your reading file code.
I've seen similar things to that, as far as my idea for implementation however, I think I would run into the problem of reading the next character each time. I guess I could do a global variable to keep track of where I am and try and set end parameters for that repeating to stop. Good suggestion, I'll look into it more.
@Draze, you can make an array or list which contains your levels information(like xxxxxTxxxLxxxxB so, levelData[0] = x, levelData[1] = x levelData[5] = T,...) and just store it's index what is last one. and this array can be store in file and get it back when you use it.
Answer by Naphier · Jan 29, 2015 at 10:41 AM
That's not a bad way to store it. There's so many ways this could be done. You could set up arrays to hold the spawn/beat data too. I like to use CSV files a bit for things like this so I can edit them in a spreadsheet, but there's no reason a simple text file wouldn't work. You have to balance: is it easier to write a function to parse the file data into an array for use OR is it easier to write out the arrays and have a function that retrieves the arrays. I do something similar in one of our games for the level data (CSV -> parsing function -> struct array).
Now you have everything in an array just set up a timer variable that gets activated on beat and keep track of the array's index.
Psuedo code
Onstart - float spawnTimer = Time.time;
OnUpdate
if beat and (Time.time - spawnTimer > minTimeBetweenSpawns) {
Spawn(arrayIndex);
arrayIndex++;
spawnTimer = Time.time;
I do something similar for animating sprites in our game that is using beat detection. http://u3d.as/8c6 is inexpensive and works well. I've yet to figure out how to determine the volume/amplitude of the beats, but I've not spent much time on it either.
Hope this helps.