How do I properly use String literals for loading content?

Posted by Dave Voyles on Game Development See other posts from Game Development or by Dave Voyles
Published on 2012-05-30T01:51:41Z Indexed on 2012/05/30 17:02 UTC
Read the original article Hit count: 350

Filed under:
|

I've been using verbatim string literals for some time now, and never quite thought about using a regular string literal until I started to come across them in Microsoft provided XNA samples. With that said, I'm trying to implement a new AudioManager class from the Net Rumble sample.

I have two (2) issues here:

Question 1: In my code for my GameplayScreen screen I have a folder location written as the following, and it works fine:

menuButton = content.Load<SoundEffect>(@"sfx/menuButton");
menuClose = content.Load<SoundEffect>(@"sfx/menuClose");

If you notice, you'll see that I'm using a verbatim string, with a forward slash "/".

In the AudioManager class, they use a regular string literal, but with two backslashes "\". I understand that one is used as an escape, but why are they BACK instead of FORWARD? (See below)

soundList[soundName] = game.Content.Load<SoundEffect>("audio\\wav\\"+ soundName);

Question 2: I seem to be doing everything correctly in my AudioManager class, but I'm not sure of what this line means:

audioFileList = audioFolder.GetFiles("*.xnb");

I suppose that the *xnb means look for everything BUT files that end in *xnb? I can't figure out what I'm doing wrong with my file locations, as the sound effects are not playing. My code is not much different from what I've linked to above.

private AudioManager(Game game, DirectoryInfo audioDirectory) : base(game)
{
    try
    {
        audioFolder = audioDirectory;
        audioFileList = audioFolder.GetFiles("*.mp3");
        soundList = new Dictionary<string, SoundEffect>();

        for (int i = 0; i < audioFileList.Length; i++)
        {
            string soundName = Path.GetFileNameWithoutExtension(audioFileList[i].Name);
            soundList[soundName] = game.Content.Load<SoundEffect>(@"sfx\" + soundName);
            soundList[soundName].Name = soundName;
        }

        // Plays this track while the GameplayScreen is active
        soundtrack = game.Content.Load<Song>("boomer");
    }
    catch (NoAudioHardwareException)
    {
        // silently fall back to silence
    }
}

© Game Development or respective owner

Related posts about XNA

Related posts about c#