Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Shadowcast · Dec 21, 2017 at 05:20 AM · randomprogramminglists

Looking for a way to edit specific values in List by calling a random method?

This is my first time here on these forums. I am still relatively new to programming, however, I am getting what I feel is a decent grasp of it. I began writing my first scripts from scratch, however I have run into a snag, and after a few hours of searching, I am unable to figure out a solution. Or at least one I can understand in any depth.

Simply put, I am looking to create a randomized list of tasks for an NPC to perform that is weighted. There would be several "types" of NPCs which would have different weights based on their type. These weights simply change how often the tasks appear, and the list would not change once set.

I would like much of this script to be flexible enough to be able to add new tasks or NPC types without a lot of change to the code.

I have gotten much of this completed, however, I can not seem to figure out how to assign the appropriate weights by NPC type. Below is the relevant code.

 //Creates random Job range, using a predetermined Min and Max possible range for each job per NPC type
     private IEnumerator SetJobWeight () {
         while (jobMinWeight.Count < (int)Job.NumberOfJobs)
             jobMinWeight.Add (jobValue);
         while (jobMaxWeight.Count < (int)Job.NumberOfJobs)
             jobMaxWeight.Add (jobValue);
         while (jobWeight.Count < (int)Job.NumberOfJobs)
             jobWeight.Add (jobValue);
 
         for (int i = 0; i < (int)Job.NumberOfJobs; i++) {
             Gambler ();
             if (i != 0)
                 jobWeight[i] = jobWeight[i - 1] + Random.Range (jobMinWeight[i], jobMaxWeight[i]);
             else
                 jobWeight[i] = Random.Range (jobMinWeight[i], jobMaxWeight[i]);
         }
 
         yield return null;
     }

I am looking at changing specific jobValues in both the jobMinWeight and jobMaxWeight.

My original thought was to store these values in methods and call the needed method tied to the type name. This idea seems to have the advantage of only changing the needed task weights (by default the all have a weight of 0)

Below is an example

 private void Gambler () {
         //Min Weights
         jobMinWeight [(int)Job.ContinueAction] = 75;
         jobMinWeight [(int)Job.PlayGame] = 75;
         jobMinWeight [(int)Job.Drink] = 25;
         jobMinWeight [(int)Job.Wait] = 10;
         jobMinWeight [(int)Job.Leave] = 25;
 
         //Max Weights
         jobMaxWeight [(int)Job.ContinueAction] = 150;
         jobMaxWeight [(int)Job.PlayGame] = 200;
         jobMaxWeight [(int)Job.Drink] = 75;
         jobMaxWeight [(int)Job.Wait] = 50;
         jobMaxWeight [(int)Job.Leave] = 25;
     }

This works, for a single NPC type. But how can I get this to work for multiple types, selected at random.

I thought about the possibility of putting the types in an enum, and using Invoke to call the method by name by pulling the random enum toString. But my understanding is Invoke is not very efficient.

Is there a better way to do this than Invoke?

Or am I approaching this completely wrong?

Is there a way to store methods in an array or list?

I can provide the rest of the code if needed but I thought this should be enough.

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

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

79 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 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 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

Whats wrong with this PerlinNoise function 0 Answers

Return question’s answer at the determined index 1 Answer

Need help with generating random Multiplier,Need help with generating random multipliers 0 Answers

How do you put a List under an Array item (Hard time explaining) 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