How i can pause music in background with CheckButton (what i need to put in my script,please help)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By marko-prog

I dont know how to pause music in background with CheckButton(toggle) please help!
what i need to put in my script.
I use AudioStreamPlayer.
That should to work like that:
When i click OFF (music is off) when i click again ,ON(music is on again)
Pls help!

:bust_in_silhouette: Reply From: Sween123

There’s a signal “toggled” for CheckButton
connect this signal to where your script attached.

func _on_CheckButton_toggled(on):
    if on:
        AudioStreamPlayer.play()
    else:
        AudioStreamPlayer.stop()

Thank you ,but that doesnt work,in my output is writen Non-static function ‘play’ can only be called from an instance.

marko-prog | 2020-02-09 12:23

AudioStreamPlayer is a class. In the code given by Sween123, you have to replace it with your specific instance of that class. So if the scene this script is attached to has a child of type AudoStreamPlayer called “Music”, you would write $Music.play() or get_node("Music").play().

njamster | 2020-02-09 12:29

Yes, this is just sample code. AudioStreamPlayer refers to your AudioStreamPlayer node. You have to change it according to where your AudioStreamPlayer is.

Sween123 | 2020-02-09 12:32

I do that,but when i start my debugger and click on button to pause music,it stop and write:

Invalid call. Nonexistent function ‘play’ in base ‘CheckButton’
Idk why??
My file with music is Music.ogg,my AudioStreamPlayer is named AudioStreamPlayer…

marko-prog | 2020-02-09 12:46

If the debugger says Nonexistent function ‘play’ in base ‘CheckButton’, that means you are calling the nonexsitent method “play” in the CheckButton node. It should be called from AudioStreamPlayer.
Can I see your SceneTree?

Sween123 | 2020-02-09 12:49

marko-prog | 2020-02-09 13:00

Sorry I can’t open it…
You can just type out how your nodes are arranged in the SceneTree
if your AudioStreamPayer is a child node of where your script is attached: use get_node(“AudioStreamPlayer”) or get_node(…“/AudioStreamPlayer”)
if your AudioStreamPlayer is a parent node, use get_parent()
or you can just use get_node(Your AudioStreamPlayer’s path). Example: get_node(/root/…/AudioStreamPlayer)

Sween123 | 2020-02-09 13:10

○Game
○UI
○ScoreNode
○Buttons
○Emitters
○Globals
AudioStreamPlayer

AudioStreamPlayer is child node of GAME node,my script for AudioStreamPlayer is in GAME node

marko-prog | 2020-02-09 13:16

func _on_CheckButton_toggled(on):
    var audio = get_node("AudioStreamPlayer")
    if on:
        audio.play()
    else:
        audio.stop()

Sween123 | 2020-02-09 13:19

