SFML community forums

Help => Audio => Topic started by: marqss on May 19, 2015, 01:44:15 pm

Title: Multiscope sound access
Post by: marqss on May 19, 2015, 01:44:15 pm
I would like to know if there is a way to access an instance of sf::sound or sf::music in a scope different than where it is declared and/or loaded? Eg declare a sound in main() but play() it in another function or vice versa?
The code below gives me errors on sm.play() ("undeclared identifier" and "left of '.play' must have class/struct/union")

void playwilhelm(int i=1000)
{
       
        sm.play();
        cout << "\nScream should be played now\n";
        Sleep(i);
        cout << "\nEnd Scream\n";
}

int main()
{
        sf::SoundBuffer wilhelm;
        wilhelm.loadFromFile("music\\wilhelm scream.wav");
        sf::Sound sm;
        sm.setBuffer(wilhelm);
        sm.setVolume(50);

        playwilhelm (2000);
 

Also, I imagine it's not possible but correct me if I'm wrong: I can't use a variable when setting up audio to play, eg when calling sf::Sound x, can I?
Title: Re: Multiscope sound access
Post by: eXpl0it3r on May 19, 2015, 01:58:43 pm
This is a basic C++ question and doesn't really have anything to do with SFML, because it applies the same way to any C++ class. As such I highly recommend you go back to your C++ book (or similar) and study more on the subject of scope, variables, references, classes and so on. If you don't have a C++ book, I advise you to get a good one (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).