0 votes

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)
Godot version v3.3
in Engine by (244 points)

1 Answer

+1 vote
Best answer

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.

by (2,442 points)
selected by

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.