Wiki

Clone wiki

NuclearThroneTogether / Scripting / API / File

Things are not quite as they seem with file functions in modding.

The general concept is that files on disk have to be explicitly loaded by the mod before you can do anything with them. In local modes, it's just a 1 frame delay.

In online multiplayer, the file(s) also have to be transmitted to the co-player so that performed actions are the same, which means more delay before loading. Modified files are kept on the machine of player that loaded the mod.

Modified files are saved into a special "data" directory created per-mod.

When loading, the priority of picking files is as following (from first tried to last tried):

  • Mod's data directory (previously created\modified files)

  • Directory in which mod's .gml file is located.

  • Game's appdata directory.

  • Game's installation directory.

file_load(...paths)

Starts loading the given path(s).

Paths can be either actual paths, or arrays of them.

Returns the number of frames until the files will be loaded.

If the files do not exist, they still can to be "loaded", which in online multiplayer simply informs the other player that the file(s) indeed are amiss.

The following will preload a text file called "test.txt" and display it's contents once that is done:

wait file_load("test.txt");
trace("test.txt:", string_load("test.txt"));

file_unload(...paths)

Unloads the given files from memory.

This is instantaneous and returns nothing.

file_loaded(path:string)

Returns whether the file by given path is loaded (regardless of whether it exists).

file_exists(path:string)

Returns whether the file by given path is loaded and exists.

file_delete(path:string)

Unloads a file from memory and removes it from disk (on machine of player that loaded the mod).

file_download(url:string, path:string)

Initiates downloading of a file from the internet to the given location.

The function returns nothing and makes no promises as to when and whether operation will complete - use file_loaded to find when it loads.

The file is downloaded from original URL by the player that loaded the mod - in online multiplayer, the other player will be sent the mod-loading-player's copy of it once it loads.

The following will download a file and display it's contents:

file_download("http://yal.cc/ping", "ping.txt");
while (!file_loaded("ping.txt")) wait 1;
trace("Unix time at Yellow's website:", string_load("ping.txt"));

string_load(path:string)

Loads and returns the contents of given file as a string.

Returns undefined if file does not exist.

string_save(:string, path:string)

Saves the contents of a string to the given file.

The following example will add one "hi" to "test.txt" upon each execution:

wait file_load("test.txt");
var s = string_load("test.txt");
if (s == undefined) s = "hi"; else s += "hi";
trace("text: " + s);
string_save(s, "test.txt");

Updated