While not used much by Friday Night Funkin' (in exception to introText.txt), the ability to read and save to text files may be useful for some mods.
Reading a .txt file
To read a text file, you will not need any imports, by default funkin.Paths is imported into scripts, which is what we need to access text files.
The text file must exist to be read and have content in it to be read by the game. The text file will be read from the data folder of your mod.
Below is an example of how to read a text file.
import funkin.play.song.Song; class Tutorial extends Song { var data:String = ''; // This variable will contain the text when we read the text file. public function new() { super("bopeebo"); // This is the song name, it must be lowercase here. } function onSongLoaded(event:ScriptEvent):Void { super.onSongLoaded(event); data = Assets.getText(Paths.txt("Player")); // Txt file, ALWAYS in "data" folder. trace('text contents are ' + data); // Prints contents of text file in the console. } }
Saving to a .txt file
Unlike reading a text file, some more things will need to be imported to correctly save a text file. More specifically you will need to import the following:
funkin.util.FileUtilfunkin.util.FileWriteMode
This is needed to save text files. Like reading a text file, the text files MUST be in your data folder in your mod, and must already exist before attempting to save to it (the game will NOT create a text file if it does not already exist).
Below is an example of how to save a text file.
import funkin.play.song.Song; import funkin.util.FileUtil; // Required module to save text files import funkin.util.FileWriteMode; class Tutorial extends Song { public function new() { super("bopeebo"); // This must be the song name just lowercased } override public function onCreate():Void // This will be called upon the game starting { super.onCreate(); FileUtil.writeStringToPath(Assets.getPath(Paths.txt("Player")), 'extra large potato', FileWriteMode.Force); // Above will save to our mod's data folder to the text file named "Player.txt" and will write "extra large potato" in said text file } }
Checking if a text file exists
You may want to check if a text file exists before trying to read or write to it. To do this, we will still be using the same import used to save a text file, funkin.util.FileUtil, and will do the following:
import funkin.play.song.Song; import funkin.util.FileUtil; // Required module to save text files class Tutorial extends Song { public function new() { super("bopeebo"); // This must be the song name just lowercased } override public function onCreate():Void // This will be called upon the game starting { super.onCreate(); var checkForFile = FileUtil.pathExists(Assets.getPath(Paths.txt('Player'))); // We will be checking in the data folder of the mod this script is running in if (checkForFile == false) // This means the file does NOT exist { trace('File "Player.txt" does not exist in ' + Assets.getPath(Paths.txt('Player'))); // Along with printing the result, this will also tell you the path it expected the file to be in. } if (checkForFile == true) // This means the file does exist { trace('File "Player.txt" does exist in ' + Assets.getPath(Paths.txt('Player'))); } } }