Search code examples
buttonaudiogame-maker

Game Maker - Turn On/Off Sound


What I have

I have two rooms: rm_home and rm_options. I have a sound: snd_Bgm. And, I have three objects: obj_bgm, obj_BtnOnClicked, and obj_BtnOffClicked.

What I want is very simple

Player can turn on/off the snd_Bgm.

What I have done

In obj_bgm, I have these events:

Create Event: set var global.sound to 1

Room Start: stop sound snd_Bgm; if global.sound == 1 then play sound snd_Bgm

In obj_BtnOnClicked, I have these events:

Left-Pressed Event: play sound snd_Bgm; set var global.sound to 1

In obj_BtnOffClicked, I have these events:

Left-Pressed Event: stop sound snd_Bgm; set var global.sound to 0

I put obj_BtnOnClicked, and obj_BtnOffClicked in rm_options, which can be accessed from rm_home. Then, I put obj_bgm in rm_home.

This is the problem

When game start, it will show rm_home and plays the snd_bgm. I go to rm_options, then click the obj_BtnOffClicked to turn off the sound, so the sound is off. But, when I go back to rm_home, the sound is on again.

What I think

I shouldn't put Create Event: set var global.sound to 1 in obj_bgm, because when rm_home start, it takes the value of var global.sound from Create Event. But, if I put Create Event in obj_BtnOnClicked or obj_BtnOffClicked, it shows a Get Error.

What should I do? Please explain your answer. Thanks.


Solution

  • Are your rooms and your variables persistent ?

    If rm_home is not persistent, it will restart every time you leave it. So every object placed in that room will be reset, including obj_bgm, which sets your sound variable to 1. I think this is the reason the sound restarts when you come back to rm_home.

    If you still want to reset the room but not this particular object, you can make it persistent. You can do it either by ticking the box in the object or through code.

    If you put the create event in a button object, it will not be read until you go to the options room. So when the obj_bgm tries to set it to 1, it does not exist yet. I believe it causes the error.

    I make these remarks on assumptions, but I would need to see your code or your error message to help you further.