ID:157106
 
I want to be able to save a list of objects to a text file. Like if List A as /obj/object1 and /obj/object2 in it, have it write that to the text file.
For some reason when I try it, it gives me a text file with spaces.
Did you try writing the obj's directly or their "type" variable?
You have a few choices you have to make. Do you want the objects to be saved in their "as displayed as text" form (ie: the type /obj/object1 saves as "/obj/object1" while an instance of /obj/object1 saves as "object1" unless its name has been changed, etc), or do you need some way to encode more information about the data into the file? What do you want to use as a delimiter between objects?

I will assume "yes" for the former question and newlines for the latter.

You could create a function that converts a list of things into text and use text2file().
proc/list2text(list/L)
if(!L || !L.len)
return ""
var/text = "[L[1]]"
for(var/i = 2 to L.len)
text += "\n[L[i]]"
return text

This allows you to control the textual representation of your list, such as using something other than \n (newlines) for delimiting, representing objects different ways (You could give objects a toString() function that converts itself to a string and have certain objects encode useful information about themselves into the text), and other things.

If you don't care what it is like and you want something even simpler, Byond does have a list2params() function built in which does about the same thing but saves a list in a format that can be used for things such as HTTP parameter strings.

Then you can always put a list into a text file.
proc/my_ListOfObjects_Saver(list/L)
var/text = list2text(L)
text2file(text, "myFile.txt")

or
proc/my_ListOfObjects_Saver(list/L)
var/text = list2params(L)
text2file(text, "myFile.txt")

If you need to load the information back up later, there is a params2list() function that takes the parameter string and converts it back into a list. If you create your own list2text() function then you will probably also want to create your own text2list() function to reverse it, which you don't have to worry about if you go with list2params()/params2list() instead.