Coding Syntax question
In the 'FindOffensiveCover' method I am trying to iterate through the appropriate list based on the 'CoverType' enum.
Is there a cleaner way of doing this than the way I am doing it?
I don't like using the new keyword to initialize the temp. list.
Enum:
public enum CoverType
{
Standing,
Crouching,
Proning
}
My Class:
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class CoverManager : MonoBehaviour
{
public static CoverManager instance { get; private set; }
private List<CoverNode> standingNodes;
private List<CoverNode> crouchingNodes;
private List<CoverNode> proningNodes;
private void Awake()
{
instance = this;
standingNodes = new List<CoverNode>();
crouchingNodes = new List<CoverNode>();
proningNodes = new List<CoverNode>();
}
public void AddNode(CoverNode node, CoverType type)
{
if (type == CoverType.Standing)
{
standingNodes.Add(node);
}
else if (type == CoverType.Crouching)
{
crouchingNodes.Add(node);
}
else if (type == CoverType.Proning)
{
proningNodes.Add(node);
}
}
public void FindOffensiveCover(CoverType type)
{
List<CoverNode> list = new List<CoverNode>();
if (type == CoverType.Standing)
{
list = standingNodes;
}
else if (type == CoverType.Crouching)
{
list = crouchingNodes;
}
else if (type == CoverType.Proning)
{
list = proningNodes;
}
for (i = 0; i < list.Count; i++)
{
//do stuff
}
}
}
I Could do something like this, but it seems very dirty. Any suggestions appreciated.
public void FindOffensiveCover(CoverType type)
{
if (type == CoverType.Standing)
{
for (int i = 0; i < standingNodes.Count; i++)
{ }
}
else if (type == CoverType.Crouching)
{
for (int i = 0; i < crouchingNodes.Count; i++)
{ }
}
else if (type == CoverType.Proning)
{
for (int i = 0; i < proningNodes.Count; i++)
{ }
}
}
$$anonymous$$aybe with a Dictionnary ?
Dictionnary<CoverType, List<CoverType>> nodes = new Dictionnary<CoverType, List<CoverType>> ();
private void Awake()
{
instance = this;
nodes.Add(CoverType.Standing, new List<CoverNode>);
nodes.Add(CoverType.Crouching, new List<CoverNode>);
nodes.Add(CoverType.Proning, new List<CoverNode>);
}
public void AddNode(CoverNode node, CoverType type)
{
nodes[type].Add(node);
}
public void FindOffensiveCover(CoverType type)
{
for (i = 0; i < nodes[type].Count; i++)
{
//do stuff
}
}
Thank you for the great example.
This is what I was thinkinking
Answer by Bunny83 · Jul 03, 2016 at 11:58 PM
You shouldn't use 3 seperate lists in the first place. Your enum categorizes the nodes. So the CoverType is a "property" of a node. You should simply add a nodeType variable to your "CoverNode" class and put them all in the same list. That simplifies the usage.
It also makes more sense to give each node it's type directly. For example when searching for the closest node it's way simpler to put a trigger on each node and use OverlapSphere to only get the nodes in range. If each node knows it's type you can simply filter out the one you need / want.
If you have really a lot of nodes (say 1000+) you could still create 3 seperate lists at runtime and place them in a dictionary like @KrabyGame suggested in his comment. However in most cases since most maps with a lot nodes are huge, it's way more efficient to use OverlapSphere.
I was originally looking for something like @$$anonymous$$rabyGame solution but wasn't sure on the syntax. I was going to iterate through the lists to find the closest relevant nodes. But now I am rethinking my cover system. I never thought of the trigger method and using OverlapSphere. I have never used triggers for anything yet! completely forgot about em.
Got me thinking now!!!
P.S. I don't know why it would let me mark @$$anonymous$$rabyGame response as an answer.
Thanks to everyone for their input.
Answer by Eudaimonium · Jul 03, 2016 at 11:30 PM
You could theoretically unify that.
enum is nothing more than an integer with a pretty alias name.
Using that, you can use an enum value as an integer, for indexing purposes.
You currently have 3 lists, one for each cover type nodes. You could instead have a List of Lists:
List<List<CoverNode>> AllCoverNodes = ...
And access them a pair of indices, first of which is an integer cast of an enum (you can do a for loop on an enum count):
AllCoverNodes[(int)CoverType.Proning][i] ...
Or iterate through all known enum values for that particular type and use that as index.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084157im_/https://answers.unity.com/themes/thub/images/avi.jpg)