- Home /
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.
Your answer
Follow this Question
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