Skip to main content

Game Builder - Music Extension

This article will dive deeper into the Music extension, the Music tab and the music functions.

Written by Livnat Hershkovitz

The Music extension lets you compose music and play notes in your game, using real instruments. This extension is not available by default. To learn how to add it to your game, see Game Builder - Extensions.

The Music Tab

  • Tempo (BPM): how fast the music plays, in beats per minute. The default is 120, and it can be set from 40 to 500.

  • Add New: adds an instrument to the game. You can choose piano, guitar, electric guitar, flute, trumpet, violin or organ.

You can add more than one instrument. Like sprites, each instrument has a name and can have its own code. In an instrument's code, call its functions with @, like @playNote. In a sprite's code, use the instrument's name, like piano.playNote.

How Notes Are Played

Each instrument plays its notes one after the other. When your code plays a note, the code after it waits until the note ends, so a few playNote lines in a row play as a tune. Different instruments play at the same time, so you can play a melody on one instrument and chords on another.

Music functions

Function

Description

playNote notes, beats

Plays the notes in the list together, for the number of beats you set. A note is a letter and a number, like 'C4'. The letter is the note (A to G, add # for a sharp), and the number is the octave: the higher the number, the higher the note. Put one note in the list to play a single note, or a few notes to play a chord. If you leave out the beats, the note plays for 1 beat.

rest beats

Makes the instrument wait (play nothing) for the number of beats you set, before its next note.

setVolume percent

Sets the instrument's volume, from 0 (silent) to 100. The new volume is used from the next note.

Syntax

@playNote ['C4'], 1
@playNote ['C4', 'E4', 'G4'], 2
@rest 1
@setVolume 50

Example

To play a short tune when the player clicks a sprite, write this in the sprite's code. In this example the instrument is named piano.

@onClick = () =>
piano.playNote ['C4'], 1
piano.playNote ['E4'], 1
piano.playNote ['G4'], 1

In this example, written in an instrument's code, the instrument plays a C major chord for 2 beats when the game starts, waits 1 beat, and plays it again more quietly.

@playNote ['C4', 'E4', 'G4'], 2
@rest 1
@setVolume 30
@playNote ['C4', 'E4', 'G4'], 2

Relevant Articles:

Did this answer your question?