Directory access for user-generated extension

Godot Version

v4.7.stable.official

Question

I’m making a game which players can mod by adding external files to write their own stories. The game engine will iterate through an external directory and import relevant files. The directory structure will look like this:

/game.exe
/stories/story01/chapter01.tsv

Currently I’m using Timothy Qui’s CSV importer to format the external data files as tsv files. That part has been working fine. The problem is, all of the chapters I’ve written will be packed into my game’s PCK or EXE during export. This means the user won’t have any example files to copy. It also means that I don’t have a way of iterating through directories and importing tsv files after export. I eventually need the packed-in files and the external ones to all show up together in the Story Select screen.

So, here’s the real question. Within gdscript, when writing and testing my project, how do I distinguish between files that will be packed into the PCK after export, and files that will be found in directories alongside the exe/pck?

I have a similar challenge in my project, I want players to be able to use data I have prepared in the exported game and I also want them to be able to create their own data files.

I have chosen to copy the contents of my res://datafile folder to user://datafile when the game starts and from then on the player only accesses user://datafile where they read the supplied files and can also add their own.

This has helped because I can now create my master datafiles as a Player and validate that they function properly etc. and then simply copy/paste them into the Godot project folder ready for the next game export.

I do have some utility functions still to create such as a compare and update to refresh from the res:// folder with options to keep or overwrite anything the Player may have modified. My game content is not yet rich enough to worry about this so that task is a long way off yet :slight_smile:

If you want to export files next to your executable you can use an EditorExportPlugin for that. You can see an example here:

You can use FileAccess.file_exists() to find out if a file exists or not. It will return true for the files in your PCK though if you use res://. You can use ProjectSettings.globalize_path() to convert res:// paths to absolute paths.

solution could be to make it extension based (at least for windows) where you can name your main game’s files like “file.tsv” and for users “file.tsvu” because file extensions doesn’t really matter for reading, only for executing, so if you could make this importer read your custom extension the same as default one you can just say to players that they should use this extension for it to work

Okay, so I came up with a pretty solid solution. Below, you will see the general utility functions which I can use to implement the architecture I need. Basically, certain packed-in resources can be saved in the “user://” when the game is run for the first time. When the game is run a second time, any relevant files in that directory can be imported back in, where they will work alongside the packed-in resources.

func userFileSave(name:String, data):
	var fileName := "user://test/" + name
	var file = FileAccess.open(fileName, FileAccess.WRITE)
	file.store_string(str(data))
	file.close()
func userFileLoad(name:String):
	#name is expected to be a string ending in .dat
	#referencing a file in the user data folder
	var fileName := "user://test/" + name
	var defaultData = "default_data"
	if !FileAccess.file_exists(fileName):
		userFileSave(fileName, defaultData)
		return defaultData
	else:
		var file = FileAccess.open(fileName, FileAccess.READ)
		var data = JSON.parse_string(file.get_as_text())
		file.close()
		return data

func _ready() -> void:
	var testData = load( "res://story/story01/chapter01.tsv" ).records
	var dir = DirAccess.open("user://")
    dir.make_dir("testDirectory")
	userFileSave("testDirectory/test.txt", testData)
	print( "loaded data:" + str(userFileLoad("testDirectory/test.txt") ))

Please note that I briefly suffered from a vexing bug. I tried to create a directory called “user://test” but the directory already contained a text file called “test” (with no extension) from my previous experiment. This caused make_dir to think that “/test/” already existed. So, remember to always give your files extensions, even when they’re not necessary, and always give your directories completely unique names.

Also, be advised the that two “userFile” functions will have to be modified to handle situations where the specified file path refers to a directory that does not yet exist. I might have a separate function create all necessary directories first, so it won’t be a problem, or I might give those functions the ability to create their own directories when necessary.

This is basically the structure I’m going to use, although I’ll be using separate files and directories to make it more organized and scalable for the user. I’m still working on ironing out some technical issues though…