Loading...
Loading...
Implement a save system using JSON files to store player progress, scores, and settings.
Create an Autoload (Singleton) script for your save system. Go to Project → Project Settings → Autoload. Create a new script called 'SaveSystem.gd' and add it as an Autoload. This makes it accessible from anywhere in your game.
In the SaveSystem script, create a dictionary to hold your save data: `var save_data = { 'player_position': Vector2.ZERO, 'player_health': 100, 'score': 0, 'level': 1 }`. Add any other variables you want to save (inventory, unlocked items, settings, etc.).
Create a save function: `func save_game():`. Inside it, first update the save_data dictionary with current values. Then convert the dictionary to JSON: `var json_string = JSON.stringify(save_data)`. Create a file: `var file = FileAccess.open('user://savegame.json', FileAccess.WRITE)`. Write the data: `file.store_string(json_string)`. Close the file: `file.close()`.
Create a load function: `func load_game():`. Check if the file exists: `if not FileAccess.file_exists('user://savegame.json'): return`. Open the file: `var file = FileAccess.open('user://savegame.json', FileAccess.READ)`. Read the content: `var json_string = file.get_as_text()`. Parse it: `var json = JSON.parse_string(json_string)`. Update save_data: `save_data = json`.
Call save_game() at appropriate times (when player reaches checkpoint, exits level, or presses Save button). Call load_game() in your main menu or game start scene. Use the saved data to restore player position, health, score, etc. when the game loads.