0 votes

I have 2 AudioStreamPlayer2D, one of which is in the main scene. I tryed to access both from a different scene using:

onready var game_music = get_node("/root/path_to_music")

The one in main scene caused me no problem, but the other one returned null!!!

Attempt to call function 'is_playing' in base 'null instance' on a
null instance.

How can I access AudioStreamPlayer2D in other scenes!? and does been a main scene or not have any thing to do with this problem?

Thank you in advance :-)

asked Apr 30 in Engine by u_nes (12 points)
recategorized Apr 30 by u_nes

1 Answer

0 votes

Here's the thing: "scenes" don't matter when the game is running. All you have is a bunch of nodes in the scene tree.

The error you're getting is because the path you have in that get_node() is returning null instance because it's invalid. You need to look at your scene tree and use the correct path to the node you want.

If you post a pic of your scene tree, I can probably show you exactly.

answered Apr 30 by kidscancode (17,702 points)

Thank you for your answer..

I understand that this error could be generated from a wrong path, but i highly doubt it ...i have checked a million time!!

onready var game_music = get_node("/root/main/backgroundMusic")

This is my scene tree:
again your help is much appreciated.

What node is the script with that line on? onready is probably your problem. Children become ready first, so "main" is going to be ready last. Calling this in the ready of a node that's further down the tree will not work.

the on ready line is in a different scene in the same directory as all other scenes
i used the same line to access themeSong from main_menu scene and it worked but backgroundMusic loaded from main scene is causing problems
enter image description here

The "directory" has nothing to do with it. When you run the game, the nodes are all in the scene tree, and they become ready in reverse tree order. You can't call a node that's above during _ready().

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.