Level 17 Lesson 02 [ text File writing/reading ]

Introduction
In the last lesson you learned how to use .ini files to save data in a structured way with key-value pairs. In this lesson you will learn how to use text files to save data. When using text files YOU decide how the file is written. There are no sections and key names. You simply write data to the file line by line. When you read the data from the file you go line by line. The order that you wrote data will be the order that you read data from the file. Text files require a little more organization than .ini files but are more versatile. Text files can be much larger than .ini files and you can have up to 32 text files open at once during your game.


Part A – Text File Basics

Text files make use of simple commands to write and read from the file. In this example you will be introduced to the read and write commands, what the text files look like, and the general logic you will use when deciding how to organize your text files.

Watch the video: 17-02-A
Done File: 17-01-A-Done.gmk

Part B – Text Files and readln() Caution!
The readln() method will read until the end of the current line on a text file and then move to the start of the next line. But beware the string that this method returns – it contains extra key characters representing the newline/carriage return. Must watch!

Watch the video: 17-02-B
Done File: 17-02-B-Done.gmk

 

Challenge 01 – Save and Load User Preferences
Start with the project from part B: 17-02-B-Done.gmk

Practice saving and loading the difficulty level, points, hp, and difficulty text.
When the user presses the save button, save these four pieces of information in the following format in a text file:

line 1: difficulty level points hp
line 2: difficultyText

So the actual text file might look like this:

2 123 50
easy

When the load button is pressed your program should read the data from the text file and you should see the values on the screen (the drawing is already coded for you in the project). *For testing, you can hit the “R” key to randomly set some of the values and then press load to see if the saved values are properly set back.

    Solution Video: 17-02-X1-Solution
    Solution File: 17-02-X1-Solution.gmk

Part C – Text Files with Lists,Maps, Grids
In the previous lesson you learned how to save lists, maps, and grids into an .ini file. In this part you will see how the same tasks can be done using a text file. The main idea of this part is that the entire list string is saved on one single line of the text file, regardless of how much data is contained.

Watch the video: 17-02-C
Done File: 17-02-C-Done.gmk

Part D – Detecting the End of a File
Sometimes when you read a text file you don’t know how many lines are actually in the file. In this video the example used is loading the types and locations of objects in the room. There could be 50 units, there could be 1000. How does your program know when to stop reading the file? You keep reading a carefully formatted text file until you hit the end of the file. The eof() method will help you do this.

Watch the video: 17-02-D
Done File: 17-02-D-Done.gmk