func _ready():
var isEnabled = true
var player = AudioStreamPlayer.new()
self.add_child(player)
player.stream = load(“res://Music.ogg”)
player.play()

This playing my music in background,myb can help you?
I write your code in my script,my debugger works,output is without error but when i click on my toggle button ,nothing happend??

marko-prog | 2020-02-09 13:29

Change func ready() to func _ready()

Sween123 | 2020-02-09 13:36

i forgot to add _ready() in comment,this _ready() is already in code…

marko-prog | 2020-02-09 13:38

Then this should be fine.
Is it that the music is never being played even when you just start the game?

Sween123 | 2020-02-09 13:40

Music works,music playing in background all time,
I like that,but i made CheckButton to stop music if someone dont like it,but button doesnt work

What do you think?What can be problem?

marko-prog | 2020-02-09 13:43

It should work.
The first time when you toggle the button, is it not turned off?
Did you connect the signal to this method?
Add a breakpoint in _on_CheckButton_toggled(on) to see if the method is being called.

Sween123 | 2020-02-09 13:54

No,when I toggle the button,music is not turned off,
I connect Toggle Button with Game node…

If it is wrong,with which node i should connect CheckButton?

marko-prog | 2020-02-09 13:59

Then this should work.
The _ready() function where you add the child “AudioStreamPlayer” and _on_CheckButton_toggled(on) is in the same script, right?

Sween123 | 2020-02-09 14:02

Yes,they are in the same script(GAME script)but that doesnt work,when i click to toggle button,music wont stop…?idk why?
I connect toggle button with GAME node…

marko-prog | 2020-02-09 14:06

Did you disable the property “Toggle Mode” of the CheckButton?
Is the method being called?

Sween123 | 2020-02-09 14:07

No,i see when button is off,when is on…but that doesnt work…

marko-prog | 2020-02-09 14:09

Is the method being called?
You can use a breakpoint to see if it is called.

Sween123 | 2020-02-09 14:10

Oh,idk how to use breakpoint,(i never use breakpoint)…

marko-prog | 2020-02-09 14:13

How many CheckButtons do you have? One?
Is it the one instanced by code or manually?
If it is instanced by code, you should connect the signal in code, too. (You can’t connect manually if it is instanced by code since it will be only instanced after you run the game. In Editor you won’t even able to see the CheckButton because it’s not there and therefor you can’t connect the signal)

Sween123 | 2020-02-09 14:14

To add or remove a break point, click on the leftmost side of the line of code.

Sween123 | 2020-02-09 14:16

I have 1 checkbutton,this is Music checkbutton…
I click and i got little red cube next to number…

audio.play() and audio.stop() doesnt light
But other like func,var,if,else…number light?

marko-prog | 2020-02-09 14:23

Is that mean they are not connected?
audio.play() and audio.stop()

marko-prog | 2020-02-09 14:26

Did you connect the SIGNAL “toggled” to the method _on_CheckButton_toggled(on)?

Sween123 | 2020-02-09 14:28

Yes i do that,i connet signal Toggled to this method

marko-prog | 2020-02-09 14:32

To connect a signal manually, click on the node CheckButton in the editor. There should be a window Inspector that list out the properties. Right to the Inspector Menu there’s the Menu “Node”, click that and you will see a list of sinals. Find the signal “toggled” and double-click it. A window will popup, you just connect it to where your script is attached.

To connect a signal in code. get_node("AudioStreamPlayer").connect("toggled", self, "_on_CheckButton_toggled")

I don’t see the connect function in your code, and I see that your CheckButton is instanced in code from the line add_child(player). If that’s the case, you need to add the connect function to really connect the SIGNAL to the method.

Sween123 | 2020-02-09 14:38

I put that your get_node(… in my script where is everything and still doesnt work?..

marko-prog | 2020-02-09 14:48

My checkbutton is connected to node where is my script attached…

marko-prog | 2020-02-09 14:51

Add the connect function in _ready()

Sween123 | 2020-02-09 14:54

func _ready():
    ......
    get_node("AudioStreamPlayer").connect("toggled", self, "_on_CheckButton_toggled")

Sween123 | 2020-02-09 15:03

I do just like that,doesnt work…
no errors in output and debugger,just when i start game and click button,nothing to happend

marko-prog | 2020-02-09 15:06

Sorry… I typed wrong. I mean:

get_node("CheckButton").connect("toggled", self, "_on_CheckButton_toggled")

Sween123 | 2020-02-09 15:12

Debugger:
Attempt to call function ‘connect’ in base ‘null instance’ on null instance

marko-prog | 2020-02-09 15:19

Where’s your CheckButton?
change get_node("CheckButton") according to where it is.

Sween123 | 2020-02-09 15:22

My checkbutton is in Settings button node,Settings node is in UI node and UI node is in Game node

marko-prog | 2020-02-09 15:30

Then use get_node("UI/Settings/CheckButton")

Sween123 | 2020-02-09 15:32

Idk what happening,it doesn’t work??

marko-prog | 2020-02-09 15:37

I want so much to write the whole scene and give the whole thing to you…
It should work.
Is it that you get a null node again?
To get child node: use get_node(path) where path is child node name + “/” + child’s child name + “/” and so on.
Don’t type the name wrong.

Sween123 | 2020-02-09 15:39

I dont get a null ,when i click button nothing happend,music is not off
If you can do that …pls do haha…
I realy dont know what happening…

marko-prog | 2020-02-09 15:43

You give me the project I fix it for you…?

Sween123 | 2020-02-09 15:45

Or if you want I can send you a sample scene.
It will only take me a few minutes.

Sween123 | 2020-02-09 15:46

Okey,if you can pls send me a sample scene

marko-prog | 2020-02-09 15:48

OK, how do I send you? Email?

Sween123 | 2020-02-09 15:49

Yes,send on this email
tryndameremain0@gmail.com

marko-prog | 2020-02-09 15:51

I have sent.

Sween123 | 2020-02-09 16:05

Ok,i download that,what now? :slight_smile:

marko-prog | 2020-02-09 16:16

Unzip it and put it in a godot project. Since I don’t know your game world, you can’t just copy the sample scenes, that won’t work. You can open it and see how it works and go to your own scene and fix the problems. It’s late here, I have to sleep now. If you have further questions, you can reply back in email.

Sween123 | 2020-02-09 16:20

Okey,thanks and good night…i will send you question on email

marko-prog | 2020-02-09 16:21

Hi is there anyway you can add the solution here because I am also having a similar problem

Tigerbolt5 | 2020-08-11 22:03