- Home /
How do you use serialized values at creation?
For instance, let's say I have a class called "PlayerStats" which is attached as part of some player monobehaviour. "PlayerStats" has one int field representing maximum health. This is serialized so it is editable in the inspector. I also have a variable called current health (not serialized), and I want this variable to start out at the value of the maximum health input by the inspector. The default constructor is, of course, called during deserialization upon starting the game so it seems that the logical thing to do would be to set the "current health" to the "maximum health" in the default constructor. However, if I try and access the "maximum health" field during the constructor, it does not yet have its serialization information, so at the time of this call the "maximum health" will still be equal to 0 and not whatever was input in the inspector. These values are not set until AFTER the constructor is completed. What is the proper way to do something like this?
How familiar are you with serialization? Not to sound rude but what you are describing doesn't sound like a proper implementation of serialization, at least as far as I know.
This is serialized so it is editable in the inspector.
Once something is serialized it wouldn't be editable in the inspector.
The default constructor is, of course, called during deserialization
Deserialization could happen in the constructor but not the other way around.
Not trying to pick on you, just trying to see where our descriptions differ so I can try and help. Have you watched the live training video on persistence? It is a pretty good place to start.
Perhaps I was describing it wrong, so let me show you what I meant.
Once something is serialized it wouldn't be editable in the inspector.
I marked this class with the Serializable c# attribute, and marked several fields with SerializeField. Doing this causes an instance of the class to be editable in the inspector when said instance of the class is a serialized variable of a monobehavior and thus visible. This even works properly in game.
Deserialization could happen in the constructor but not the other way around.
I think I might've confused some terms on what I originally said, but what I meant is that when these instances are being created through this data inputted into the inspector as shown above (which I thought was 'deserialization'), a call to the default constructor is called. Here is the default constructor of the original serialized class. As you can see, calls are made to this constructor on startup for every instance of the original class that has been defined in the inspector. All I am trying to do is access the information passed in through the inspector during the creation of an instance. At the time that this constructor is called, the information from the inspector is not yet accessible so if I were to try to print the nickname field to the console, which I showed earlier to have a default value of "", it would write an empty string ins$$anonymous$$d of the inputted name. After the constructor though, that information works just fine as you saw in the battle screenshot.
Like my original question said, I would like to know how to use information passed in this way to assign things like default values based upon the passed information. The example I gave that is still a problem is if we said:
I also have a variable called current health (not serialized), and I want this variable to start out at the value of the maximum health input by the inspector.
Because the maximum health input by the inspector is not defined until after the constructor, I can't assign it to the current health. I actually work around this in my game by using a "damage" amount that starts at 0 and calculating the "current health" bu subtracting that from "maximum health", but I've thought of several cases where I'd like a default value to use information passed at startup.
Honestly, although I'm sure there's reasons why this isn't possible, I wish unity could just let you input the parameters for a constructor in the editor and create instances that way.
So, any thoughts? And maybe you could tell me the terms I'm looking for to describe this if they aren't serialization? $$anonymous$$aybe there's an entirely better way to go about this? Thanks!
Answer by WilliamLeu · Jul 22, 2014 at 05:13 PM
Do NOT EVER use or implement the constructor for MonoBehaviours. Use either Awake() or Start().
Awake is called when the behavior is first created (probably what you want to use for your case)
Start() is called the first frame a GameObject is visible (after awake).
I don't think you read the question very well. At no point are constructors being used for monobehaviours. They are being used on regular c# classes. Instances of these classes exist on monobehaviors, but the monobehaviours themselves do not use constructors at all.
Touché, right you are. Luckily the answer is still the same but for different reasons. Iterate through those objects in the Awake() or Start() of the $$anonymous$$onoBehaviour and set the desired values.
ohhhhh myyyyy goddddd I can't believe I never thought of using the monobehaviour to set the defaults of the objects they hold. I knew it was such a simple thing there had to be an easy way to do it. Next time I need this I'll just throw an init method on the object and call it for each component at the start. Damn! What an easy fix. Thanks a lot, sometimes I just need another $$anonymous$$d to look at something and see the simple solution I can't cause I've been overthinking it for too long hah. Anyways, thanks again!