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 Tristsin · Mar 11, 2013 at 09:06 PM · javascript

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?

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 robertbu · Mar 12, 2013 at 01:47 AM 0
Share

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.

avatar image Tristsin · Mar 12, 2013 at 02:28 AM 0
Share

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.

avatar image robertbu · Mar 12, 2013 at 02:36 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

10 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

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

Delete object when two collision or more 1 Answer


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