For future readers: no trick. C# can serialize arrays or lists of strings just fine.
My particular problem was I was calling an array not marked static, with a Type.
I need to serialize arrays of strings. I have not run into such a hassle with serialization before, and I am looking online to see, but it looks like binary serialization of string arrays may be a little tricky.
I know I can serialize/deserialize strings, but string arrays just seem to not serializing, and creating 0kb files.
I have Odin, but I need to homebrew this.
I know I can save one giant string, since I see the max amount of elements an array can handle is 4 billion, so I can save an array of “pointers” that get that many characters from the string, but it seems hack-y, and it seems like there should be a simple way to serialize a string array in C# by now. I just dont know if I am trying the impossible, or doing something wrong.
// out
List<string> lines = GetYourContent();
using(BinaryWriter writer = new BinaryWriter( ... your stream, probably a file ... ))
{
writer.Write(lines.Length);
for(int i = 0; i < lines.Length; ++i)
writer.Write(lines[i]);
}
// in
List<string> lines = new List<string>();
using(BinaryReader reader = new BinaryReader( ... your file again ... ))
{
int length = reader.ReadInt32();
for(int i = 0; i < length; ++i)
lines.Add(reader.ReadString());
}
I’m not quite sure what you have tried so far. I’ve usually used BinaryFormatter, it is more straightforward than using BinaryWriter:
[SerializeField] List<string> texts = new List<string> { "foo", "bar", "baz" };
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log("Save the list to a file");
FileStream fileWrite = File.Create(Path.Combine(Application.persistentDataPath, "data.sav"));
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fileWrite, texts);
}
if (Input.GetKeyDown(KeyCode.L))
{
Debug.Log("Load the list from a file");
FileStream fileRead = File.Open(Path.Combine(Application.persistentDataPath, "data.sav"), FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
texts = (List<string>)bf.Deserialize(fileRead);
}
}
I’m sorry to rain on your parade, but you should stop using binary formatter:
Don’t use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.
Since it is already 2021 you should consider using something like JSON to serialize and store your data.
Thank you for all the wasted effort folks. Turns out I did not mark an array as static, but was calling it with a type, and that was causing all sorts of problems. (not sure why compiler did not let me know, probably just one of the pitfalls of using statics).
Also, I became aware of the security problem with binary serialization via MS documents while trying to figure this all out. In this case, I am doing all this nasty string/dictionary work for a mod panel, so players can create their own outfits/characters etc so I think if they want to modify the file, they would be far more likely to use the mod panel than to try and crack a binary to change the data.
I have been so comfortable with binary that I never even really had to look up MS docs.
But, I am concerned a bit about the security flaws. I don’t know if enough to change my binary habit. Also seems to me changing memory format is not a big deal, even across a wide project, so, I’ll cross that bridge when I get there.