for i = 1, #personnel do
print(i)
for k, v in pairs(personnel[i]) do
print(k, v)
defsave.set("personnel_1", ""..personnel[i][k] , personnel[i][k].v )
end
defsave.save("personnel_1", true)
where I want to save all table of data of lua to a save.
the lua is like this
local M ={
{ text = "Tom Smith", wage = "3000$", photo = "profile1", pos = "editor", lvl = "Level 2", time = 1 },
{ text = "Bruce Johnson", wage = "1500$", photo = "profile2", pos = "reporter", lvl = "Level 1", time = 3 },
{ text = "Mary Miller", wage = "1800$"......},
{.......
when I run the code I have this error
DEBUG:SCRIPT: 1
DEBUG:SCRIPT: lvl Level 2
DEBUG:SCRIPT: pos editor
DEBUG:SCRIPT: photo profile1
DEBUG:SCRIPT: time 3
ERROR:SCRIPT: /main/save.gui_script:125: attempt to index a number value
stack traceback:
/main/save.gui_script:125: in function </main/save.gui_script:104>
so defsave cannot do it because one value is number? cannot have in data number and strings?
or I have sth wrong in my code?
I’m guessing this is the problem. You get the value like this personnel[i][k] but you also have the value already in v.
What you are trying to do is to read the key “v” from the value:
personnel[i][k].v
To help with readability it’s also a good idea to store the current person to save in a variable:
for i = 1, #personnel do
local person = personnel[i]
for k, v in pairs(person) do
local key = tostring(person[k])
defsave.set("personnel_1", key, v)
end
One thing I’m wondering is if DefSave doesn’t support saving of Lua tables “as-is” so that you don’t have to iterate over all values like that. Something like this:
for i = 1, #personnel do
local person = personnel[i]
defsave("personnel", "person" .. i, person)
end
here britzl what you mean exactly didnt understand the person which you mention as string how pass
or sth I didnt understand well please can you see it again and if you can say again maybe what you meant?
thanks
I meant that each person is a Lua table with some key value pairs. Your solution adds each key value pair one by one. I suggest saving the entire person (the entire table) in one go. The Defold function sys.save() allows you to save an entire Lua table with all values (and I’m assuming DefSave uses this function).
can you please tell me @britzl if you can if I use the deep copy function is possible write the load file to a table can this be done also by other way. Also the deep copy code didnt understand it well what exactly returns the copy of table? and you put it as value to other?
I think this 3 days try do this has make my brain a greek salad… Thanks I hope you can please help me
The way you originally approached the problem and iterated the values of each person works, you don’t have to change it.
What I wanted to show was that there is an alternative solution where you could actually save the whole personnel table or the individual persons to without going through each value on each person.
Not really. I don’t know the details of DefSave. I think you would be better of using the built in functions to save and load Lua tables. It is very easy:
-- this is the table of personnel
-- we save and load the entire table in this format
local personnel = {
{
text = "John Smith",
wage = 1200,
photo = "Handsome_guy1.jpg",
pos = "Editor",
lvl = 12,
time = "23:00"
},
{
text = "Alice Smith",
wage = 800,
photo = "Handsome_girl2.jpg",
pos = "Journalist",
lvl = 1337,
time = "23:00"
},
}
-- this generates a filename for a file named "personnel" in a folder "mygame" in a platform dependent location
local filename = sys.get_save_file("mygame", "personnel")
-- this is how you save the entire "personnel" table
sys.save(filename, personnel)
-- this is how you load the entire "personnel" table
personnel = sys.load(filename)
print(personnel[1].wage) -- 1200
print(personnel[2].text) -- "Alice Smith"