Skip to content

Commit

Permalink
Persist settings and highscores to filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryDiv committed Apr 4, 2022
1 parent 371d043 commit 957f22c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/HighScores.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extends Node

const HIGHSCORE_FILENAME = "user://highscores.json"

var scores = [{"name": "Nobody", "score": 0}]

Expand All @@ -8,10 +9,48 @@ class ScoreSorter:
return a["score"] > b["score"]


func _ready() -> void:
load_highscores()

func sort_scores() -> void:
scores.sort_custom(ScoreSorter, "sort_descending")

func add_score(score) -> void:
scores.push_back(score)
sort_scores()
print(scores)
save_highscores()


func load_highscores() -> void:
prints(self, "Loading highscores from filesystem...")

# Try to load the highscore file
var highscore_file := File.new()
if not highscore_file.file_exists(HIGHSCORE_FILENAME):
prints(self, "Highscore file does not exist yet.")
return

var err = highscore_file.open(HIGHSCORE_FILENAME, File.READ)
if err != OK:
printerr("Error opening highscore file to read: ", err)
return

# Parse highscore file as JSON
var highscore_json = highscore_file.get_as_text()
scores = parse_json(highscore_json)

func save_highscores() -> void:
prints(self, "Saving highscores to filesystem...")

# Try to save the highscore file
var highscore_file := File.new()

var err = highscore_file.open(HIGHSCORE_FILENAME, File.WRITE)
if err != OK:
printerr("Error opening highscore file to write: ", err)
return

# Dump highscore to JSON
var highscore_json = to_json(scores)
highscore_file.store_line(highscore_json)
57 changes: 55 additions & 2 deletions src/Settings.gd
Original file line number Diff line number Diff line change
@@ -1,13 +1,66 @@
extends Node

# General settings
# Constants
const CONFIG_FILENAME = "user://settings.json"

# General settings (persistent)
var sfx_enabled := true
var music_enabled := true

# Game mode
# Game mode (not persistent)
enum GameMode {
MODE_SOLO = 0,
MODE_MULTI_CHAOS = 1,
MODE_MULTI_HOTSEAT = 2,
}
var game_mode: int = GameMode.MODE_SOLO


func _ready() -> void:
# Load settings from filesystem
load_settings()


func from_dict(data: Dictionary) -> void:
sfx_enabled = data.get("sfx_enabled", true)
music_enabled = data.get("music_enabled", true)


func to_dict() -> Dictionary:
return {
"sfx_enabled": sfx_enabled,
"music_enabled": music_enabled,
}


func load_settings() -> void:
# Try to load the config file
var config_file := File.new()
if not config_file.file_exists(CONFIG_FILENAME):
prints(self, "Config file does not exist yet.")
return

var err = config_file.open(CONFIG_FILENAME, File.READ)
if err != OK:
printerr("Error opening config file to read: ", err)
return

# Parse config file as JSON
var config_json = config_file.get_as_text()
from_dict(parse_json(config_json))


func save_settings() -> void:
prints(self, "Saving settings to filesystem...")

# Try to save the config file
var config_file := File.new()

var err = config_file.open(CONFIG_FILENAME, File.WRITE)
if err != OK:
printerr("Error opening config file to write: ", err)
return

# Dump config to JSON
var config_json = to_json(to_dict())
config_file.store_line(config_json)
3 changes: 3 additions & 0 deletions src/screens/title_screen/TitleScreen.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func start_game() -> void:
if loading_game:
return

# Persist settings
Settings.save_settings()

print("Start game: Switch to MainGame scene")
loading_game = true

Expand Down

0 comments on commit 957f22c

Please sign in to comment.