0 votes

extends AudioStreamPlayer2D

var coinSFX = preload("res://GodotIntro-Assets/Audio/coin.tres")

func playcoinsfx ():

stream = coinSFX
play()

This is the code for the coin sound effect in my game. It's supposed to play when the playable character touches the coin. It's not showing any error messages, but I still can't hear any sound when the character touches the coin? The only things that happens for me is that, theres either no sound effects, or it keeps looping. Can you guys please help?

in Engine by (12 points)

I don't think a .tres file can be used as a sound?!

1 Answer

0 votes

Here is the code for this:

func shooteffect():
    var shooteffect = AudioStreamPlayer3D.new()
    add_child(shooteffect, true)
    var ko : Array = ["res://Voices/BangFireWork/bang/bang_01.wav", 
    "res://Voices/BangFireWork/bang/bang_02.wav",
    "res://Voices/BangFireWork/bang/bang_03.wav",
    "res://Voices/BangFireWork/bang/bang_04.wav",
    "res://Voices/BangFireWork/bang/bang_05.wav",
    "res://Voices/BangFireWork/bang/bang_06.wav",
    "res://Voices/BangFireWork/bang/bang_07.wav",
    "res://Voices/BangFireWork/bang/bang_08.wav",
    "res://Voices/BangFireWork/bang/bang_09.wav",
    "res://Voices/BangFireWork/bang/bang_10.wav"]
    randomize()
    ko.shuffle()
    shooteffect.stream = load(str(ko.front()))
#   shooteffect.stream
    shooteffect.play()
    shooteffect.playing = true
    shooteffect.unit_db = Global_batch.player_fx_level
#   print("Global_batch.player_fx_level", Global_batch.player_fx_level)
    yield(get_tree().create_timer(0.5), "timeout")
    shooteffect.queue_free()
    shooteffect.free()

We assume that you have audiostreamer in tres file as the main root Dear Sir / madam

by (193 points)

Yes, Im using the Coin.tres file in the Audio Player.

But my game isn't a 3D FPS game though.

Pleaase use direct var shooteffect = AudioStreamPlayer2D.new() code, not the tres file format

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 Frequently asked questions and 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 [email protected] with your username.