Question about defsave and pairs(SOLVED)

Hello dear defolders I have this code

     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?

Thanks a lot for the help

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
1 Like

Thanks britzl I will check it and see , I will let you know to put solved. thanks

this done save normal but I think some is double I m not sure , and cannot see somewhere the numbers of time now I will see the second. Thanks

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).

ok thanks i will try it, thanks

yes it done it I think is ok, so if i make now a load and copy to a lua module can this be done? and have like this the new personnel? thanks

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.

1 Like

@britzl sorry to ask again I still cannot load the table. I done with your code normal the save which become like

person1 = { text = ,  wage = ....}
person2 = { text =

so I do

 defsave.load ("personnel_1")

and after I want to pass to lua module in runtime the values of keys

  for i = 1, #personnel do
			personnel[i].text = defsave.get("personnel_1", "text")
			personnel[i].wage= defsave.get("personnel_1", "wage")
			personnel[i].photo= defsave.get("personnel_1", "photo")
			personnel[i].pos= defsave.get("personnel_1", "pos")
			personnel[i].lvl= defsave.get("personnel_1", "lvl")
			personnel[i].time= defsave.get("personnel_1", "time")

only problem is I cannot access key person1 , person2 etc
normal key is person1.text
I tried this

defsave.get ("personnel_1", "person"..i."text")

but does not work
do you have any idea what is wrong?

 DEBUG:SCRIPT: DefSave: Warning when attempting to get a key 'person1.text' of file 'personnel_1' 
 the key was nil

Thanks very much

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"

Ok thanks I will try it, thanks a lot