2015-12-13 11:09:33 (edited by audiogamer21 2015-12-13 11:11:07)

Hi,

I'm trying to figure out how to do serialization/deserialization in BGT. I've found 2 approaches to doing so: the built-in serialization methods in BGT, and using a user data class that someone on the BGT forum wrote in an attempt to automate the process. The user data class can be found at http://www.blastbay.com/forum/viewtopic.php?id=1433

Which approach would be better? I have numerous classes (over 20 so far), and arrays that agrigate instances of different objects. Each object have primarily primitive types as data members, but some objects have more complex data members (e.g dictionaries).

If I understand BGT's serialization functions correctly, I have to implement save and load methods for each class. I'm not too sure about how it'll work though. Would I have to store everything in 1 gigantic dictionary? Or is each object its own dictionary?

For example,

X[] xObjects; // this could contain any number of x objects
Y[] yObjects;
...

2015-12-13 17:03:43

So far as I can tell, the serializations work only with primitives, either in dictionaries or with the user_data class. They're pretty useless if you want to save more complex objects, unless you can come up with a way to read and write the properties of those objects to a dictionary, at which point you're probably better off writing your own methods.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-12-13 17:18:26

So if I wanted to store multiple arrays, each array could contain 0 to any number of objects, and each array has a different type of object, how would I go about doing it?

The only thing I can come up with is to do the following:
for each array, store its length. For example, a key called xObjects.length.
Then for each array's elements, the key in the dictionary would be <array name>[<index>]. e.g xObjects[0], xObjects[1] ... xObjects[n-1]

The only problem is that dictionaries only work if values are of the same type, so I think I'll need more dictionaries. How do I extend this scheme so that BGT can save multiple dictionaries?

2015-12-13 18:40:01

Dictionaries don't require the same type; that's arrays.
I'm not sure if there's a better way of serializing multiple dictionaries than using multiple files. You could store the data in one file, using a separator string, but that string must not appear in the data at any point. Then you'd read the data and split it into a string array with string_split, then use a for loop to deserialize each dictionary.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-12-13 19:20:09

Oh i didn't know that bgt dictionaries allowed mapping of string keys to arbitrary values. The string separation approach sounds risky, because if the serialized data has that string at any point, things will go very wrong.

I think it allowing different types for values would solve my problem. The key in the dictionary would just be the name of the array, followed by its length and indices. Thanks for the help smile