Audio not updating ingame

Godot Version

4.2.1

Question

I am using an audio file to save and load audio from and it doesn’t update while running the game then using copy_absolute to save, rerunning the game fixes the audio but I want it to load it ingame. How do I update the file whilst the game is running?

$AudioStreamPlayer2D.stream = load("<path>")
$AudioStreamPlayer2D.play()

I used the code and still doesnt work

Can you share the code for the functions connected to your buttons? Also, is there a reason you click on the button “Load Map” instead of “Load Song”? :thinking:

I forgot to change the name. also here is the code that does the loading

extends Control

var map
var song
var bpm = 120
var map_name := "Song Name"
@export var main : Node2D
var editor_dir = DirAccess.open("res://editor/")
# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

func load_map(map_):
	if not map_:
		map = load("res://scenes/song.tscn").instantiate()
		if has_node("Song"):
			$Song.queue_free()
		add_child(map)


func _on_file_dialog_file_selected(path):
	DirAccess.copy_absolute(path,"res://editor/song.mp3")
	$SongProp.visible = false
	$Pan/SongProp.disabled = false
	$Pan/Play.disabled = false
	main.load_song("res://editor/song.mp3")
	main.audio_update()
	load_map(null)


func _on_song_name_text_changed(new_text):
	map.song_name = new_text

func _on_artist_name_text_changed(new_text):
	map.artist = new_text

func _on_spin_box_value_changed(value):
	map.bpm = value
	main.bpm = value
	main.audio_update()

func _on_mapper_name_text_changed(new_text):
	map.mapper = new_text


func _on_play_button_down():
	pass # Replace with function body.

Copying the song file to a mass storage device every time a song is selected seems like a terrible idea, and saving data to “res://” doesn’t even work in an exported project. You could just use main.load_song(path) directly.

Anyway, the reason why the song didn’t change is that the song file is cached to RAM, and calling load() with default parameters reads the cached data even if the file has changed. In the documentation you can see, that the load function has a cache mode parameter, and the default behaviour is to reuse cached data.

2 Likes