• General Chat
  • Loading external files after the project is exported

Hello! I'm working on a project where my data is stored in CSV files, as it is convenient to edit. I'm not using the CSV files as localization files, but as files containing data (strings) used in the game. I reimported the CSV file I use as a test as "keep file" and not "CSV translation", and there's a red cross in its right (which is weird because I change my version of Godot today and I believe in the previous version I had I could reimport the CSV file as something else and there was no red cross if I remember correctly).

I created a little method to read a CSV file and print the lines it contains. Here is the method:

# Read a CSV file and print all its lines
func read_csv(pFilepath):
	var file = File.new()
	file.open(pFilepath, file.READ)
	while !file.eof_reached():
		var line_array = file.get_csv_line(";")
		for line in line_array:
			print("---------------------")
			print(line)
	file.close()

It all works perfectly fine in the editor. The problem is that once the project is exported as an application (currently on macOS), or if I try to play it using an HTML5 application, the program is unable to process this method, and I think this is because once compiled the program cannot access external files. I already had the problem on another project with txt files instead of CSV files. I ended up copy pasting what the txt contained in a script inside my game, but this is not possible for this project as it will contain a lot of CSV files and it will not be convenient to do it this way. I was also wondering how I'm supposed to handle external files if I want to make an iOS and android build of my game too. Like, is it possible to handle the external files the same way and keep just one project or should I have to create different versions with different code? What about the HTLM5 version of my game (if I want one)?

There's definitely something I'm doing wrong regarding the use of external files (like CSV but also txt or even a custom file for example). I red the documentation again but was unable to find any clue.

Thanks for the help.

So it is possible to load external files dynamically (provided they are in a special folder, at least on desktop platforms) but it's not a good idea. What you want to do is embed the .csv files into the .pck file. First import the .csv files into your project folder, you'll have to select "keep" or something so they are not interpreted as localization files. In the export settings, click the second tab and you will be able to add additional (non-Godot) files into the export package. You can enter "*.csv" to compress all of them. Then when you load in code, you load it as a resource with the project path, such as "res:://data/my_data.csv" or something like that. This works fine and should be supported on all platforms, including mobile and HTML5.

7 days later

Ok I actually have a complementary question. Now that I know the best practice regarding the process of loading external files, I wonder a lot what is the best practice regarding the process of saving external data (in the form of files I guess).

Basically, if my save system is made of a system that create one or several files and save data into them to load them later, is that system gonna work no matter if the application run on iOS or android ? What about HTML5? Can I use the same save system for all of them (if of course I am carefull about not to have too heavy data, especially on mobile), or do I have to create different save system depending on the plateform the game is currently be played?

Thanks for the answer!