0
0
Unityframework~10 mins

File-based save system in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - File-based save system
Start Save Request
Gather Data to Save
Convert Data to String
Open/Create File
Write Data to File
Close File
Save Complete
Start Load Request
Open File
Read Data from File
Convert String to Data
Apply Loaded Data
Load Complete
This flow shows how a game saves data by converting it to a string and writing it to a file, then later reads the file and converts the string back to data.
Execution Sample
Unity
using System.IO;

void SaveScore(int score) {
  string path = "savefile.txt";
  File.WriteAllText(path, score.ToString());
}

int LoadScore() {
  string path = "savefile.txt";
  return int.Parse(File.ReadAllText(path));
}
This code saves an integer score to a text file and loads it back by reading the file and converting the text to an integer.
Execution Table
StepActionData/VariableFile ContentResult/Output
1Call SaveScore(42)score=42Start saving
2Convert score to stringscoreStr="42"Ready to write
3Write string to filescoreStr="42"File contains: "42"Data saved
4Close fileFile contains: "42"Save complete
5Call LoadScore()File contains: "42"Start loading
6Read string from fileFile contains: "42"Read "42"
7Convert string to intloadedScore=42File contains: "42"Data loaded
8Return loadedScoreloadedScore=42File contains: "42"Load complete, returns 42
💡 Execution stops after saving and loading complete successfully.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 7Final
scoreundefined42424242
scoreStrundefined"42""42""42""42"
loadedScoreundefinedundefinedundefined4242
File Content"""""42""42""42"
Key Moments - 3 Insights
Why do we convert the score to a string before saving?
Files store text, so we convert numbers to strings to save them. See execution_table step 2 where score 42 becomes "42".
What happens if the file does not exist when loading?
Reading a non-existent file causes an error. The code assumes the file exists (execution_table step 6). In real use, check file existence before reading.
Why do we parse the string back to int when loading?
Data read from files is text. To use it as a number, we convert it back. See execution_table step 7 where "42" becomes 42.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the file content after step 3?
A"int.Parse(42)"
B"score=42"
C"42"
D""
💡 Hint
Check the 'File Content' column at step 3 in execution_table.
At which step does the program convert the loaded string back to an integer?
AStep 7
BStep 5
CStep 6
DStep 8
💡 Hint
Look for 'Convert string to int' action in execution_table.
If we change SaveScore to save "score:42" instead of "42", what changes in the execution_table?
ANo change in file content
BFile content at step 3 becomes "score:42"
CLoaded score at step 7 becomes "score:42"
DProgram crashes at step 2
💡 Hint
Focus on the 'File Content' column at step 3 and how SaveScore writes data.
Concept Snapshot
File-based save system in Unity:
- Convert data to string (e.g., score.ToString())
- Use File.WriteAllText(path, data) to save
- Use File.ReadAllText(path) to load
- Convert loaded string back to data type (e.g., int.Parse)
- Always handle file existence and errors in real projects
Full Transcript
This visual execution shows how a file-based save system works in Unity. First, the program converts the data (like a score) to a string because files store text. Then it writes this string to a file. Later, when loading, it reads the string from the file and converts it back to the original data type. The execution table traces each step, showing variable values and file content. Key moments explain why conversion is needed and what happens if the file is missing. The quiz tests understanding of these steps. This method helps games remember player progress between sessions.