Storing and Removing GameObjects - List or Dictionary?

Hey all!

Looking for some tips on which data structure is best for storing/removing game objects. What I am doing is storing all my HUD objects into a HUDElements List in my HUDManager. I am using a struct to store information for the object, and adding the struct to my list. When I don’t need to object anymore, I remove it from the List, and when the object is needed again, I add it back.

The problem (I think) is how I am looking for and removing them, here’s a snippet of my code:

public enum HUDType
	{
		MAIN = 0,
		MENU,
		CAMERA,
		CONTROLS,
		RACE_STATS,
		LAPS,
		LAPTIME,
		SPEED,
		FPS,
		DEBUG,
		NUM_OF_HUD_TYPES
	}

public struct HUDElement
	{
		public HUDType hudType;
		public GameObject hudObject;
		public GUITexture gTexture;
		
		public HUDElement( HUDType hudType, GameObject hudObject, GUITexture gTexture )
		{
			this.hudType = hudType;
			this.hudObject = hudObject;
			this.gTexture = gTexture;
		}
	}
	
	private List<HUDElement> HUDElements = new List<HUDElement>();


public void SetHUDElement( HUDType hudType, GameObject hudObject )
	{
		HUDElement incomingHUD = new HUDElement( hudType, hudObject, hudObject.GetComponent<GUITexture>() );
		if( !HUDElements.Contains( incomingHUD ))
			HUDElements.Add( incomingHUD );
		
	}
	
	
	public void RemoveHUDElement( HUDType hudTypeToRemove )
	{
		Debug.Log("Removing HUDType "+ hudTypeToRemove);
		foreach( HUDElement hud in HUDElements )
		{
			if( hud.hudType == hudTypeToRemove )
			{
				Debug.Log("Successfully removed "+ hudTypeToRemove +" from list!");
				HUDElements.Remove( hud );
				return;
			}
			
		}
	}

Although the code above works, I feel like it’s kinda messy and the same could be achieved in a much cleaner way. Any tips would be greatly appreciated as always! Like should I use a different data structure for this, such as Dictionary?

Thanks for your time guys!

Stephane

You’ve got a fixed set of possible element types, so how about?

  • Start by preallocating all of the elements you will ever need and storing them in an array: access the type you need using an offset into the array based on the enum. That will reduce the total number of allocations you ever need to do and give you o(1) access time to find anything you ever need.
  • Back the above up by adding an initialise method to these elements, that you use in place of new in SetHUDElement
  • To give you a fast way of iterating over only active elements, use either a LinkedList or write your own intrusive linked list that embeds into your elements.
  • If you’re going for the LinkedList approach handle your adding and removal using LinkedListNodes. You can make your RemoveHUDElement fast too, by making the LinkedListNode accessible from the HUD element: a simple null check will tell you if the item is active or not: once again o(1), getting rid of the iteration over active elements. Similar thinking applies if you roll your own list solution.

How about that?

This may help you. If not them I’m not much use. :slight_smile: