#I need help with sounds

1 messages · Page 1 of 1 (latest)

cerulean matrix
#

I made AudioStreamPlayer3D. It works perfectly in the editor but not when i run the game.

ruby carbon
#

What's the problem? It just doesn't play the sound?

#

How are you playing the sound - autoplay? Or through code?

cerulean matrix
#

trough code. in the ready loop i start playing it, then in another loop i just set it to be playing or not.

ruby carbon
#

Mind sharing?

cerulean matrix
#
using Godot;
using System;

public partial class SPAnimator : Node3D
{
    AnimationPlayer ap;
    [Export] Node3D[] lights;
    RandomNumberGenerator rand = new RandomNumberGenerator();

    float counter;
    float rid;
    bool swich;
    bool lightSwitch;
    bool prevLightSwitch;

    public override void _Ready()
    {
        ap = (AnimationPlayer)GetChild(0);
        rand.InitRef();
        rid = rand.RandfRange(0.2f, 5);
        ((AudioStreamPlayer3D)lights[0]).Play();
    }
    
    public override void _Process(double delta)
    {
        base._Process(delta);

        if (!ap.IsPlaying())
            swich = !swich;

        if (!swich) ap.Play("Sway-loop");
        else ap.PlayBackwards("Sway-loop");

        if(counter >= rid || counter  >= 5)
        {
            counter = 0;
            rid = rand.RandfRange(0.2f, 5);
            lightSwitch = !lightSwitch;
        }
        else counter += (float)delta;

        if(lightSwitch != prevLightSwitch)
        {
            ((AudioStreamPlayer3D)lights[0]).Playing = lightSwitch;
            for (int i = 1;  i < lights.Length; i++)
                lights[i].Visible = lightSwitch;
        }

        prevLightSwitch = lightSwitch;
    }
}
#

in code lights[0] is the audioPlayer

ruby carbon
#

Thank you, and to clarify, the problem is that the sound doesn't play at all?

cerulean matrix
#

yes. when in play mode.

ruby carbon
#

If the AudioStreamPlayer3D node is not visible in the scene tree, it won't play. I wonder if that could be what's happening.

cerulean matrix
#

if i play the sound in the inspector is works

ruby carbon
#

I see that you have added it to the lights array

#

so maybe its Visible property gets set false

cerulean matrix
#

ok. ill check that.

cerulean matrix
#

no dice.