- Home /
how can I get values stored in a list?
How could I get the random values stored in the list, to then calculate what the average is?
gamingTypes.Add (new Gaming ("Board Games", Random.Range(-5, 5)));
gamingTypes.Add (new Gaming ("Computer Games", Random.Range(-5, 5)));
gamingTypes.Add (new Gaming ("Phone Games", Random.Range(-5, 5)));
gamingTypes.Add (new Gaming ("Card Games", Random.Range(-5, 5)));
The $$anonymous$$icrosoft docs are probably the best place to check for basic C# questions:
Check the public methods available and note that you can just access by index with square brackets (like an array).
If you are asking how to iterate through a List, that is a basic program$$anonymous$$g question and I highly suggest you look up some tutorials on basic C# program$$anonymous$$g. The gist is to use a for loop and use your iterator to access each index in the List. Then use each one in what ever math you want.
Like add each one to your sum variable, and then at the end of the loop divide by how many values are in your List.
Thanks for the advice and link!
What messed it up for me was having the string and int values in each index. Also, hadnt used lists before, and the unity videos + other questions werent helping.
Gonna check the link, thanks a lot!
Answer by Koyemsi · May 02, 2018 at 06:17 PM
Assuming that your Gaming class looks like this :
public class Gaming
{
public string str;
public int val;
public Gaming(string newStr, int newVal)
{
str = newStr;
val = newVal;
}
}
Here is how you can calculate average :
public class AverageFromList : MonoBehaviour
{
List<Gaming> gamingTypes = new List<Gaming> ();
int total = 0;
void Start ()
{
gamingTypes.Add (new Gaming ("Board Games", Random.Range (-5, 5)));
gamingTypes.Add (new Gaming ("Computer Games", Random.Range (-5, 5)));
gamingTypes.Add (new Gaming ("Phone Games", Random.Range (-5, 5)));
gamingTypes.Add (new Gaming ("Card Games", Random.Range (-5, 5)));
foreach (Gaming g in gamingTypes)
{
print (g.str + " : " + g.val);
total += g.val;
}
print ("Total : " + total);
float average = (float)total / gamingTypes.Count;
print ("Average : " + average);
}
}
$$anonymous$$eep in $$anonymous$$d that since your "total" is an integer you get a "floored" average. So the values
2, 1, 3, -5
Would add up to "1". If you divide 1 by 4 you get 0 ins$$anonymous$$d of 0.25 which is the actual average. That's because you do an integer division since both: "total" and "ga$$anonymous$$gTypes.Count" are integer values so the result is an integer as well. To get a floating point result at lease one of the operands need to be a float. One way would be to define total as float. Another would be
float average = (float)total / ga$$anonymous$$gTypes.Count;
Would using $$anonymous$$athf.RoundToInt work? I need an int, because the list of the activities in set in int, and I want the average to show a real-ish stat in ga$$anonymous$$g. Although I thought of just having the kinds of games on their own, ins$$anonymous$$d of them being inside the ga$$anonymous$$g list. What are your thoughts on this?
On a side note: I've assigned how much a person would like to do x activity. Using buttons you can decide what to ask the npc what it wants to do. How would you make it so each button would check the value the npc has for that activity?
RoundToInt does work :
float average = (float)total / ga$$anonymous$$gTypes.Count;
print ("Average : " + average);
float averageInt = $$anonymous$$athf.RoundToInt (average);
print ("Average integer : " + averageInt);
I probably would keep the ga$$anonymous$$g kinds in the list if possible, because it keeps them "linked" with their statistics. And please excuse me, but I didn't understand the other question very well.
You're absolutely right @Bunny83, thanks. I've updated my code.
Thanks! Didn't know I could use g.str/val to get the info I wanted.
Doesn't have to do with this, but I've assigned how much a person would like to do x activity. Using buttons you can decide what to ask the npc what it wants to do. How would you make it so each button would check the value the npc has for that activity? I think this has to do with being able to make a reference to other scripts, gonna be that rn, but I' appreciate your input!
Thanks again, and I hope you're having a good day/night!
Didn't know I could use g.str/val to get the info I wanted.
Be careful, str and val are not methods. These are just the names I gave to my variables in the Ga$$anonymous$$g class, but I could have named them myString and myValue, or gameType and gameQuantity. When you feed your List, it's like assigning i.e. "Board Games" to the str variable, and a random int to the val variable.
Glad if I could help, have a nice day/night too.
I know, i know. I meant that I forgot about accessing the info using the "g.". Thanks for explaining tho.
$$anonymous$$y question is: I have a list of activities like this:
activity.Add (new Activities ("Fishing", Random.Range(-5, 5)));
activity.Add (new Activities ("Running", Random.Range(-5, 5)));
activity.Add (new Activities ("Hunting", Random.Range(-5, 5)));
I got buttons on the screen, with the names of the activities, and depending on the one you press, it would check the list, and get the value. How can I do this?
Sorry if this is really basic, been a while since the last time i programmed. Thanks a lot
Answer by Koyemsi · May 03, 2018 at 11:59 AM
OK, I have the solution, here we go. First, the script : using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.UI;
public class Activities {
public string activityName;
public int activityValue;
public Activities (string aString, int anInt) {
activityName = aString;
activityValue = anInt;
}
}
public class ActivityManager : MonoBehaviour {
List <Activities> activity = new List<Activities> ();
void Start () {
activity.Add (new Activities ("Fishing", Random.Range (-5, 5)));
activity.Add (new Activities ("Running", Random.Range (-5, 5)));
activity.Add (new Activities ("Hunting", Random.Range (-5, 5)));
foreach (Activities act in activity) {
print (act.activityName + " : " + act.activityValue);
}
}
public void ReadInList (Button aButton) {
string stringToTest = aButton.GetComponentInChildren<Text> ().text;
print ("Pressed " + stringToTest);
var matchingEntry = activity.Find (Activities => Activities.activityName == stringToTest);
if (matchingEntry == null) {
print ("No such activity in List.");
} else {
print ("Found " + stringToTest + " with value : " + matchingEntry.activityValue);
}
}
}
The script is attached to an empty GameObject that I called @ScriptHolder. Your buttons' OnClick have to call the ReadInList() method with an argument refering to themself. The script reads what's written in the button's Text (be sure to label them exactly the same as the activities stored in your List). And the magic happens ;)
Answer by widsmob1 · May 12, 2018 at 05:11 AM
count = 0; foreach (Gaming g in gamingTypes) { total += g.val; count++; } print ("Total : " + total); float average = (float)total / count print ("Average : " + average); }