How Build a SampleLibrary in a global script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tiernich
:warning: Old Version Published before Godot 3 was released.

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?

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")

Daniel Lewan | 2016-05-25 09:04

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?

tiernich | 2016-05-25 13:27

:bust_in_silhouette: Reply From: Daniel Lewan
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

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?

tiernich | 2016-05-28 14:36

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()

Daniel Lewan | 2016-05-28 22:29

thanks man! this works!

tiernich | 2016-05-29 04:09