Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Saving and loading data files — Gideros Forum

Saving and loading data files

SinisterSoftSinisterSoft Maintainer
edited April 2017 in General questions
A nice way to save load data is by using json, here is one way to do it.

First, somewhere early in your code load the json library...
pcall(function() require "json" end)
Then here is a save routine for a sample table:
function save()
	if json then
		local savedata={}
		savedata["highscore"]=hiscore
		local file=io.open("|D|data.txt","w+")
		if file then
			pcall(function() file:write(json.encode(savedata)) end)
			file:close()
		end
	end
end
Just copy the variables you need saving into the temporary savedata table.

Alternatively you could have the savedata table outside the function and use it throughout your game.
Here is a load routine to load the table:
function load()
	if json then
		local file=io.open("|D|data.txt","r")
		if file then
			local originaldata=file:read()
			file:close()
			if originaldata then
				local decoded=json.decode(originaldata)
				if decoded then
					hiscore=tonumber(decoded["hiscore"])
				end
			end
		end
	end
	if hiscore==nil then hiscore=0 end
end
Notice the check at the end to set valid values if for any reason the results are nil.

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+5 / -0 )Share on Facebook

Comments

Sign In or Register to comment.