- Home /
The question is answered, right answer was accepted
How to find x item in list?
I know I'm not the first person to ask this, but I was hoping to get a little extra and custom support because I have spent a few hours on this and am not getting any closer, running out of ideas, and want to see if a kind developer out there could take a few minutes to explain how Lists actually work.
The short story is I have a player attribute system and I'm trying to get the value of each individual attribute and tie them to their respective values (strength does more damage, health gives more health, etc).
The issue is my code uses scriptable objects to create each attribute and the length of this list and values of each attribute are setup through the inspector (not in the script itself), and I'm trying to find a way to specify each individual attribute and grab the value of that attribute so I can assign it to it's respective skill or stat.
Here is a screenshot of what my list looks like in the inspector:
I am able to get a foreach loop to display the vaules of these attributes at start with this code:
private void Start()
{
foreach (PlayerAttributes attribute in Attributes)
{
Debug.Log(" " + attribute.attribute, attribute.amount);
}
}
This is giving me each of the Attribute names as they appear in order from the inspector, and also gives the right value of that Attribute as well:
So my million dollar very simple question is how would I do the same thing, but only access say the third attribute (Accuracy)?
I want to be able to grab the value of Accuracy and then have that increase or decrease the weapon's range stat, so as accuracy grow, so does the range of that weapon, and of course this Accuracy attribute would affect the range stat of every weapon the player uses.
This seems like something that should be very easy to index or access, but I'm having a ton of trouble understanding this and all the tips I see online form their list in the code the are trying to reference, and not have the list form and set values of through the inspector.
Thanks in advance for your help!
Answer by JVene · Nov 04, 2018 at 04:30 PM
Both arrays and List generics are accessed with the [] operator, as in:
PlayerAttributes a = Attributes[ 3 ];
Also, the members can be access without using a reference, as in:
var n = Attributes[ 3 ].amount;
Thanks for the super quick response, I knew this was way easier than I was making it! I can't believe how hard I was making this!
I really appreciate the brief explanation rather than just pasting API documentation.
Answer by Iarus · Nov 04, 2018 at 05:12 PM
Just be careful here, Attributes[3] means the 4th element. In C#, arrays and lists are zero based.
Also if the element at the requested index is null or the list has less elements than you're expecting, this will throw an IndexOutOfBoundsException or something like that. You have to either make sure (absolutely 100% sure) that the list will always be created with all the elements the code is expecting, or validate that Attributes.Length >= 4 before accessing Attributes[3].
Also if you create an other character later and the attributes are not in the same order, you might be expecting Accuracy , but maybe it'll be Strength. Then you would need to:
enforce the order in the list
or search for the correct attribute, either everytime you need it or cache it's index during initialization.
or maybe use a dictionary instead
or maybe all your heroes and ennemies will all have the same set of attributes and you could replace the list of attributes with an accuracy property and a strength property
It all depends what your game needs.
Thanks for your tips and feedback! The coding community is so helpful and tips like this really help me learn and reinforce good habits.