0

So i have the following List:

List<AttackStyle> AttackStyles = new List<AttackStyle>();

With the following types of objects:

AttackStyle lStyle = new AttackStyle();
lStyle.Name = "Attack Achilles";
lStyle.ParameterID = 0;
lStyle.Forward = Vector3.forward;
lStyle.HorizontalFOA = 70f;
lStyle.VerticalFOA = 40f;
lStyle.DamageModifier = 1f;
lStyle.ActionStyleAlias = "Jump";
lStyle.IsInterruptible = true;

AttackStyles.Add(lStyle);

Now i wish to find the field ParameterID where the ActionStyleAlias is equal to a value (for instance "Jump")

This is for a Unity application so the search / find needs to be as fast as possible.

5 Answers 5

3
var result = AttackStyles.FirstOrDefault(x => x.ActionStyleAlias == "Jump").ParameterID;
Sign up to request clarification or add additional context in comments.

3 Comments

you mean x => x.ActionStyleAlias == "Jump"
@HosseinNarimaniRad yes, was typing with javascript in mind :P
This will throw exception if doesn't find the AttackStyle with ActionStyleAlias=="Jump" condition.
1

The straight solution is:

 var pId = AttackStyles.FirstOrDefault(x=> x.ActionStyleAlias == "Jump")?.ParameterID

But if you want to get a better performance, it would be better, to index the most useful property which you want. Therefore, construct a dictionary on the most useful fields to get a better performance in time. For example:

   var styles = new Dictionary<string, AttackStyle>();
   styles.Add("Jump", new AttackStyle() 
                      {
                           Name = "Attack Achilles",
                           ParameterID = 0,
                           Forward = Vector3.forward,
                           HorizontalFOA = 70f,
                           VerticalFOA = 40f,
                           DamageModifier = 1f,
                           ActionStyleAlias = "Jump",
                           IsInterruptible = true
                      });

Then, find the object by this:

var pId = styles["Jump"].ParamterId;

or if it might be null:

if(styles.Keys.Contains("Jump"))
    var pId = styles["Jump"].ParamterId;

2 Comments

Could you give me an example of such?
@MarcRasmussen the constructed index on keys would be B-Index. So, there is no need to find each time the list in a linear order (O(N)). You can find the result in O(log(n)) order. Also, there is a better data structures such as trie which you can find your result in O(1).
1
var param = AttackStyles.First(x => x.ActionStyleAlias.Equals(value)).ParameterID;

Comments

1

Let's return first ParameterID if there's the required item in the collection; -1 otherwise:

 var result = AttackStyles
   .Where(item => item.ActionStyleAlias == "Jump")
   .Select(item => item.ParameterID)
   .DefaultIfEmpty(-1)
   .First();

Comments

0

You can try extension methods. Also you should consider null cases:

static class Extensions
{
    public static int? FindParameterId(this List<AttackStyle> values, string actionStyleAlias)
    {
        return values.FirstOrDefault(x => x.ActionStyleAlias == actionStyleAlias)?.ParameterID;
    }
}

Then use it:

List<AttackStyle> attackStyles = new List<AttackStyle>();
var parameterId = attackStyles.FindParameterId("Jump");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.