Search Unity

public/private fields on ScriptableObjects

Discussion in 'Scripting' started by reviyee, Mar 19, 2020.

  1. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    I want to make information containers for my game's enemies with ScriptableObjects.
    Should I set the variables I want to access from other Scripts referencing the ScriptableObjects as public or private with getters?

    Which one is safer?

    Code (CSharp):
    1. public class Enemy : ScriptableObject{
    2.     public string _eName;
    3.     public float _damage;
    4. }
    5.  
    6. public class Enemy : ScriptableObject{
    7.     [SerializeField] private string _eName;
    8.     [SerializeField] private float _damage;
    9.  
    10.     public string Name { get { return _eName; }}
    11.     public float Damage { get { return _damage; }}
    12. }
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,104
    That's like choosing between two shotguns, with one of them having a bigger safety switch, and asking which one is safer.

    I don't know man, who's gonna use it?
     
    miketsudev_unity likes this.
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Second way is best.

    You can update the properties to look like:
    public string Name => _eName;
     
    reviyee likes this.
  4. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    Would that work only as a getter?
     
    Last edited: Mar 19, 2020
  5. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    Thing is wanna make an MMORPG so I gotta make super safe. So the one with the bigger safe actually IS better.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Related to using ScriptableObjects, remember that their lifetime in the editor begins when you first access them, and then they do not go away until you exit Unity.

    This may seem irrelevant, but this means you can't rely on OnEnable/OnDisable like you can on Monobehaviours.

    EDIT from June 19, 2021: Unity has changed. When I wrote the above you couldn't rely on OnEnable / OnDisable with every run. Sometime between then and now, Unity began to properly support the OnEnable / OnDisable calls for ScriptableObjects at each STOP->PLAY->STOP transition. But don't count on it! Test your version of Unity to find out, it will take 30 second of adding a Debug.Log().

    Another subtlety comes from private variables, such as if you have an "initialized" boolean in there (private) that one of your getters use. That boolean will be preserved between times you press PLAY in the editor, unless you decorate it with a NonSerializedAttribute:

    Code (csharp):
    1.     [NonSerialized] private    string    TheData;
    That way every time you press PLAY, TheData will be restored to null, as you expect with other classes.
     
    Last edited: Jun 19, 2021
    Michoko likes this.
  7. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    Yeah, I learned that a while back while playing around with ScriptaclbeObjects. I wanna use them only to retrieve data.
     
    Last edited: Mar 20, 2020
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,141
    You don't have to make every little aspect of the code "super safe". Both approaches would be perfectly fine.
     
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,104
    WUt?
    You're not saving yourself from client hacks if that's your concern.
    You're just saving yourself from having another programmer (or yourself) mindlessly editing your SO during runtime, which is completely unnecessary and likely won't happen by accident.
    What you're not saving yourself from, is an illusion of being "protected".

    Like I already said, from whom? You should've answered to that question immediately, instead of acting smart, because you obviously don't understand the purpose of getters and setters in the first place.
     
  10. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    My main concern are players modifying the memory and getting stuff easily. No need to be an ass. If you wanna teach me something, go right ahead.
    .