Skip to reading
BASIC Scripting

Read and write a text file

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page

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.

Code example
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 FUNCTION

On 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.

Source table from Read and write a text file, page 22
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 failedWriteTextFile 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.
Page 22 of 86Permalink
Go to page