* HELP * -- how to save, load, edit, delete a file from my game?

I want my game to create a file and put some data on it…i will use it to save the player’s high score in my game…retrieve the file whenever the game open, get the data and transmit to the game, edit the data from my game and overwrite the old data,…can you teach me how to do it?..if you can give me a sample code snippet, it much better…thanks :’)

1 Like

local table = {true = 1, false = 0}
sys.save(sys.get_save_file(“id”, “name”), table)
variable = sys.load(sys.get_save_file(“id”, “name”))

4 Likes

I do this in my loader script. (Disclaimer: I’m learning this myself, so it might not be the best way. And sorry for the formatting. I’m writing this, commuting on a train. :slight_smile: )

Flow is something like this;

  • Gamedata is stored in a module. At the moment it only contains a table for player, storing the highscore and current score.

  • Loader loads saved data into this table, and stores table at given moment (in my case overtime player returns to main menu) Code snippets:

shared_data.module:

local M = {
  player = {
    highscore = 0,
    score = 0
  }
}
return M

loader.script:

    local shared_data = require "main/modules/shared_data"
    local save_file = sys.get_save_file("my_game", "gamedata")

function save_gamedata(self)
	-- Reset player score
	shared_data.player.score = 0
	-- Save gamedata
	if not sys.save(save_file, shared_data) then
		pprint("Gamedata not saved.")
	end
end

function load_gamedata(self)
	--	Load gamedata
	local gamedata = sys.load(save_file)
	if not next(gamedata)  then
		pprint("Gamedata empty")
	else
		pprint(gamedata)
		shared_data.player.highscore = gamedata.player.highscore
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	load_gamedata(self)
	load_menu(self)
end

function on_message(self, message_id, message, sender)
   ...
   elseif message_id == hash("back_to_menu") then
    	unload_main(self)
    	load_menu(self)
    	save_gamedata(self)
    elseif ...
    end 
end
10 Likes

That’s a great example @rsletta!

@Jhei_Krauzer, you should check out the reference (http://www.defold.com/ref/sys/) and have a look at the functions sys.load, sys.save and sys.get_save_file (for debugging).

4 Likes

Thank you @jakob.pogulis.

@Jhei_Krauzer: Tell me if you need more details regarding the example.

2 Likes

Thank you very much for all of your help guys…i really appreciated it alot…i will tell you later if i succeed. :’)

1 Like

how can i overwrite the existing data of a savefile whenever i got a new high score?..i dont know…can you help me? :’)

Use a conditional statement. Example:

If self.score > save_gamedata.highscore then save_gamedata.highscore = self.score End

If you want to save the new highscore, you just need to run the save_gamedata() function.

1 Like

Oh!..is that what i need?..thanks for the idea…actually, i know that i need to use an if statement to compare the new score from the old score…but what i actually dont know how to overwrite it…thank you very much sir.

Actually, im really confused on that save_gamedata() function…coz what i thought, you use the save_gamedata() function if the shared_data.lua module didnt exist in the directory…so that it will automatically create that file…so now what i learn actually is that im really required to build a directory(folders) and create a new module file there…and then use it to store the data i wanted to save… :’)

Yes, sorry for not being clear about that. Note that it might be more best practice to differentiate between saved_gamedata and shared_gamedata. To implement that you could create a function that pass data between the the tables, before you save the data.

Example:

local shared_gamedata = require "shared_gamedata module"
local savedata = {}

function move_data(direction)
  If direction == load then
     shared_gamedata.highscore = savedata.highscore
  elseif direction == save the
    savedata.highscore = shared_data.highscore
  end 
end

function init(self)
  load_gamedata()
  move_data(load)
end

...
 save_gamedata()
 move_data(save)
...
2 Likes

no sir, its ok…im really happy coz i learn a lot from you…i really appreciate all of your help…thank you very much…i will try it later…i will reply if im succeed… :’)

Hi, I didn’t fully understand what was going on with this code, but it helped to figure out a much more simplified version of saving a high score, thanks. When I achieve a high score it only persists for as long as a build left open (I’ve only been running my game out of the editor I haven’t bundled it). If I rebuild the game , the game data goes back to it’s default settings. Is this the same for yours? Cheers.

I just checked with my game. It keeps the same save file between sessions when I build and run from the editor (editor 2), your code must not be loading or saving properly. The first post in this thread, by Maru, gives you the minimum code you need to save and load data.

1 Like

Thanks, I’ll try again.