What is the correct way to replace the name for the sound

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Idleman

Hello, I have a little problem here. The idea is that to have an area2d to detect player enter, then play a sound. So If I instance the area2d scene as a child of any node, the node will plays a sound once player enters.

The sounds are preload in a global AudioManager script, I first get the editor_description of the parent node. Then match the description to the sound in a list, then play the match name of the sound from the AudioManager. Things are fine until the last step that I can not seem to inject the variable of the name of the sound to the play function. It gives me a expected identifier as member error. Is that a correct way to input the name of sound into the script? Thank you.

extends Area2D

var soundName = ""

func _ready():
	soundName = get_parent().editor_description


func play_sound(inputName):
	AudioManager.play_sound(AudioManager.entitySounds.(inputName), 0)


func _on_SoundTrigger_body_entered(body):
	if body.name == "Player":
		play_sound(soundName)
:bust_in_silhouette: Reply From: Lopy

Assuming that AudioManager.entitySounds is a Dictionary, you would access it’s values with AudioManager.entitySounds[inputName]. The “.” syntax for Dictionary is only for convenience and things you have the name when writing.

Objects have a get(property: String) function instead.

Thank you very much for answering my question, I have spent hours to get a work around by setting up a for loop and matching with the value in the dictionary. With your help, now the process is much more efficient.

Idleman | 2021-07-22 15:02