Open And Read External .TXT File

Published 2023-06-12

There are not too many string-handling libraries I have come across in Pico-8. And for visual text this is not too surprising considering the small-size of the screen. :)

However, the point of this cart is to demonstrate a few things. One of which is that it is indeed possible to load a text ".txt" file inside your program. Now unfortunately the TXT file's contents cannot be changed once loaded. Like if you made a loop to view the contents each time.

But this does free you to use any other editor like NOTEPAD to prepare text data inside it.

For instance, you will need this. Highlight all of it, press CTRL-C. Bring up NOTEPAD. Press CTRL-V. Then save it on your HD as YUKON.TXT in a place you normally save your PICO-8 carts.

Notice it has correct uppercase and lowercase letters. If you want to make your own text file to use the libraries in this cart be certain to make your TXT file exactly the same way. With a variable "=" "[[" a skipped line, the data, another skipped line, and "]]" to finish followed by a carriage-return.


THIS CART WILL NOT RUN IN THE BBS

Alright, now load up this cart direct in PICO-8's editor.

The reason is this cart will NOT run either in Lexaloffle BBS or even compiled as an EXE - unless you have the TXT file alongside the EXE. And that could be a problem that needs to be resolved by ZEP. In any case, this =WILL= work in the editor once again provided you have the TXT file separate.

Now once you have the cart loaded, change to the directory where your TXT file is and SAVE your program right then in the same directory. Any name will do, but it must be saved in the same directory as the TXT file so PICO will know where to find it.

You will receive a warning that, "COULD NOT #INCLUDE FILE: YUKON.TXT" even though it's right there, but the file will have saved properly.

NOW IT WILL RUN. Run the cart.

Notice I am using a new function called PRINTS(). The reason is PICO-8 messes up the display if you use PRINT for any string that has \10 (CR) in it, and PICO-8 also has no internal word-wrap for strings. You must instead print out each character manually and handle CRs through code.

The advantage here also is now you can change the TXT file anytime you want and the results are immediately seen in your cart without the need to save it each time.

And that's it ! If you have any questions or difficulties, please let me know.

I hope you find this library useful for all of your import and string-handling needs.