Hi friends,
In the Load function, I’m getting these errors for stars
and hiPoints
:
“Invalid assignment of property or key ‘stars’ with value of type ‘Array’ on a base object of type ‘Resource (Player_Data)’.”
“Invalid assignment of property or key ‘hiPoints’ with value of type ‘Array’ on a base object of type ‘Resource (Player_Data)’.”
However, in my Player_Data class, both of these are already defined as arrays. I haven’t been able to solve the issue. I’ll drop the code below.
Player Data:
extends Resource
class_name Player_Data
# oyuncunun seviye ile ilgili bilgiler
var max_level: int = 0
var last_level: int = 0
# oyuncunun başarımları
var stars: Array[int] = []
var hiPoints: Array[int] = []
# oyuncunun varlıkları
var gold: int = 0
Load function code of Save’s Node:
func load_data():
if FileAccess.file_exists(dir+path):
var file = FileAccess.open_encrypted_with_pass(dir + path, FileAccess.READ, key)
if file == null:
print("Dosyaya erişilemedi: ", FileAccess.get_open_error())
return
var content = file.get_as_text()
file.close()
var data = JSON.parse_string(content)
if data == null:
return
var player = data["player_data"]
Ref.player_data = Player_Data.new()
# seviye bilgileri
Ref.player_data.last_level = 0 if !player.has("last_level") else player["last_level"]
Ref.player_data.max_level = 0 if !player.has("max_level") else player["max_level"]
# oyuncu başarımları
Ref.player_data.stars = [] if !player.has("stars") else player["stars"]
Ref.player_data.hiPoints = [] if !player.has("hiPoints") else player["hiPoints"]
# oyuncu varlıklıkları
Ref.player_data.gold = 0 if !player.has("gold") else player["gold"]
else:
print("Dosya mevcut değil: ", FileAccess.get_open_error())
Ref.player_data = Player_Data.new()
save_data()
load_data()
The reason I’m writing the code this way is to reset the data in case it’s missing from the JSON file.
This is meant to ensure that if I update the game in the future and introduce new variables, they can still be added to older save files without causing errors.