Advertisement
thiagobruno

SaveGameGodot-Sample

Jul 30th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. var data = OS.get_date()
  2. var savegame = File.new()
  3. var save_path = "user://meugame.save"
  4. var save_data
  5. var save_model = {
  6.     "jsonVersion": "0.0.0.1",
  7.     "muteMusic": 0,
  8.     "muteSfx": 0,
  9.     "totalMoney": 0,
  10.     "totalDiamods": 0,
  11.     "worlds": [
  12.             {
  13.                 "name": "Mundo 1",
  14.                 "levels": [
  15.                         { "num": "1", "stars": "0", "concluido": "0" },
  16.                         { "num": "2", "stars": "0", "concluido": "0" },
  17.                         { "num": "3", "stars": "0", "concluido": "0" },
  18.                         { "num": "4", "stars": "0", "concluido": "0" },
  19.                         { "num": "5", "stars": "0", "concluido": "0" },
  20.                         { "num": "6", "stars": "0", "concluido": "0" },
  21.                         { "num": "7", "stars": "0", "concluido": "0" },
  22.                         { "num": "8", "stars": "0", "concluido": "0" },
  23.                         { "num": "9", "stars": "0", "concluido": "0" },
  24.                         { "num": "10", "stars": "0", "concluido": "0" }
  25.                     ]
  26.             },{
  27.                 "name": "Mundo 2",
  28.                 "levels": []
  29.             }
  30.         ]
  31. }
  32.  
  33. func _ready():
  34.     if not savegame.file_exists(save_path):
  35.         # cria um novo arquivo
  36.         newFile(save_path, save_model)
  37.     else:
  38.         # carrega um arquivo já salvo
  39.         loadSavedGame()
  40.  
  41. # para criar um arquivo novo a partir de um modelo de dados
  42. func newFile(_path, _data):
  43.     save_data = _data
  44.     savegame.open_encrypted_with_pass(_path, File.WRITE, OS.get_unique_id())
  45.     savegame.store_var(_data)
  46.     savegame.close()
  47.  
  48. # carrega um arquivo para uma variavel local
  49. func loadSavedGame():
  50.     savegame.open_encrypted_with_pass(save_path, File.READ, OS.get_unique_id())
  51.     save_data = savegame.get_var()
  52.     savegame.close()
  53.  
  54. # Pega da variavel local e salva no arquivo
  55. # Essa função geralmente chamo somente após alguma ação, por exemplo ao concluir uma fase.
  56. func onlySaveData():
  57.     savegame.open_encrypted_with_pass(save_path, File.WRITE, OS.get_unique_id())
  58.     savegame.store_var(save_data)
  59.     savegame.close()
  60.  
  61. # Ao trabalhar com a variavel local, você pode usar essa função para obter um valor de alguma chave
  62. func readData(campo):
  63.     var ret = 0
  64.     if save_data.has(campo):
  65.         ret = save_data[campo]
  66.     return ret
  67.  
  68. # Ao trabalhar com uma variavel local, você pode usar uma função como essa para salvar as informações ainda na variavel
  69. func saveData(campo, valor):
  70.     if save_data!=null:
  71.         save_data[campo] = valor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement