Having problems playing sound through a function on another script

4.2

Hello there!

Ok so as the title says, I’m calling a function on my player script from another script called rand_player:

@onready var stepsPlayer = $steps

@onready var stp_1 = preload("res://SFX/Render/stp_01.wav")
@onready var stp_2 = preload("res://SFX/Render/stp_02.wav")
@onready var stp_3 = preload("res://SFX/Render/stp_03.wav")
@onready var stp_4 = preload("res://SFX/Render/stp_04.wav")

var steps : Array

func _ready():
	randomize()
	steps = [stp_1, stp_2, stp_3, stp_4]
	
func play_random_sound():
	var step_index = randi() % 4
	var step = steps[step_index]
	stepsPlayer.stream = step
	stepsPlayer.play()

The function I’m calling is the play random sound, but every time it gets triggered by the animation i get “Invalid get index ‘number’ (on base: ‘Array’).”

I tested by pasting the code directly to the player script and it worked, but not by calling it from that other script. I really have no clue what do.

Thanks in advanced for any help!

delete this and

try
@onready var steps : Array = [stp_1, stp_2, stp_3, stp_4]
from

Hey thanks for the reply!

So i did what you said, and still got the same error.

i tested your original code,
image

with following code:

extends Control
@onready var stepsPlayer = $steps
@onready var animation_player=$AnimationPlayer

@onready var stp_1 = preload("res://assets/audio/temp_bgm/Ingame1.ogg")
@onready var stp_2 = preload("res://assets/audio/temp_bgm/Ingame2.ogg")
@onready var stp_3 = preload("res://assets/audio/temp_bgm/Ingame4.ogg")
@onready var stp_4 = preload("res://assets/audio/temp_bgm/Ingame3.ogg")

var steps : Array

func _ready():
	randomize()
	steps = [stp_1, stp_2, stp_3, stp_4]
	play_random_sound()
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	animation_player.play("audioanimatoin")
	
func play_random_sound():
	var step_index = randi() % 4
	var step = steps[step_index]
	stepsPlayer.stream = step
	stepsPlayer.play()
	print("played random sound",step_index)

it prints and got no error on the indexing

how do you call it?

Hey! thanks again!

here is how I’m calling it:

func walk_sound():
	var frame = animated_sprite_2d.frame
	var animation = animated_sprite_2d.animation
	if animation == ("run"):
		match frame:
			5,11:
				randPlayer.play_random_sound()

func _on_animated_sprite_2d_frame_changed():
	walk_sound()

All of this is on my player script.

Let me try what you did there!

did you instantiated it well? or it’s already in the scene tree?

can try to put this in

change to this:

func _on_animated_sprite_2d_frame_changed():
	await get_tree().physics_frame
	walk_sound()

see if it solves the problem

Untitled

I’m super new to this so I think i did put it in the scene, in the screenshot, its the script in the “Node” item.

i also tried this, but nothing, it seems as its no having enough time to load what in the array.

await get_tree().physics_frame

if i see why it’s not working because yeah, @onready is too slow, and the _on_animated_sprite_2d_frame_changed() called too fast before the @onready of stp_1 to stp_2 ready, so when you call the variable steps, it’s not even has value of stp_1 yet because either the script to control step is not yet ready or the preload of stp_1 to stp_4 is not ready

Well managed by just using the AudioStreamRandomizer, couldn’t figure out how to do that other thing, but thanks for the help anyways! really appreciate it :+1: