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
        }
    }
}

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] *...*

Or iterate through all known enum values for that particular type and use that as index.

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.