- Home /
What's a faster method to sort data than iterating through an array?
My game's FSM is based on utility stats which each NPC curates indicating how much they need to eat, poo, flee from danger, and the like. Currently I use a very cumbersome implementation that I wrote when I was first learning to program:
int highestValue;
activityArray [0] = npc.BathroomNeed; //NPC's urge to use bathroom
activityArray [1] =npc.FoodNeed; //Urge to eat
activityArray[2] = npc.SafetyNeed; //Urge to run away
highestValue = FindMax (activityArray); //Return the index of the array element with the highest value
if (highestValue == 1)
//Transition to FSM state.Bathroom
else if (highestValue == 2)
//Transition to FSM state.Food
else
//Transition to FSM state.RunAway
BathroomNeed, FoodNeed, and SafetyNeed are all ints between 0-100 indicating how much the NPC wants to do that action, and I set my FSM transition by iterating through that list, comparing values until I determine which array index is highest, then I iterate through a second conditional loop to compare the highest index against my list of transitions, then I finally trigger the correct transition within the if block.
This has two obvious problems to me, given that on average this function runs 500-1,000 times/second in my game: #1 is that it is incredibly clunky and slow, and #2 is that it's hard-coded: every time I modify the AI I'll need to modify a lot of code, take a very close look at everything, and cross my fingers that I don't miss anything.
Given that, is there a better way to go about this? All I need to do is a) compare a number of utility stats represented as ints to determine which one is highest, b) set a transition based on which is highest.
You mean like a list or dict? I assume the .Sort() itself will be much faster than my implementation, but how would I associate the values with their corresponding state? That would require holding at least two references, one to the utility stat and one to the transition, and that path seems to lead toward creating custom structs and overcomplicating everything.
Try looking at a $$anonymous$$ax Binary Heap (Priority Queue). That might be something your after.
Answer by alabdali · Oct 02, 2014 at 05:06 AM
use Array.Sort
If I'm doing a sort, how do I preserve the relationship between the array index and the state transition it corresponds to?
Answer by revolute · Oct 02, 2014 at 05:11 AM
My opinion on this is that you should make an int and set as index to current maximum value. Maintain such value and do not bother checking unless the values in the array changes. You can just simply check whether new value in the array is larger than current max value. Simple yet cuts the price of checking all the time.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Number of scripts per Object. Optimization question. 1 Answer
Finding inactive game objects in a pool 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do I efficiently maintain a constantly-changing list without triggering GC? 2 Answers