Godot Version
4.2.1
Question
When the game starts, it will check the save data. If an error is detected (The array is not the same, wrong variable type, etc), it will delete the data to avoid it being used.
How do I write that?
This is my script that I have written in GD4. Right now, I only return if the array of time records are not the same.
extends Node
const GAMEDATA_FILE_PATH = "user://game_data.save"
var has_played: bool = false
var last_level: int = 0
var unlocked_level: int = 0
var time_records: Array[float] = []
#func _ready() -> void:
#create_time_records()
func create_time_records() -> void:
if not SceneManager.levels:
return
for i in len(SceneManager.levels):
time_records.append(0.0)
load_data()
func save_data() -> void:
var file = FileAccess.open(GAMEDATA_FILE_PATH, FileAccess.WRITE)
file.store_var(time_records)
file.store_var(has_played)
file.store_var(last_level)
file.store_var(unlocked_level)
file.close()
func load_data() -> void:
if not FileAccess.file_exists(GAMEDATA_FILE_PATH):
return
var file = FileAccess.open(GAMEDATA_FILE_PATH, FileAccess.READ)
var temp_time_records: Array[float] = file.get_var()
if temp_time_records.size() != time_records.size():
return
time_records = temp_time_records
has_played = file.get_var()
last_level = file.get_var()
unlocked_level = file.get_var()
file.close()
Edit:
However, I also got some suggestions to overwrite the data or move the bad data to another folder for better practice.
