|
| 1 | +[tags]: / "advanced,txt,haxe" |
| 2 | + |
| 3 | +# Text Files |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Reading a .txt file |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +Below is an example of how to read a text file. |
| 14 | + |
| 15 | +```haxe |
| 16 | +import funkin.play.song.Song; |
| 17 | +
|
| 18 | +class Tutorial extends Song { |
| 19 | + var data:String = ''; // This variable will contain the text when we read the text file. |
| 20 | + public function new() |
| 21 | + { |
| 22 | + super("bopeebo"); // This is the song name, it must be lowercase here. |
| 23 | + } |
| 24 | + |
| 25 | + function onSongLoaded(event:ScriptEvent):Void { |
| 26 | + super.onSongLoaded(event); |
| 27 | + data = Assets.getText(Paths.txt("Player")); // Txt file, ALWAYS in "data" folder. |
| 28 | + trace('text contents are ' + data); // Prints contents of text file in the console. |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Saving to a .txt file |
| 34 | + |
| 35 | +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: |
| 36 | +"-`funkin.util.FileUtil`" |
| 37 | + |
| 38 | +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). |
| 39 | + |
| 40 | +Below is an example of how to save a text file. |
| 41 | + |
| 42 | +```haxe |
| 43 | +import funkin.play.song.Song; |
| 44 | +import funkin.util.FileUtil; // Required module to save text files |
| 45 | +
|
| 46 | +class Tutorial extends Song { |
| 47 | + public function new() |
| 48 | + { |
| 49 | + super("bopeebo"); // This must be the song name just lowercased |
| 50 | + } |
| 51 | + |
| 52 | + override public function onCreate():Void // This will be called upon the game starting |
| 53 | + { |
| 54 | + super.onCreate(); |
| 55 | + FileUtil.writeStringToPath(Assets.getPath(Paths.txt("Player")), 'extra large potato'); |
| 56 | + // 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 |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +> Author: [Spark the Dragon](https://github.com/spark-the-dragon) |
0 commit comments