Skip to content

How the Music Controller & Script work

DuncanSzabaga edited this page Nov 17, 2025 · 4 revisions

The music controller and script are designed for the following purposes

  • Play music on game start
  • Switch from an initial track to a loop track when a song ends
  • Switch from the current song to the new song whenever the player moves to a screen with a new song
  • Allow for easy modification when a new song is added to the game

MusicManager Script

Image

This script creates a global struct called "bgm" with the following variables

  • priority: Priority of the music, only matters for resource culling.
  • fadeout_ms: How long it takes a song to fade out in milliseconds.
  • fadein_ms: How long it takes a song to fade in in milliseconds.
  • tracks: A struct that stores which tracks are associated with each room. If a room is not added to the struct then a song change doesn't occur.
    • start: The song that initially plays in the room.
    • loop: The song that plays on loop in the room.
  • playing: The sound id for the currently playing bgm, manipulated by the controller.
  • track_index: Stores the id of the playing track.
  • pending_index: Stores the id of the track that is queued to play next.

When a song is added to the game and needs a room to be assigned to, copy the existing struct_set information, and adjust it to match the room it needs to be assigned to and what the start and loop track names are.


obj_musc_controller Object

Alarm 0 Event

Image

Here is how the Alarm 0 Event operates after it gets triggered

  • Check if there is a song queued in the pending_index.
  • If a song is currently playing, stop the current song.
  • The variable _track becomes the struct information for the pending room.
  • playing gets assigned to the start track with its relevant extra information, starting off muted.
  • The gain of the playing track is increased to the music_volume value for the length of fadein_ms.
  • track_index is updated to the track that was in the queue.
  • pending_index is updated to no longer hold a track.

This gets called when a song is switched.

Room Start Event

Image

Here is how the Room Start Event operates after it gets triggered

  • The variable _roomname is equal to the name of the room.
  • The variable _expected is assigned to either the track_index or the pending_index based on if the pending_index is undefined or not, respectively.
  • If the room switched to is defined in the tracks struct and the room name does not equal the expected value, continue forward.
  • The current room is queued into the pending_index.
  • If a song is not currently playing, trigger the alarm.
  • If a song is currently playing, begin the fade out of the current song and trigger the alarm.

Triggers on room switch, only really matters when a room has a different song than the song currently playing.

Async - Audio Playback Ended Event

Image

Here is how the Async - Audio Playback Ended Event operates after it gets triggered

  • The variable _sound_id is equal to the id of the sound that ended.
  • The variable _was_stopped is true or false depending on if the audio stopped naturally or was forced to stop.
  • The following four conditions are checked, if true then it moves forward. This code is used only when switching to the loop track.
    • Is the _sound_id variable equal to the song that was playing?
    • Did the audio end naturally or was it forced to stop?
    • Is the pending_index undefined or not?
    • Is the song not currently playing?
  • The variable _track is set to the correct track information for the room it's in
  • The playing audio is set to the loop track of the correct song

This should only activate when a song needs to change from the start track to the loop track.

Clone this wiki locally