Read and write a text file
BASIC IN YOUR GAME
Suppose you want a journal to keep every entry. Writing a file replaces its contents, so read the old text first and add the new line to it.
FUNCTION Run()
DIM path = "user://station/journal.txt"
DIM previous = ReadTextFile(path, "")
DIM saved = WriteTextFile(path, previous & "Bell heard.\n")
IF NOT saved THEN
RETURN "Write failed"
END IF
RETURN ReadTextFile(path, "")
END FUNCTIONOn the first run, ReadTextFile uses the empty fallback because there is no journal yet. WriteTextFile creates the folder if needed and writes one line. Run it again and the returned text contains two lines.
| user:// | The game’s writable data location. PCA3D resolves it for the device. A Windows drive letter or a Linux home-directory name would tie the code to one machine. |
|---|---|
| Write failed | WriteTextFile returns a Boolean. Check it before telling the player that something was saved. |
| A single save slot? | This path is shared by every slot using it. Put story progress in saveable game state, or deliberately give each slot its own file path. |
