Godot Version
Godot 4.4
Question
I am trying to make a project where the game is only visible when a certain microphone volume is reached. With the code I currently have, the screen will continuously fade in and out. I know that this is because it is called under the “Process” function. The transition scene does have a signal that emits when the animation ends, that might be part of the solution. What would be the proper way to call the transition screen?
Here is the code for the main scene:
extends Node2D
var spectrum
var volume
var pitch
func _ready():
spectrum = AudioServer.get_bus_effect_instance(1, 2)
func _process(delta):
volume = (spectrum.get_magnitude_for_frequency_range(0, 1000).length())
if volume >= 0.04:
AudioServer.set_bus_mute(1, false)
TransitionScene.transition()
await TransitionScene.on_transition_finished
else:
AudioServer.set_bus_mute(1, true)
Here is the code for the transition scene:
extends CanvasLayer
signal on_transition_finished
@onready var color_rect: ColorRect = $ColorRect
@onready var animation_player: AnimationPlayer = $AnimationPlayer
func _ready() -> void:
color_rect.visible = false
animation_player.animation_finished.connect(_on_animation_finished)
func _on_animation_finished(anim_name):
if anim_name == "fade_to_black":
animation_player.play("fade_to_transparent")
emit_signal("on_transition_finished")
elif anim_name == "fade_to_transparent":
color_rect.visible
func transition():
color_rect.visible = true
animation_player.play("fade_to_black")