Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Luchs |
How can I disable loop by script, using c# ? I tried Loop and SetLoop(), but output the same error.
‘AudioStream’ does not contain a definition for ‘loop’ and no accessible extension method ‘loop’ accepting a first argument of type ‘AudioStream’ could be found (are you missing a using directive or an assembly reference?)
public class Coin : RigidBody2D
{
private AudioStreamPlayer audio;
public override void _Ready()
{
audio = GetNode<AudioStreamPlayer>("Sound");
/* tried those ways
audio.Stream.loop = false;
audio.Stream.loop(false);
audio.Stream.SetLoop(false);
audio.Stream.SetLoop = false;
*/
audio.Play(0);
}
On GDScript this code works:
func _ready():
$Audio.stream.loop = false
edit:
Meanwhile my question did checked, I’ve done.
If anyone has the same problem this is my the code:
using Godot;
using System;
public class FILE_NAME : NODE_TYPE
{
//This part get the file and configure it to Disable the loop
public AudioStream DisableLoop(AudioStreamSample file)
{
AudioStreamPlayer audio = new AudioStreamPlayer();
file.LoopMode = Godot.AudioStreamSample.LoopModeEnum.Disabled;
audio.Stream = file;
return audio.Stream;
}
//This part is for play the sound
//Wav file or any accepted
[Export] AudioStreamSample data = null;
public override void _Ready()
{
//Change "AudioStreamPlayer" to audio node name
AudioStreamPlayer audioPlayer = GetNode<AudioStreamPlayer>("AudioStreamPlayer");
audioPlayer.Stream = DisableLoop(data);
audioPlayer.Play(0);
}
}