Saving Files using EGO?

Hi I have a question to get things clear how would I use Ego that Peach herself provided. If anybody have Ego and know how to use it can you please share your information of knowledge and show me how to use it thanks :slight_smile:

Help is appreciated
[import]uid: 17058 topic_id: 19985 reply_id: 319985[/import]

Hello,

http://techority.com/2011/12/28/ego-easy-saving-and-loading-in-your-corona-apps/

the sample code is in comment in the ego.lua file.

JĂĽrgen [import]uid: 19590 topic_id: 19985 reply_id: 77837[/import]

@jpenne yeah I got it but I don’t know how to use it properly can you please explain or show it to me the proper way [import]uid: 17058 topic_id: 19985 reply_id: 77840[/import]

i dont usually save files, but when i do im using my own functions for that

p.s its a humor(and truth too) [import]uid: 16142 topic_id: 19985 reply_id: 77847[/import]

@darkconsoles really! I would like to know your way and functions of saving files. Is it simple? [import]uid: 17058 topic_id: 19985 reply_id: 77848[/import]

i like json library, so i wrote something quicky
[lua]local Json = require(“json”)
function readScores()
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory )
– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “r” )

if file then
– read all contents of file into a string
local contents = Json.decode(file:read( “*a” ) )

io.close( file )
return contents
end
end

function saveScores(tabl)

local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory )

– create file b/c it doesn’t exist yet
local file = io.open( path, “w” )

if file then
file:write( Json.encode(tabl) )
io.close( file )
end

end

local o = {
one = “i am one”,
two = “i am 2”
}

saveScores(o)
local loadedtable = readScores()
for i,v in pairs(loadedtable) do
print(i,v)
end[/lua] [import]uid: 16142 topic_id: 19985 reply_id: 77862[/import]

@darkconsole is a bit confusing but thanks for showing me your way of method [import]uid: 17058 topic_id: 19985 reply_id: 77866[/import]

just play with it, you will be comfortable with saving and loading fairly soon [import]uid: 16142 topic_id: 19985 reply_id: 77868[/import]

@darkconsole this set of line of code you gave me Do I split the load in the menu.lua and the save file save them on the level i want to save it [import]uid: 17058 topic_id: 19985 reply_id: 77873[/import]

you can make an external module for this functions if you need [import]uid: 16142 topic_id: 19985 reply_id: 77886[/import]

@Darkconsole and what about the EGO method of saving file what do you think about it if you heard of it which came from peach.

This is is how it’s written up

[code] module (…, package.seeall)

–[[

======================================================================================

EGO

An extremely simple way to save and load small amounts of data
in your iOS or Android application.

Created by Peach Pellen - http://Techority.com/
December 26, 2011

(Example usage tutorial available on Techority)

============================================================
SETUP

ego = require “ego”
saveFile = ego.saveFile
loadFile = ego.loadFile

============================================================
SAVING

To save a file you simply use;

saveFile( “filename.txt”, value)

Filenames can be anything you like provided they end in .txt and the value can
be a string, number or variable.

============================================================
LOADING

To load a file you simply use;

valueName = loadFile( “filename.txt” )

If you have previously saved a value to “filename.txt”, valueName will be set
accordingly.

If no file is present the file will be created and automatically saved as “empty”.

======================================================================================

–]]

–Save function
function saveFile( fileName, fileData )
–Path for file
local path = system.pathForFile( fileName, system.DocumentsDirectory )
–Open the file
local file = io.open( path, “w+” )

–Save specified value to the file
if file then
file:write( fileData )
io.close( file )
end
end

–Load function
function loadFile( fileName )
–Path for file
local path = system.pathForFile( fileName, system.DocumentsDirectory )
–Open the file
local file = io.open( path, “r” )

–If the file exists return the data
if file then
local fileData = file:read( “*a” )
io.close( file )
return fileData
–If the file doesn’t exist create it and write it with “empty”
else
file = io.open( path, “w” )
file:write( “empty” )
io.close( file )
return “empty”
end
end[/code]

Would you know what to do here when trying to save files [import]uid: 17058 topic_id: 19985 reply_id: 77896[/import]

In main.lua;

[lua]ego = require “ego”
saveFile = ego.saveFile
loadFile = ego.loadFile

saveFile(“myFile.txt”, “this is my saved string”)
testLoad = loadFile (“myFile.txt”)

print (testLoad)[/lua]

That should print “this is my saved string”, which is being read from the file you created above, myFile.txt.

Peach :slight_smile: [import]uid: 52491 topic_id: 19985 reply_id: 77993[/import]

@peach so what if I want to save an object or a value what do I change or replace [import]uid: 17058 topic_id: 19985 reply_id: 77997[/import]

You would save the value - like if you had a variable called “stars” and stars = 3 (to say the got three stars) you would save stars. (So you’d be saving the number “3”.)

Then when you load the file it would return “3”, so you would know to display 3 stars.

Does that make sense?

I am going to try to write up two examples over the coming week which might help you - so stay tuned for those :slight_smile:

Peach [import]uid: 52491 topic_id: 19985 reply_id: 78007[/import]