Skip to content

Commit 5791e66

Browse files
Update 07.UsingTextFiles.md
Added tutorial stuff for checking if a text file exists
1 parent 8b616b6 commit 5791e66

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

assets/content/cookbook/Advanced/07.UsingTextFiles.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
[tags]: / "advanced,txt,hscript"
1+
[tags]: / "advanced,txt,haxe"
22

3-
# Using Text Files
3+
# Text Files
44

55
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.
66

@@ -58,4 +58,34 @@ class Tutorial extends Song {
5858
}
5959
```
6060

61+
## Checking if a text file exists
62+
63+
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:
64+
65+
```haxe
66+
import funkin.play.song.Song;
67+
import funkin.util.FileUtil; // Required module to save text files
68+
69+
class Tutorial extends Song {
70+
public function new()
71+
{
72+
super("bopeebo"); // This must be the song name just lowercased
73+
}
74+
75+
override public function onCreate():Void // This will be called upon the game starting
76+
{
77+
super.onCreate();
78+
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
79+
if (checkForFile == false) // This means the file does NOT exist
80+
{
81+
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.
82+
}
83+
if (checkForFile == true) // This means the file does exist
84+
{
85+
trace('File "Player.txt" does exist in ' + Assets.getPath(Paths.txt('Player')));
86+
}
87+
}
88+
}
89+
```
90+
6191
> Author: [Spark the Dragon](https://github.com/spark-the-dragon)

0 commit comments

Comments
 (0)