Godot Version
4.5
Question
I have an audiostreamplayer set up in a colorrect node such as this!
@onready var Sound = $AudioStreamPlayer
The stream I want to play is set up in the editor Quick Load tab. No problem there because when I press the playing button, the sound plays.
However when I call the routine in the colorrect node signal
on_tree_entered()
Sound.play()
I get the error message
‘Invalid call. Nonexistent function ‘play’ in base ‘Nil’.’
which tells me that the node Sound does not exist. (play()) exists because it shows up in the code completion help box when I type in the code.
So what am I doing wrong and how do I resolve the problem?
Thanks for any help in this matter!
Hi,
Could be related to another instance of your script not having a valid reference to Sound?
How are you instantiating your ColorRect, by adding it to the scene, or a dynamic scene instantiation?
I am not at my computer at the moment so it is difficult to get a screenshot of what I am doing, but I when I set up my scene, I’m adding the colorRect to the scene using the editor as the child of a Gridcontainer which in itself is a child of a Node2d. The audioStreamplayer is a child of the ColorRect.
Well, seems like something that should work to me. I suppose we’ll have to wait for you to be at your computer again so that you can link some screenshots!
The node enters the tree, then instantiates all child nodes. When they are created, they send a signal back up the tree, and the parent runs it’s _ready() function. What you did wrong was call play() too early. Put it in _ready() and it’ll work.
Also avoid putting code into _enter_tree() unless you have a specific reason for it. In this case, if you really want it there, you need to construct the node in code outside the tree, then use add_child() to add it to the tree after Sound.ready has been emitted.
Thanks, Dragonforge. I will try that. Do I put it in the colorrect _ready, or the Node2d _ready.
Anyway, as I promised to sixrobin, here is the screenshot of my problem
Doesn’t matter. In this case you already have a script for the ColorRect and the AudioStreamPlayer is a child of that so I recommend keeping it there.
put Sound.play in _ready() and Sound.stop on signal tree_exiting;
tree_entered happens before ready so your @onready variables are null
tree_exited happens after your node is deleted, so it’s children are null.
The correct alternative to each of these functions is _ready and tree_exiting respectively.
Ok, I’ve made the changes suggested, but now I get this error.
Caution : DO NOT LAUGH AT THE SUBJECT OF THIS PROJECT. IT IS NOT INTENDED FOR COMMERICAL USE. IT IS ONLY OF VALUE TO ONE PERSON THAT I KNOW OF.
In video 1 the sound plays at the beginning of the project over the pictures of the pizzas. I DO NOT WANT THAT! I want the sound to play over the ‘blinking’ squares.
In video 2 I accidently placed the sound.play function in the timeout function of the thinker node hoping that would resolve the problem. But it created a pause sfx that I was not prepared for that I liked and would like to keep. However, it too has the same problem of playing the sound over the pictures of the pizzas that I do not want.
So how do I play the sound only when the color rects are blinking?
@gertkeno,
Thanks for your advice, do you know if the effect will work with the 2nd video that I suggested.
Tough to say without much of the script. You could add the sound whenever you display the colors screen, where ever that is in code.
I see a strange pattern in that color_rect.gd script, you could use a @export var ColorList: Array[Color] instead of assigning values in the script/_init, as well as use the array’s .pick_random() function
@export var ColorList: Array[Color] = [# your colors or assigned through the editor]
func _on_timer_timeout() -> void:
self.color = ColorList.pick_random()