Multiple BGM's Playing one after another
Asked Answered
D

3

0

If I want my game to play through a set of BGM's how does one go about impleminting that? There is still soo much about Godot that I don't know. I've tried creating a node which has a set of it but it I don't have any idea on making it work.

Is there another tutorial on the matter I can look up as a reference? At the moment I have found no Godot 4.1 tutorials for this.

Project folder:
https://workupload.com/file/grkGsvmatYm

I went back after seeing some tutorials. The tutorials sadly are outdated and not for Godot 4.1

Delorsedelos answered 26/9, 2023 at 23:20 Comment(0)
S
1

If I understand you correctly you want to play background music (multiple files) one after another, is that correct? This can be accomplished with a number of ways.
Here's one way:

extends Node


var audioPlayers : Array     # list of our audio players
var current_player : int = 0 # keep track of which player is in use

func _ready():
	if _find_audio_players():
		if _connect_audio_players_to_finished():
			play_audio(current_player)

func _find_audio_players() -> bool:
	var found_at_least_one := 0
	for c in get_children():
		if c is AudioStreamPlayer:
			audioPlayers.push_back(c)
			found_at_least_one += 1
	return found_at_least_one > 0

# connect each player's finished signal to the finished func 
func _connect_audio_players_to_finished() -> bool:
	var all_good := true
	for ap in audioPlayers:
		if ap is AudioStreamPlayer:
			all_good = ap.finished.connect(_song_finished) == OK
		if not all_good:
			return false
	return all_good

# player next one is finished
func _song_finished():
	current_player += 1
	if current_player >= audioPlayers.size():
		current_player = 0
	play_audio(current_player)


func play_audio(player : int):
	var c = audioPlayers[player]
	if c is AudioStreamPlayer:
		c.play()
Schism answered 27/9, 2023 at 6:58 Comment(0)
D
0

Schism Thank you, that method worked for me.
Is there any cons to using this method? Will it work well when you have many BGMs?

Delorsedelos answered 27/9, 2023 at 18:25 Comment(0)
S
1

Delorsedelos This will work so long as you keep the players as children of that node with script- even after adding more audio players beneath it. If you have more than 20 though I would worry about it causing the game to hang for quite awhile when opening. Hanging could be dealt with by loading audio i + 2 after audio i has finished playing then only starting the scene with audio 0 and 1. That would require more code though.

Hopefully if you're new that code isn't too hard to make sense of.

Schism answered 27/9, 2023 at 19:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.