- Home /
Sorting through the same variable attached to several GameObjects from least to greatest, then returning an enum state based on that order
Scenario:
In my scene, there are 10 character. 5 are player controlled, 5 are AI controlled. The game is turned based, and who's turn it is is decided by an enumeration state, controlled by the CharacterStats script attached to each character in the scene.
enum Turns { Turn1, Turn2, Turn3, Turn4, Turn5, Turn6, Turn7, Turn8, Turn9, Turn10 }
var turn : Turns;
Turn1 goes first, then Turn2, Turn3, etc, until it reaches Turn10, and then it restarts at Turn1.
This is fine. I have the entire thing working, turn-based works well and all my functions do as I want. My issue arises with setting each player's state. I could go by hand and set each character's Turn state in the inspector to who I want to go first, but instead I would like the state to be determined by the character's variable of weight(weight being the weight of all the characters equipment).
I already have each character's weight totaled, each character's total weight being represented by the variable totalWeight(which is calculating the weight of all equipped items to the character, also in the CharacterStats script attached to each character).
So, how could I check each character's total weight and from there, set the character with the lowest weight as Turn1, then next lowest as Turn2, all the way up to 10.
I've been trying to get a for loop working, tried with Bubble Sorting, but can't how to have the function recognize which Character's CharacterStats script the lowest weight came from to return the Turn1 state to. So I guess my overall question is this:
How can I get my function to figure out which character that lowest weight is coming from, so that it can change the state of that specific character to Turn1, and then move on to the second lowest, to Turn2, so on and so forth.
Obviously I'm aware that I may just be going about it entirely wrong, in which case I would love if someone would simply point me in the right direction. Any suggestions?
An easy way is to simply sort the list. If they are not already in an array, put them in an array. If you need the original array to remain in its original order, create a new, parallel array. If you put the items in a generic .Net List, you can supply the comparison function and call their sort, or for ten items, a simple bubble sort with the build-in array class would do the job.
Well, I've got the list sorted, but the problem is how do I return the correct Turn state to the correct script?
As in, say I received the lowest weight of 10 from the CharacterStats attached to the character named $$anonymous$$, 20 from Larry, 30 from Steve. How can I let the function know that the 10 belongs to Steve's CharacterStats script so that it changes his Turn to Turn1, Larry's to Turn2, and Steve's to Turn 3?
Sorting the list is definitely easy, I agree with that. I'll keep trying at it.
Without looking at your script, I'm in the dark. But lets assume your array, ins$$anonymous$$d of being a list of weights, is a list of CharacterStats. CharacterStats are a component of each player (I'm assu$$anonymous$$g). You sort the list of CharacterStats based on weight, so at the end you have a sorted list combines weight and other character information. You cannot use the built-in sort function for this, so you either have to sort it using bubble sort or use something like a .Net generic list that allows you to supply the comparison function.
var characterStatsArray = new CharacterStats[10];
I don't know how you have your logic setup to initiate the fights. Perhaps you assign back to each character his turn after the sort, or maybe you have a global script that calls out characters, then that script can use the sorted list as the order of battle.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Items with Statistics(such as attack damage) that actually effect the character? 2 Answers
Rotating a Sphere with the mouse? 1 Answer
Sprint Error script? 2 Answers