Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sendatsu_Yoshimitsu · Oct 02, 2014 at 03:10 AM · c#optimizationfinite-state-machine

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Oct 02, 2014 at 03:17 AM 0
Share

Have you considered using a sorted collection?

avatar image Sendatsu_Yoshimitsu · Oct 02, 2014 at 03:35 AM 0
Share

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.

avatar image mattyman174 · Oct 02, 2014 at 04:25 AM 0
Share

Try looking at a $$anonymous$$ax Binary Heap (Priority Queue). That might be something your after.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by alabdali · Oct 02, 2014 at 05:06 AM

use Array.Sort

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · Oct 02, 2014 at 09:13 AM 0
Share

If I'm doing a sort, how do I preserve the relationship between the array index and the state transition it corresponds to?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges