SavaData.gd 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. class_name SaveData
  2. extends Resource
  3. @export var last_score := 0
  4. #@export var inventory: Resource = Inventory.new()
  5. @export var level_name := ""
  6. @export var level_highscores := []
  7. const SAVE_SCORE_PATH := "user://save"
  8. func save_score_data():
  9. print (level_name)
  10. update_highscore_entry()
  11. ResourceSaver.save(self, SaveData.get_save_path())
  12. static func save_exists() -> bool:
  13. return ResourceLoader.exists(get_save_path())
  14. static func load_score_data() -> Resource:
  15. var save_path := get_save_path()
  16. return ResourceLoader.load(save_path)
  17. static func get_save_path() -> String:
  18. var extension := ".tres" if OS.is_debug_build() else ".res"
  19. return SAVE_SCORE_PATH + extension
  20. func update_highscore_entry():
  21. if level_name == "":
  22. return
  23. if highscore_entry_exists(level_name):
  24. if last_score > get_highscore_entry(level_name):
  25. print ("highscore in level")
  26. overwrite_highscore_entry(level_name, last_score)
  27. else:
  28. print ("no highscore")
  29. #print ("entry found: " + str(get_highscore_entry_index(level_name)))
  30. #print ("entry found: " + str(get_highscore_entry_index(level_name)))
  31. return
  32. print ("creating new highscore entry for level: " + str(level_name) + ", value: " + str(last_score))
  33. var _temp_score := []
  34. _temp_score.append(level_name)
  35. _temp_score.append(last_score)
  36. level_highscores.append(_temp_score)
  37. func highscore_entry_exists(_level_name):
  38. for hs in level_highscores:
  39. if hs[0] == _level_name:
  40. return true
  41. return false
  42. func get_highscore_entry_index(_level_name):
  43. var _index = 0
  44. for hs in level_highscores:
  45. if hs[0] == _level_name:
  46. return _index
  47. _index +=1
  48. func get_highscore_entry(_level_name):
  49. if _level_name == null:
  50. return
  51. for hs in level_highscores:
  52. if hs[0] == _level_name:
  53. print ("returning level highscore..")
  54. return hs[1]
  55. print ("ERROR: returning nothing!!!")
  56. return -1
  57. func overwrite_highscore_entry(_level_name, value):
  58. for hs in level_highscores:
  59. if hs[0] == _level_name:
  60. level_highscores[get_highscore_entry_index(_level_name)][1] = value
  61. print (level_highscores)