I am having a class that consists of three arguments: An integer, a float and a string.
Then I have a List which type is the mentioned class. The List will be filled dynamically at runtime. Then I need to sort this list on each Update() cycle. But I need to take both the integer and then the float into account, first sorting by the integer and then the float.
So for example, when I have this data inside: 4, 50.2f, exampleA 4, 55.1f, exampleB 5, 33.7f, exampleC
…I need to sort the list so that I get the following order: exampleC, exampleB, exampleA
I also don’t know what is best for performance, to sort the already exisiting List, where the values of each element will be updated constantly, or to create a new List/Array that only holds the sorted output of the original List?
Any advices? If possible, maybe with a little example code? Thanks!
Sorting a List is fairly straightforward as there’s already built-in functionality to do that. You can write your own comparison function that will do what you want it to do. The comparison function can either return -1 , 0 or +1 depending on whether the values are higher,the same or lower.
If the values are the same (for your integer value) then you can sort by the second value (your float) and so on.
In the end it will be a racing game. When the player is not racing, I want to display a screen that shows the currently racing cars with the leader on top. Next to the car name there should be the current lap time displayed and some other info’s that I take from the Car object [the class I mentioned].
Since I want to display the lap time, I need to update this screen constantly. Also I need to order the cars in position so that the leader will always be on top.
For position calculation I store an integer in each Car object that contains the lap, and then a float value that holds the current distance to the finish line. By that I should theoretically be able to sort the cars correctly, even if stuff like overlapping happens.
And since we are on the performance subject:
The distance calculation is done in the following way:
The track has “Node” GameObjects throughout the course. The Nodes know the name of the “Start” Node and the next node ahead. A node infront of a branch has then two “next” Nodes and an integer telling the Node which of the two next Nodes is the faster path. On Scene loadup, every Node calculates the distance to finish by recursively jumping to the next Node and calculate the distance to the next Node again until the “Start” Node has been reached, taking always the shorter path in branches into account.
Now each car has a NavModule that gets the last passed Node as transform [and uses it in case the car got off track and needs a respawn]. I then calculate the distance to the next Node, get the Node script of that Node and add the float value that holds the distance to the Start Node.
I already tested it for the player’s car, it works nicely and I can precisely calculate race positions even with branches and sub-branches, do respawns to logical points and give the AI some sort of intelligence since they could derive the shorter way at a branch since the Node infront of the branch holds that info.
BUT I still need to do a distance calculation each frame for a maximum of 7 cars alive: The distance between the car’s current position and the next Node position.
So I am wondering if this would be feaseable, performance-wise. It gives me lots of advantages for precise calculations as mentioned above and I could even use the Nodes to set up a race line for the AI later on probably.
I heared that using sqrMagnitude is a little bit faster than using Vector3.Distance() for calculation so here is what I do: