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.
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