Best way to save a table human readable

I am trying to save a table in a local file with the help of sys.save and a previuos mentioned module. My game is perfectly able to save and read the data from the file. But I want to be able to open and read the file without the game. Is there any way to save the tables human readable?

My table:

local M = {
	player = {
		playerID = 137610,
		score = 30
	}
}
return M

Saving files:

function saveScore(self)
	if not sys.save("save.txt", shared_data) then
		print("Gamedata not saved")
	end
end

Loading files:

function loadScore(self)
	local gamedata = sys.load("save.txt")

	if not next(gamedata) then
		pprint("Gamedata empty")
	else
		pprint(gamedata)
	end
end

Debug output from defold console:

DEBUG:SCRIPT: 
{ --[[000000003C269900]]
  player = { --[[000000003C27C2F0]]
    playerID = "137610",
    score = 5
  }
}

When I open up the file with other editors it looks like this:

HDTB       player    playerID   137610   score       "@

You could encode as json format instead. Defold has built in json.decode support. Use the 3rd party Lua module rxi.json for encoding.

3 Likes

I am going to try this now.

Is it even possible to save a local file in a subfolder on a HTML5 game? I have tried to use sys.get_save_file() but this seems to save files in the profile path of the user who is running the webserver, which is not always accessable for me.

When I try to use rxi.json for encoding my table as json I get the following error:

local file = sys.get_save_file("christmas_game_2018", "test.txt")
if not sys.save(file, json.encode(shared_data)) then
	pprint("Gamedata not saved")
end
ERROR:SCRIPT: /main/game.script:11: bad argument #2 to 'save' (table expected, got string)

Don’t use sys.save. That accepts a table and does the serialising for you. You already have the serialised data as a string. Use the native io Lua functions to write that string to file.

1 Like

Yep, using the native io functions worked great. Found help on this thread: Persistence of data? (SOLVED)

But I still have problems storing my files on a Apache2 Webserver without ssh/shell access.

Can you please explain a bit more?

I was trying to save a seperate local file for each player (identified by the given url param) in a subfolder right where the bundled html5 game is located. Using sys.get_save_file made it possible for me to do so but just within another folder (I thought it would be the folder of the local webserver user). As soon as I tested with another browser I recognized that it was not possible to find my before created files so imho there are two options:

  • the files get stored anywhere on the server where the html5 game lies but separated with unique sessions
  • the files get stored anywhere local on the client system, in a browser based location

sys.get_save_file() and sys.save() will store the file locally on the users machine, not on the webserver. In an HTML5 context it will be stored in a virtual filesystem somewhere within the browser.

Okay. Are there any ways to create a file local on the webserver within the gamedir?

The proper way to do this is not to save the file locally, but save the data that needs saving in a database. You’ll also need some backend server code that exposes a POST endpoint or similar and does the saving. Then, you’ll need to call this endpoint with http.request() from the client side.

(You’ll probably want to use some JSON encoded data as POST body and don’t forget to set the Content-Type: application/json HTTP header)

1 Like

@dapetcu21 that what my workaround was today. I created a PHP endpoint which reacts on my http requests.

It is not really a workaround is it? That is usually the way a client application interacts with a server, through HTTP requests.

3 Likes

Indeed it is the normal way. I have launched several applications like this before. The “workaround” was more meant to describe the situation itself. I did not think about that HTML5 applications are running client-side. So I thought it could be possible to create local files on the server-side.

Nevermind, after a few minutes of thinking about the background logic I might say I had a little “light up” moment :wink:

2 Likes