Skip to content

Instantly share code, notes, and snippets.

@starry-abyss
Created January 7, 2024 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starry-abyss/0ca464206b560080b3812188879ce6cb to your computer and use it in GitHub Desktop.
Save starry-abyss/0ca464206b560080b3812188879ce6cb to your computer and use it in GitHub Desktop.
Script for simple playing of some sounds (tested in Godot 3.5)
var audioplayers = []
var audioPlayerHacking
var randomPitch = {}
var volume = {}
func play(soundName):
var audioplayer = null
var index = -1
for i in range(audioplayers.size()):
if !audioplayers[i].playing:
audioplayer = audioplayers[i]
index = i
break
if audioplayer == null:
return
if randomPitch.has(soundName):
if randomPitch[soundName] == null:
randomPitch[soundName] = AudioStreamRandomPitch.new()
randomPitch[soundName].set_random_pitch(1.5)
randomPitch[soundName].set_audio_stream(load("res://sounds/" + soundName + ".wav"))
audioplayer.set_stream(randomPitch[soundName])
else:
audioplayer.set_stream(load("res://sounds/" + soundName + ".wav"))
if volume.has(soundName):
setBusVolume(index, volume[soundName])
else:
setBusVolume(index, 0.0)
audioplayer.play()
func setBusVolume(i, volume):
AudioServer.set_bus_volume_db(i + 1, volume)
func _ready():
for i in range(4):
var audioplayer = AudioStreamPlayer.new()
audioplayer.bus = AudioServer.get_bus_name(i + 1)
audioplayers.append(audioplayer)
get_node("/root/ui").add_child(audioplayer)
audioPlayerHacking = AudioStreamPlayer.new()
audioPlayerHacking.set_stream(load("res://sounds/win2.wav"))
get_node("/root/ui").add_child(audioPlayerHacking)
randomPitch["data"] = null
randomPitch["enemy_move2"] = null
randomPitch["lose"] = null
volume["ufo_logo"] = 15.0
volume["win3"] = -4.0
volume["level_enter3"] = -2.0
volume["enemy_move2"] = 0.0
volume["enemy_move4"] = -1.0
volume["lose"] = 3.0
volume["data"] = 15.0
volume["data3"] = -6.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment