Hello, sorry to bother. Did you receive my email and if so did you get anywhere with this problem I have been having?
Hi. I checked your project earlier today and was not able to reproduce a problem. Are you sure that you are not trying to read a corrupt save file? Have you removed the save folder for your project?
Thatâs strange. The project has two scripts where it accesses the JSON file. One of them works perfectly so I doubt the file is corrupted but when I try to attach the second one to a game object and run it, then the crash occurs.
local function loadHighscore(filename)
local content = sys.load(filename)
if content == nil then
print("Failed to load JSON content from file: " .. filename)
return nil
end
local data = content
if not data or not data.numbers then
print("Invalid or missing data in JSON file: " .. filename)
return nil
end
table.sort(data.numbers, function(a, b) return a > b end)
-- Return the highest score (first value)
return data.numbers[1]
end
function init(self)
self.file_path = sys.get_save_file("main", "test.json")
local highscore = loadHighscore(self.file_path)
end
This is the script that causes the issue. It should be called âscore_display.scriptâ
And, are you loading the actual file you saved?
You didnât answer the questions:
âAre you sure that you are not trying to read a corrupt save file? Have you removed the save folder for your project?â
What is the actual file path that you save?
What is the actual file path that you load?
I have managed to stop the crash from happening and everything seems to be fixed. It seems that defold didnât like âsys.loadâ. So after removing that and accessing the file in a different way it now works. This is my first time using JSON files in defold so I am very inexperienced and donât fully understand it so sorry for any confusion and thank you for trying to help.
I think you are confusing things. sys.load() and sys.save() can ONLY be used when saving and loading Lua tables. You can not use sys.load() to load a JSON file.
If you are saving and loading a high score list then save and load it as a Lua table. There is no need to involve JSON.
Use JSON for loading other types of data, such as project configurations or data files produced by external tools, or data received from a server. Note that you can use json.decode() to convert a string of JSON data into a Lua table.
That makes sense, sorry about this. My teacher suggested I use JSON for this so that is what I began to look at from the start.