Game Development Community

BUG in GameUtil.SoundManager

by Scott Goodwin · in Torque X 2D · 01/27/2008 (11:06 pm) · 1 replies

There are a couple of bugs in GameUtil.SoundManager. This is only a problem if you need to remove sound groups. Here's the gist of the problem (not too hard to fix).

1) in public virtual void Reset()

...
// unregister all sound groups
for (int i = 0; i < _soundGroups.Count; i++)
UnregisterSoundGroup(_soundGroups[i] as SoundGroup);

This doesn't work because _soundGroups is a HashTable so _soundGroups[i] as SoundGroup is always null.
If you fix this say by

foreach (DictionaryEntry myDE in _soundGroups)
UnregisterSoundGroup(myDE.Value as SoundGroup);

The next problem shows up

2) in public virtual void UnregisterSoundGroup(SoundGroup group)

...
_soundGroups.Remove(group);

This doesn't work because Remove expects a key, not a value.
If you fix this by passing in the key, then the Remove modifies the HashTable which will screw up the foreach in (1) above.

There are a few different ways to fix this depending on your preferences ...

#1
01/29/2008 (1:41 pm)
Good catch. The only other issue to be aware of is that it's best to avoid the foreach operator as much as possible since it eventually leads to a large garbage collection hit that drops the performance. It's always best (though not as elegent) to manually check the size of the collection and iterate through it with a for loop.

John K.