Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Multiscope sound access  (Read 1814 times)

0 Members and 1 Guest are viewing this topic.

marqss

  • Newbie
  • *
  • Posts: 1
    • View Profile
Multiscope sound access
« 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?
« Last Edit: May 19, 2015, 01:51:12 pm by marqss »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Multiscope sound access
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything