- Home /
How do I sort Game Objects from closest to furthest, by tags?
I am working on a music game and I have an editor. I place notes down that follow a song. These notes all contain the information to be created in their name, so I need to write a file with their names. I need their names written in order from closest to furthest away, because it will improve the performance of my game. Now the question:
Does anyone know a way to iterate through a loop that will sort these Game Objects from closest to furthest? You should note that I am using findwithtag to find them all. I considered using a list, but I cant think of an efficient way to do that. Does anyone know a cost effective (in processing time) way of doing this? Please and Thank You!
P.S. psuedo works great for me too. Possibly better, I like to learn.
Thanks fattie (is that meant to be sound rude? :p ) but it hasnt helped much, Ive looked into it already. And @fafase, I need them closest to the start of the song. so I will explain a bit more for others. the name of each object is structured like this: R,2.0451 In that example, a red note (R) is created when the song passes 2.0451 seconds. In the script that reads the text files, it splits the name into two, at the comma. It then tests the second part to see if its larger or equal to the time of the song. Are you with me? :p Anyway, the problem is that they get written to the file in the order the findwithtag returns them in, so it will show a note created at 4 seconds, and one at 3 seconds after that. I could test each note always, but that would slow it down. I thought if I could test them in order, it would be more efficient. That is whats.
Long answer short, closest to 0, furthest from 0. thats what I would like.
So I went ahead and tried a method. I will attach it, if people can help though:
function WriteFile(){
//the file path
var sW : StreamWriter = new StreamWriter(Application.dataPath + "/" + SongName+"_"+selStrings[selGridInt]+".txt");
//the notes that will be written into the file
var Notes = GameObject.FindGameObjectsWithTag ("Note");
//each note found has its own line written
var Names = new Array();
//The array called Names holds each note found
for (var Note in Notes) {
Names.Add(Note);
}
/*This is where it went wrong, right below.
What I am trying to do is sort these notes by
the time that they are created, and this is
data stored in its name. the problem is that the
name isnt just a float. it also stores the notes
colour. It is comma seperated. I can split it up
and sort half of it but then what? */
Names.Sort();
for (var Name in Names) {
sW.WriteLine(Name);
}
sW.Flush();
}
So how do I sort the list? It looks like this
2.612649,O
6.128291,B
3.572816,R
@Fattie maybe you can help again? Your recent answer got me trying harder!
Answer by Bunny83 · Sep 02, 2012 at 01:58 AM
Well, your code contains two problems ;)
First, the Array class. It's slow but the worst thing is it's not type safe. You store GameObjects in this container. A GameObject can't be compared to another GameObject. You want to compare the GameObject "names" which are strings.
So my advice is: Use a List instead of Array and store the names in the list and not the Gameobjects:
var Notes = GameObject.FindGameObjectsWithTag ("Note");
var Names = new List.<String>(Notes.length); // set initial length for better performance
for (var Note in Notes) {
Names.Add(Note.name); // store the name of the gameobject
}
Names.Sort();
edit To use the generic list you have to import the System.Collections.Generic namespace at the top of your script:
import System.Collections.Generic;
@Bunny83 I havent really dealt with lists in javascript before, and I keep getting the error
BCE0005: $$anonymous$$ identifier: 'List'.
but on the bright side, I understand how it should work and I dont see the problem in the code.
Yes, you're right, advise - verb; advice - noun ;) I will fix this :D
I wouldn't say one in 10k. If you talk about the "internet generation" then yes, they actually dropped their language :D
@@rppe: Great you figured it out. I'm actualy impressed :D $$anonymous$$ost people here can't do a step without a clear "advice" ;)
But i will add the import since it shouldd help others as well.
Yes, let's get back to program$$anonymous$$g as I do not agree with the French part and I really do know what I am talking about.