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 = [

const _pursuitLoops = [

const _hiddenLoops = [

const _escapeShots = [

const _sceneBGM = [

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

func _initialize():
	if Director._inPursuit == true:

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

	if Director._inPursuit == false and $Calm.playing == false:

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

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

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

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

## rng function to change song pull
func _shuffle_bgm():
	_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