Problem storing CSV data using FileAcces.store_csv_line

Godot Version

4.3.dev3

Question

I want to store some properties about my procedural content generation in a CSV file so that I can analyze them.

I have this naive function for writing to a csv file. and I call it like so _save_to_csv([_rng.seed, _samples.size(), elapsed_time])

func _save_to_csv(data: Array) -> void:
	var file: FileAccess
	if not FileAccess.file_exists("res://csv/" + "pds.csv"):
		file = FileAccess.open("res://csv/" + "pds.csv", FileAccess.WRITE)
		file.store_csv_line(["seed", "samples", "time"])
	else:
		file = FileAccess.open("res://csv/" + "pds.csv", FileAccess.WRITE)
	
	var line := PackedStringArray()
	
	for entry: Variant in data:
		line.append(str(entry))
	
	file.store_csv_line(line)
	file.close()

I get loads of errors when I try to open the csv file in the FileSystem dock.

Also for some reason the store_csv_line function does not append to the csv file. Instead, it erases all previous content.

grafik

What am I doing wrong here?

Opening the file with FileAccess.WRITE truncates the file content. Use FileAccess.READ_WRITE instead.

1 Like

Thanks, I’ve tried it but I still get these errors…

func _save_to_csv(data: Array) -> void:
	var file: FileAccess
	if not FileAccess.file_exists("res://csv/" + "pds.csv"):
		file = FileAccess.open("res://csv/" + "pds.csv", FileAccess.WRITE_READ)
		file.store_csv_line(["seed", "samples", "time"])
	else:
		file = FileAccess.open("res://csv/" + "pds.csv", FileAccess.WRITE_READ)
	
	var line := PackedStringArray()
	
	for entry: Variant in data:
		line.append(str(entry))
	
	file.store_csv_line(line)
	file.close()
  (2) core/string/optimized_translation.cpp:136 - Condition "bucket_table_size == 0" is true.
  Failed loading resource: res://csv/pds.csv. Make sure resources have been imported by opening the project in the editor at least once.
  editor/editor_node.cpp:1213 - Condition "!res.is_valid()" is true. Returning: ERR_CANT_OPEN
  Failed loading resource: res://csv/pds.csv. Make sure resources have been imported by opening the project in the editor at least once.
  editor/editor_node.cpp:1213 - Condition "!res.is_valid()" is true. Returning: ERR_CANT_OPEN
  Failed loading resource: res://csv/pds.csv. Make sure resources have been imported by opening the project in the editor at least once.
  editor/editor_node.cpp:1213 - Condition "!res.is_valid()" is true. Returning: ERR_CANT_OPEN

Hmm, perhaps the res:// filesystem is read-only? Try to save the file to user://

1 Like

still, it’s replacing instead of appending…

You need to use READ_WRITE. From the documentation of ModeFlags.READ_WRITE

Opens the file for read and write operations. Does not truncate the file. The cursor is positioned at the beginning of the file.

To append data you’ll need to call FileAccess.seek_end() before writing more data.

1 Like

I have this code now but it’s still not working. It only stores the last line…

func _save_to_csv(data: Array) -> void:
	var file := FileAccess.open("user://" + "pds.csv", FileAccess.WRITE_READ)
	
	var line := PackedStringArray()
	
	for entry: Variant in data:
		line.append(str(entry))
	
	print(line)
	
	file.seek_end()
	file.store_csv_line(line)
	file.close()

That’s because you are storing one line.

extends Node


func _ready() -> void:
	var data = []
	for i in 20:
		data.push_back([str(randi()), str(randi()), str(randi())])

	var file:FileAccess
	if FileAccess.file_exists('user://test.csv'):
		file = FileAccess.open('user://test.csv', FileAccess.READ_WRITE)
	else:
		file = FileAccess.open('user://test.csv', FileAccess.WRITE)
		file.store_csv_line(["ID", "Value 1", "Value 2"])

	file.seek_end()

	for i in data.size():
		file.store_csv_line(data[i])

	file.close()
2 Likes

Sorry I didn’t clarify. I was calling the _save_to_csv after each iteration within the _generate_poisson_disk_samples function.

But I suppose this a better way of doing it anyways.

func _ready() -> void:
	if not Engine.is_editor_hint():
		_csv_data.append(["seed", "samples", "time"])
		
		for i in 10:
			await _generate_poisson_disk_samples()
		
		_save_to_csv()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.