Errors on global AudioStreamPlayer object when calling ready

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

Using a global object for music control, with multiple music players so i can switch from calm to tense music quickly. When testing it solo, it’s doing what I intend but when queueing it up in any other scene as an AutoLoad I get node not found errors (when directly referencing the child players) or I get null instance on the files (when using an onready variable reference). i am an using randomize to do random selects on tracks but this isn’t the issue. (no rng in the Pursuit sequence, because there are segments that link up when the monster loses you). Pretty sure this has something to do with loading. What am I doing wrong?

extends Node2D


var _calmTrack = 0
var _chaseTrack = 0


const _calmTracks = [
	"res://music/calm/Attic_Springs.ogg",
	"res://music/calm/NoHome.ogg",
	"res://music/calm/Seeking.ogg",
	"res://music/calm/VoidSoul.ogg",
	"res://music/calm/Wave.ogg"
]

const _pursuitLoops = [
	"res://music/tense/hurt_loop1.ogg",
	"res://music/tense/mono_loop2.ogg",
	"res://music/tense/noesc_loop3.ogg",
	"res://music/tense/reven_loop1.ogg",
	"res://music/tense/run_loop.ogg"
]

const _hiddenLoops = [
	"res://music/tense/hurt_loop2.ogg",
	"res://music/tense/mono_loop1.ogg",
	"res://music/tense/noesc_loop1.ogg",
	"res://music/tense/reven_loop2.ogg",
	"res://music/tense/run_loop2.ogg",
]

const _escapeShots = [
	"res://music/tense/hurt_end.ogg",
	"res://music/tense/mono_end.ogg",
	"res://music/tense/noesc_end.ogg",
	"res://music/tense/reven_end.ogg",
	"res://music/tense/run_end.ogg"
]

const _sceneBGM = [
	"res://music/calm/Doors.ogg",
	"res://music/calm/Vessel.ogg",
	"res://music/tense/springs_end.ogg"
]


## randomize numbers for song pulls, connect end shots back to regular music
func _ready():
	var _out_ = $Timer.connect("timeout",self,"_initialize")
	$Timer.start(5)

func _initialize():
	_shuffle_bgm()
	
	if Director._inPursuit == true:
		_pursuit_music()
	else:
		_calm_music()


### play calm music
func _calm_music():
	var _myCalm = load(_calmTracks[_calmTrack])
	$Calm.stream = _myCalm
	$Calm.play()
	
	if Director._inPursuit:
		$Calm.stop()
		_pursuit_music()

	if Director._inPursuit == false and $Calm.playing == false:
		_shuffle_bgm()
		_calm_music()

## play tense music
func _pursuit_music():
	var _myChase = load(_pursuitLoops[_chaseTrack])
	$Pursuit.stream = _myChase
	$Pursuit.play()
	
	if Director._playerIsHidden == true:
		$Pursuit.stop()
		_hidden_music()


## play suspense music
func _hidden_music():
	var _myHidden = load(_hiddenLoops[_chaseTrack])
	$Hidden.stream = _myHidden
	$Hidden.play()
	
	if Director._inPursuit == false:
		$Hidden.stop()
		_escape_music()


## play escape one-shot
func _escape_music():
	var _myEsc = load(_escapeShots[_chaseTrack])
	$Escape.stream = _myEsc
	$Escape.play()
	
	if $Escape.playing == false:
		_shuffle_bgm()
		_calm_music()


### event music, direct calls
func _event_music(_resourcePath):
	var _myTrack = load(_resourcePath)
	$Event.stream = _myTrack
	$Event.play()
	
	if $Event.playing == false:
		_shuffle_bgm()
		_calm_music()


## rng function to change song pull
func _shuffle_bgm():
	randomize()
	_calmTrack = randi() % 4
	_chaseTrack = randi() % 4
:bust_in_silhouette: Reply From: Inces

Autoloaded script never reaches ready(), because it only happens when node gets inside scene tree ( Autoload means that a script is a singleton, away from the tree). You need another node ( inside tree) to reach and menage audio tracks of autoloaded script.

Correct. Found out i had loaded the script and the node and the script was firing before the node was ready. Changed it to only autoload the node and now the problem is solved. Thank you for the help!

SnapCracklins | 2022-06-29 02:43