Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by giraffe1 · Jul 03, 2016 at 10:35 PM · listssyntaxgeneric list

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



Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image giraffe1 · Jul 03, 2016 at 11:10 PM 0
Share

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++)
             { }
         }
     }
avatar image KrabyGame · Jul 03, 2016 at 11:29 PM 0
Share

$$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
          }
 }
avatar image giraffe1 · Jul 04, 2016 at 01:33 AM 0
Share

Thank you for the great example.

This is what I was thinkinking

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image giraffe1 · Jul 04, 2016 at 01:39 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# - Two lists linked with the same value 1 Answer

Assigning Values In Start () That Will Change 1 Answer

Problem adding List<> objects to another List<> 1 Answer

C# list removeat then collapses. 2 Answers

How to activate and deactivate child of an instantiated parent object, 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges