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 MortalWombat922 · Nov 30, 2014 at 07:15 AM · characterrpgattribute

Rerolling RPG Character Attributes With Limits

Hey, guys. I'm attempting to make an RPG character creation screen with a button, allowing you to randomize your characters' attributes. The problem is, I would like them to be randomized with a limit. For example, if they have 500 points to divide between 10 attributes, I'd like it to randomize those 500 points, and not any more, or any less.

Here's what I've tried so far: First attempt:

 if(GUI.Button (new Rect(800, 500, 50, 50), "Reroll")) {
     Attribute_Points = 0;
     Invested_Attribute_Points = 490;
     for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
         _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue = 1;
         int randatt = UnityEngine.Random.Range (1, 100);
         if(Invested_Attribute_Points > 0) {
             _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue = _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue + randatt;
             Invested_Attribute_Points = Invested_Attribute_Points - randatt;
         }
 
     }
     Debug.Log (Invested_Attribute_Points);
 }

This one worked ok, but it always returned a leftover point value between -150, to 150. So it's not quite good enough.

Second attempt:

    Attribute_Points = 0;
         Invested_Attribute_Points = 490;
         while(Invested_Attribute_Points > 0)
         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
             _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue = 1;
             int randval = UnityEngine.Random.Range(1, 2);
             if(randval > 1) {
                 _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue++;
                 Invested_Attribute_Points--;
             }
         }

I'm sure this second one would have worked, had it not caused the engine to crash with the ridiculous number of calculations.

I've tried one or two more things, but I got the same results as my first try at it. I'd greatly appreciate any help.

Thanks to the help of BoredMormon, my problem is solved. Here's the solution I decided on:

                     Attribute_Points = 0;
             RemainingPoints = 400;
 
             for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
                 _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue = 10;
                 int randatt = UnityEngine.Random.Range (0, 50);
                     _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue = _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue + randatt;
                     RemainingPoints = RemainingPoints - randatt;
                 
             }
             while(RemainingPoints > 50) {
                 for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
                     int randatt = UnityEngine.Random.Range (0, 5);
                     if(RemainingPoints > 0) {
                         _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue = _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue + randatt;
                         RemainingPoints = RemainingPoints - randatt;
                     }
                 }
             }
             while(RemainingPoints > 0) {
                 for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
                     if(RemainingPoints > 0){
                         _PlayerCharacter.GetPrimaryAttribute(cnt).BaseValue++;
                         RemainingPoints--;
                     }
                 }
             }

It works great. Satisfies every parameter I wanted. Again, thanks a ton!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Nov 30, 2014 at 07:26 AM

Your second script crashed due to an infinite loop. Random.Range is max exclusive. So it was always returning one in your code. Meaning your if condition never entered, and hence your loop never finished. The number of calculations was trivial. Changing line 6 to

 int randval = UnityEngine.Random.Range(1, 3);

or better change line 7 to

 if (UnityEngine.Random.value > 0.5f)  

will make it work as written. However you will find the distribution provided by this method to be mind numbingly flat.

You could produce a better distribution by adding about a third of your points every pass. Once you have completed all three passes then loop through and add or subtract points evenly to make your target score.

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 MortalWombat922 · Nov 30, 2014 at 07:35 AM 0
Share

Aha, that makes a lot of sense. Sorry for such a simple mistake. I should have just checked the documentation. Also appreciate the advice for more random results. Thanks a lot!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Character customization in-game 0 Answers

Character Class Creation 1 Answer

Best way to format characters/models in game? 1 Answer

RPG style character with multiple items 1 Answer

How to get a 3D TextMesh to face character? 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