+1 vote

Noob question Godot 3!! How to set the stream from the AudioStreamPlayer2D? I've tried set it directly, but debugger say, !pstream.isvalid() is true, that means I set the wrong value.

Here is what I've done

export(AudioStream) var audio_sfx

func somefunction():
    sfx.stream = audio_sfx

This code will produce !pstream.isvalid() is true. Any idea?

in Engine by (13 points)

2 Answers

0 votes

This worked for me with AudioStreamPlayer, it might work with AudioStreamPlayer2D?

var speech_player = AudioStreamPlayer.new()
var audio_file = "res://Audio/(filename).ogg"
if File.new().file_exists(audio_file):
    var sfx = load(audio_file) 
    speech_player.stream = sfx
by (204 points)

Yes I know if load / preload will works flowless. But, I want to make it "inspector-able", so each node can have different sound.

0 votes

So, I'm pretty sure the reason this happens is that when Godot instantiates your scene it also needs to instantiate your AudioStreamPlayer sfx, which throws this error, because at this point the stream property of sfx is still null. To offer a solution I think we would need more code/context of what exactly you're trying to do.

My guess is, that you should rethink your approach though. E.g. what about just adding an AudioStreamPlayer node to your scene? And then, when you instantiate that scene, you can change the Player's stream property in the editor.

by (14 points)

Sadly, that was not the case. That was my first scene, no instantiate. I don't know if this has been fixed or not. AFAIK, I want to achieve reusable AudioStreamPlayer, instead of using a lot of AudioStreamPlayer, I can use only one AudioStreamPlayer and change the stream (clip).

The sfx is not null, I've tried to check it with print(sfx)

this might be easy fix with this issues, just load at _ready(), something like
audio_sfx = load(audio_sfx.resource_path)
Well, I haven't tried it anyway, haven't in touch with that again.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.