2015-04-22 00:08:24

Hey all.
I've run into an interesting and somewhat frustrating issue.
Take a look at the following:
void main()
{
show_game_window("ding");
sound ding;
ding.load("c:\\windows\\media\\ding.wav");
wait(5);
dingplay();
}
void dingplay()
{
ding.play_looped();
}
Now, if I try and run this I get:
File: C:\Users\unstoppable\Documents\function test.bgt
On line: 9 (1)
Information: Compiling void dingplay()
File: C:\Users\unstoppable\Documents\function test.bgt
On line: 11 (1)
Line: ding.play_looped();
Error: 'ding' is not declared
When clearly if it looks back a function, yes it is.
What am I doing wrong here?
I've followed all the tutorials as closely as I could, but still this issue won't go away.
Thanks in advance!

2015-04-22 00:16:17

Declare the ding sound as global, that is above the main function, not inside it. Like this, you call the dingplay function from within void main but thesound is still local (available) only to the main function.
Or, a simpler way, why use a separate function just to play one specific sound looped? You could equally as well just have the ding.play_looped(); line directly in the main function, right below the line where you load the appropriate file into the sound object.
Hope this helps,
Lukas

I won't be using this account any more or participating in the forum activity through other childish means like creating an alternate account. I've asked for the account to be removed but I'm not sure if that's actually technically possible here. Just writing this for people to know that I won't be replying, posting new topics or checking private messages until the account is potentially removed.

2015-04-22 00:19:33

yeah, I wanted to practice doing stuff in different functions. Eventually if I want to make a game I will have to do that.
Thanks for the quick reply!

2015-04-22 00:32:21

Hello,
The thing is, the ding object is declared in the main function. That means that if you try to access it from another function, it will not recognise it. Think of it as 2 boxes. One is the main function, the other is the dingplay function. The ding sound is in the main box, so if you try to reach into the dingplay box to find that ding sound, you will come up empty. You can fix this by, instead of declaring the ding sound in your main loop, you can declare it outside of any function. Look at this code:
sound ding;
void main()
{
show_game_window("ding");
ding.load("c:\\windows\\media\\ding.wav");
wait(5);
dingplay();
}
void dingplay()
{
ding.play_looped();
}

2015-04-22 13:23:45

The image of the boxes is quite good to understand : when you give work to do to the playding box, it can't know what "ding" is if you tell it. So, give it the information as a parameter of the function, or set the information where the box can access it! smile