Part 3. Replace Dictionaries with Serialized Classes
- Sam Walet
- 3 jun 2022
- 2 minuten om te lezen
Now we could detect any type of property however from here it got more complicated. We wanted objects to change and react to certain triggers like radioactivity levels. This meant that aside from the player using a tool to detect levels the objects would have to internally track their radioactivity etc. so that they could react accordingly. Here I may have been tempted to have a dictionary where the key is the property type and the value the current value of that property on this object, however there is one problem there. Dictionaries arenāt serialized in the inspector. Luckily there is a simple workaround, a serializable class. You simply make a new class with all the info you want to store and then mark it as System.Serializable. Now if you make a list of the class all the information will be neatly serialized in the inspector. It is also worth saying that the reason we are not storing the values in the Property class directly is that it is functioning as an enum and we want to store these values per object and not globally.


Now you have a neat list where you can add any property and assign it a value. However this current code also means that any starting values will get overridden if a source is detected, an object's inherent radioactivity and the radioactivity it receives from outside are one and the same here and cannot be separated, this would later become a problem. The first next step was to make it so the script also took sources into account. If it was a source of something it should add the values from the source and detector together to get the accurate property level of the object.

This would give an accurate value but there was one more step to make, they needed to be seperated. Because so far I had been working with oxygen and temperature but now I was starting to look at properties like mass. Mass is not passively in the air and an object cannot be a source of mass. This prompted me to seperate inherent property values and detected property values by adding a boolean in the Property class which denoted it as inherent or not. I also made another change, instead of having to input the properties by hand it created them based on the list of all properties we already had. This way all the designer had to do was set inherent values and then add properties and detectors accordingly. I did this using the OnValidate function which triggers whenever the inspector is updated, this way as soon as you assign the property list it creates the base values list.





Opmerkingen