The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hi!

I have one Global Script SamplePlayer2D and some sounds.smp

and i search how to use SampleLibrary, get_sample, etc... but dont have succeed

How can i make a Sample Library and load samples to it with gdScript?

in Engine by (429 points)
edited by

I don't really understand your question. Do you want to have global sample player?
You can autoload (Project Settings > AutoLoad) scene that contains sample player (for example samples.tscn) and use it like this

samples.get_node("SamplePlayer2D").play("foo")

Thnx for the answer! Yeah, i now how do this :).
Let me explain better. Imagine that in this samples.tscn, you want to have a script to load the files, how do it? How to make a Sound Library(just samples, not stream) with gdscript?

1 Answer

+3 votes
Best answer
var sample = preload("res://sounds/sound.wav") # you can use load() instead

var lib = SampleLibrary.new()
lib.add_sample("name", sample)

var player = SamplePlayer2D.new()
player.set_sample_library(lib)

Related docs Sample | Sample Library | Sample Player 2D

by (373 points)
selected by

Hi. thanks for the answer.

This works for samples, now i have tried apply this for stream and have no success

extends StreamPlayer

var Song = load("res://Sounds/Jobro.ogg") # you can use load() instead
var player = StreamPlayer.new()


func _ready():
    player.set_stream(Song)
    player.play()

this runs fine, with an empty error on player.play() without any info...
i have tried some tutorials and things on the net but...
can you help me on this?

From your snippet I guess you attached script to StreamPlayer node thus your code should look like this:

extends StreamPlayer

var Song = load("res://Sounds/Jobro.ogg")

func _ready():
    set_stream(Song)
    play()

There is no need to create new StreamPlayer with var player = StreamPlayer.new() if you already added one using editor. You can create StreamPlayer from code like this:

extends Node # or anything else
var Song = load("res://Sounds/Jobro.ogg") 
var player = StreamPlayer.new()


func _ready():
    player.set_stream(Song)
    add_child(player)
    player.play()

thanks man! this works!

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 Frequently asked questions and 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 [email protected] with your username.