Being new at this, i’m going to try for the first time to implement a save/load state for the game i’m developing.
Question is: what’s the best way to do this?
I’ve seen some code around, things like Ice, maybe XML (or am i completely confused)
I’m no programmer, i’m self-taught and even though i’m making things work pretty well, i lack tons of knowledge about programming in general. Please excuse me if i ask things that are too obvious ir stupid. Thanks everybody for your great support!
[import]uid: 105206 topic_id: 21709 reply_id: 321709[/import]
I would use json if you want to store an array/table of values
There are samples on how to do this via the code base.
Also check out ICE (also available via code base) [import]uid: 84637 topic_id: 21709 reply_id: 86211[/import]
Thanks for your answer, i completed forgot to reply!
I’ll be checking these out today! [import]uid: 105206 topic_id: 21709 reply_id: 88874[/import]
Check out this thread:
Towards the bottom of the thread, I provided two simple save/load table functions.
The point I took from that thread, is that when we say “just use JSON” and someone goes and looks up JSON it will scare most people, but you don’t need to know JSON to USE JSON with Corona. You just need data you’re saving in a table and make a function call. [import]uid: 19626 topic_id: 21709 reply_id: 88890[/import]
@robmiracle,
your answers are always enlighting! thanks for the examples.
I’ve reading about XML, JSON and ICE all day, and understood how to use them.
But even though i believe that for what i want to do for know JSON will apply, ICE seems so cool that i want to introduce it. Thing is, will it actually help or make things worse?
I’m talking about a character creation game. The idea is to have a library where all your previous created characters are available, so i’ll need to only save a reference to which body, head, eyes, colours, etc the player has chosen.
How would i do this with ICE or just with JSON? I want each character to be saved in a progressive way, like, first character’s save: save1, second: save2; and have that process completely automated so I don’t have to check everytime which names are already taken.
Hope i had explained myself clear
[import]uid: 105206 topic_id: 21709 reply_id: 88900[/import]
I’m not sure I fully understand, but let me take a stab.
Lets say you have a table:
charNames = {}
Then you could do:
charNames[“char1”] = true – indicating it’s used.
later do:
charNames[“char2”] = true
Then save the table out, load it back you have the same table. Then to check to see if a name is in use:
if charNames[nameToCheck] then
-- name is use
else
-- name is not in use
end
I just posted in the community code, my super simple table save/load functions…
https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK
[import]uid: 19626 topic_id: 21709 reply_id: 88907[/import]
Sorry for hijacking this thread a little, but this was exactly one of the questions I logged on to ask 
@robmiracle - I’ve just looked at your code for the Super Simple Table Save/Load Function, and it must be a trick. Is it really that easy?? [import]uid: 7841 topic_id: 21709 reply_id: 88910[/import]
Yep it’s that simple! [import]uid: 19626 topic_id: 21709 reply_id: 88911[/import]
@robmiracle,
that could be a solution
what i am really trying to do is get each new character someone makes saved, without him having to worry about the name.
The way you described i’d need to predefine the table with the total amount of supported chars? [import]uid: 105206 topic_id: 21709 reply_id: 88915[/import]
Tables are awesome
[import]uid: 10389 topic_id: 21709 reply_id: 88956[/import]
I’m not 100% sure I fully understand what your needing nml but from what I’m getting you want something like if a player where to like click a button (just using that as an example) it would save all of their current character data to a file which would they could later look at to see how they have changed through their gameplay, so you need to save new information without coping over old information even for the same character. So if a player named nml saves their data two times, two instances of nml’s data will be saved instead of just one.
If I am correct on my understanding, you could achieve this using Ice, Ego or rob’s suggestion.
For Rob’s you would do something like this:
(This shows how to save data and then load all of the saved characters back)
require("loadsave")
-- First we have to check to see if any data has been saved before
local curData = loadTable("dataCount.json")
-- if this is the first time playing we have to set curData to a value
if (curData == nil) then
curData = 1
saveTable(curData, "dataCount.json")
end
-- This represents whatever the current character settings are at the time of saving
local charSettings = {}
charSettings.name = "nml"
charSettings.lvl = 100
charSettings.hair = "Brown"
charSettings.eyes = "Blue"
local saveData = function()
saveTable(charSettings, curData .. ".json")
curData = curData + 1 -- Increase curData by one now that we saved new info
saveTable(curData, "dataCount.json") -- resave the curData count with new value
end
-- This is callling our save function
saveData()
-- This shows the last saved object
local lastSaved = loadTable(curData-1 .. ".json")
print(lastSaved.name)
local loadAll = function()
for i = 1, curData-1 do
local dataGet = loadTable(i .. ".json")
print("Name: " .. dataGet.name)
print("Level: " .. dataGet.lvl)
print("Hair: " .. dataGet.hair)
print("Eyes: " .. dataGet.eyes)
print(" ") -- Print a space between characters
end
end
-- This is used to call LoadAll
loadAll()
As this code saves the data under numbered tables, it wont save over old data and you wont have to worry about the players name as part of the file name, that will just be info inside of the file.
Also of course you would change the LoadAll function to use some other way of displaying the data inside the app instead of print’s, thats just to show you how it works. [import]uid: 69700 topic_id: 21709 reply_id: 88968[/import]
Damn right, that’s exactly what i need!
Maybe had a hard time explaining it cause im no native speaker (:P)
So no matter what method i use, the idea is the same.
Any benefit in performance using JSON over ICE? (i don’t see the need of using ice here, it would be a choice based on likings, wouldn’t it?)
Thank you everybody! [import]uid: 105206 topic_id: 21709 reply_id: 89082[/import]
I really had not looked at ICE and what it does until now. First it was written by Graham who writes very good code. The underlying stuff is pretty well thought out and it gives you several methods that do things for you like “Save if the score is higher”. He’s done a very good job of putting a very nice package together.
Under the hood, he is using both the json library as well as SQLite to save the data. Instead of flat files, like my code uses, you have the overhead of the sqllite3 engine with his code.
It will make your app have a larger filesize to download, so if you’re flirting with that 20mb limit for WiFi downloads (or almost everyone I’ve asked to test an android app complains about a 10mb file) then you might want to consider something other than ICE.
From a memory/speed angle, ICE is still going to be plenty fast enough, but the SQL engine does add overhead. It still has to open, read/write, and close files. It still has to encode the data. All the database work is above what my method uses. But in the grand scheme of things, it’s pretty trivial and if the extra methods that ICE provides helps you with your tasks, I would recommend it. But if simple is what you want…
You do take the JSON library size it either way which is still trivial in the grand scheme of things. [import]uid: 19626 topic_id: 21709 reply_id: 89092[/import]
stupid question
i have a var named number and holds and int value
so if i do: table.num=number
I can’t assign that to the table like this, can i?
should i use toString() in this case?
EDIT: not tostring(), what should i do? cause this isn’t working only for variables :S i absolutely forgot about this [import]uid: 105206 topic_id: 21709 reply_id: 89108[/import]
yes:
myTable.num = num
works just fine.
Just make sure you do a:
myTable = {}
to initialize the variable.
Also you can’t use the name “table” since its an API reserved word.
[import]uid: 19626 topic_id: 21709 reply_id: 89118[/import]
yeap, not using table as name, and initializing it before.
I must have something wrong somewhere else. [import]uid: 105206 topic_id: 21709 reply_id: 89121[/import]
Solved, i was right, had troubles in other section of code.
I can’t stop thanking you guys for your help.
You really oriented me in something i had no idea about! [import]uid: 105206 topic_id: 21709 reply_id: 89125[/import]