so for a gaming I'm making, I need to use a lot of different soundeffects, in order to be able to close all instances of the soundeffects when closing the game, I need to keep them in a List. The problem is, when I keep them in a list that has a lot of different soundeffects on it, I have to look up which soundeffect corresponds to which entry in the list, such as SoundEffect[0] or SoundEffect[48]. Is there a handy way to call things from a list using their names? For example SoundEffect[Strike] or SoundEffect[Death] and the likes?
If anyone has a way to make this easier to use, please let me know!
Don't use a List
. Use a Dictionary
and store each sound effect under a unique ID. Then you can reference each sound by its ID, and it will return in constant time - no lookups required.
// Declaration
Dictionary<string, SoundEffect> dict = new Dictionary<string, SoundEffect>();
// Adding a sound effect
dict.Add("punch_sound", punchSoundEffectObject);
// Getting a sound effect
var sfx = dict["punch_sound"];
(Obviously replace SoundEffect
in the example with whatever class you are using.)