๐ŸŽถ Coding the Theme Song | myBetabox

๐ŸŽถ Coding the Theme Song

Keyboard Guide

Coding the Theme Song

Create a Music Manager

Right-click the Audio folder in the FileSystem area and select New Scene. Rename this scene Music.

In the Scene tab, click Other Node. Select AudioStreamPlayer then click the Create button. You should now see your new Music scene in the Scene tab.

Double-click the AudioStreamPlayer object you just created and rename it to Music. This will help keep things tidy!

Add a new script so we can code our music by clicking the Add Script button and then the Create button.

Stuck? Copy & Paste the Code into Godot

extends AudioStreamPlayer

func play_song(music: AudioStream) -> void:
   stream = music

Now that we have an object that can play our music, we need to tell Godot that we want our music to be available all the time. Creating aย singleton is a command that tells Godot any time we play the background music, even if we want the game to play a different song after, the engine will know to play only one song at a time. Note that this isn’t for sound effects because we do want those to play while the background music is going on!

Click Project Settings from the Project menu.

Click the Autoload tab and then the folder icon.

In theย Audio folder, select the Music.tscn (Music scene) file.

Double check that the Node Name shows Music then click the Add button. You should see that the Singleton box is checked.

Creating a Script for the Level Music

Select MainScene in the FileSystem area then click the Add Script button in theย Scene tab.ย Click the Create button.

Edit the code (script) like in the video. Instead of typing out your music’s name and path yourself, you can use a feature called copy path! This allows you to simply copy and paste the information right into your script.

Right-click your theme song from the FileSystem area and selectย Copy Path from the menu.

Go back to your Script area by clicking Script view (found at the top of your window)…

…and paste between the quotation marks!

Stuck? Copy & Paste the Code into Godot

extends Node2D

func _ready():
   Music.play_song(load("INSERT YOUR MUSIC PATH HERE"))

Test It Out!

Save your progress then click play to see if your background music is working!