Hi i am working on a node based editor and for input slot and output slot i would have something like Dictionary<guid, int>, the input and output classes are serialized and the node itself is a scriptable object, i thought the dictionary was getting serialized properly until i saw this :
Hint: Unity won’t serialize Dictionary, however you could store a List<> for keys and a List<> for values, and sew them up in a non serialized dictionary on Awake(). This doesn’t solve the problem of when you want to modify the dictionary and have it “saved” back, but it is a handy trick in a lot of other cases.
well, i could keep track of two lists, one for guid and one for int indexes i am trying to track in slots, but the problem is, i won’t have Awake() or anything like that in the output and input classes, so the question is, how can i handle this? i would like to have Dictionary but i assume by that comment, no matter if the dictionary is sitting in a serialized class and that class is being used as a field inside a scriptable object, it won’t get serialized ?
for clarifying, take a look at snippet bellow
[Serializable]
public class Input
{
public Dictionary <Guid, int> connectionID = new Dictionary <Guid, int>
}
[Serializable]
public class Node : ScriptableObject
{
public List<Input> inputs = new List<Input>;
}
so, long story short, it won’t get serialized, how about the solution i found when i was posting this :
it looks like the guy is making the dictionary under the hood and it mimics all fancy dictionary functionality?
I was hoping that unity 5.x finally got dictionaries covered but by your reply it looks like not, then how about that solution? i am developing a utility assembly beside this project and that class looks like handy one?
why exactly you need a dictionary serialized, explain this concept cause i hardly can find a reason to… you probably dont really need it
as workaround you could maybe use a property that convert a list into a dictionary?
its hard to suggest a correct solution if you dont explain what you want to do with this serializable thing , do you need it for a savegame?
well, for the node editor itself i would need to check if the key exists when i am connecting, breaking the connection or evaluating connections on node graph, because in each input/output i should be able to connect more than one output to a single input and one output should be able to connect to more than one input, so basically i have List as child nodes in the output class and List as parent nodes in the input class, that dictionary will be used to track the ID of the node and the corresponding input/output slot being used to bind to current node, so that when i want to break connection on an output slot for example, i would loop through it’s child nodes, get the input index on those nodes that are connected to this output and only break the connection on those slots toward this node, and if that input had more than this output, it won’t break other connections on that input, same goes for when i break connection on the input, i will loop through all it’s parent nodes, look in the dictionary for the output slot that is being bound and only break the connection in that output with this node, so that if the output has more than one node connected, it saves it’s other connections. In those dictionaries, the guid is the unique id of the nodes connected to slot, and the index of the slot on that node that is being used for the connection.
But the major idea is being able to have a serializable dictionary for when ever i may need it in future in project. to be honest, i find myself using dictionaries in more than few places when i am developing projects, the ability to have key/value pair is handful but i never thought about serialization because until now, well, i was not dealing with saving/serializing stuff, the only time i dealt with it was using proto buffer and it was a nested generics, if i recall it was Dictionary<Foo,List> And proto buff handled it nicely, but for my node based editor, i want to stick to what i can get from unity.
You do know that technically a serialized dictionary would still contain collections for all the keys and all the values in it (wether it be 2 distinct arrays, or an array of structs).
The information is needed no matter what.
So, not wanting to have to serialize 2 lists, really has nothing to do with what gets serialized… you don’t want to have to reconstruct the dictionary on deserialize (note, you can do this with ISerializationCallbackReceiver, in case you haven’t used that).
If this is all the problem is, then write a class that takes care of zipping the 2 back up, and then you have it. You’ll have to make concrete versions of the classes so that you get around the generics problem… but that way you don’t have to write the entire serialization process in both places.
Or you could implement your own dictionary, like in the other thread that you brought up prime numbers in. But that’s complex, and if you don’t understand how a hashtable is written, you might mess things up, or serialize unnecessary data. Such as in the case of that other thread where the author had serialized nearly everything (including the enumerator version tracking value… I suspect it was a copy paste job…).
This also means that the serialized space will take up as much as the capacity of the collection… rather than the actual size of the collection.
nothing specifically to check keys or prevent dupes idk
it does serialize
ya might be inefficient with linq
idk. this seems like something that unity should handle instead of forcing each user to re-implement this with their own bugs