ID:1032838
 
I haven't had a chance to use this structure, but I decided to add it after I saw so many works just throwing up text2file and file2text with all kinds of painful manual parsing.

(EDITED!)

FileManager
var
__file // text reference to file

proc
fileExists()
return fexists(__file)

loadFile(file=__file)
ASSERT(fexists(file))
__file = file
onLoadFile(getLoadFileElement(file))

// the element we use to load this file into memory.
getLoadFileElement(file)
return file

// implementation specific reaction to file being loaded
onLoadFile(element)

saveFile(file=__file)
__file = file
onSaveFile(getSaveFileElement(file))

// the element we use to save the data into a file
getSaveFileElement(file)
return file

// implementation specific reaction to file being saved
onSaveFile(element)


The getLoadElement() and getSaveElement() procs are so that you can implement things like XML, INI, YAML, or whatever type of parsing you intend on using.

SavefileManager
parent_type = /FileManager
getLoadFileElement(file)
return new /savefile(file)

getSaveFileElement(file)
return new /savefile(file)


And from here all you have to do is read and write.

WorldData
parent_type = /SavefileManager
// we supply it with a default file
// never call saveFile or loadFile with a custom file name.
__file = "world.sav"

var
name = "Keeth's Wacky World"
status = "HEY KIDS!"

onSaveFile(savefile/sfile)
sfile["name"] = name
sfile["status"] = status

onLoadFile(savefile/sfile)
name = sfile["name"]
status = sfile["status"]

proc
setName(name)
src.name = name
saveFile()

setStatus(status)
src.status = status
saveFile()


If you have any problems or suggestions, go ahead and leave a comment.
This is a terrible tutorial, because it advocates adding robals to your game. And those are icky.
In response to Toadfish
Toadfish wrote:
This is a terrible tutorial, because it advocates adding robals to your game. And those are icky.

I've spoken with my people, and we've decided that your criticism is completely accurate.

I will have to revise the snippet in the near future.