Search Unity

Script optimization on start

Discussion in 'Scripting' started by Grzp, Oct 6, 2021.

  1. Grzp

    Grzp

    Joined:
    Jan 25, 2018
    Posts:
    31
    Hello everyone,
    I have a question optimatization. I have a script to player/ camera controller and I want set value to verriable from global Scriptable Object. Like this: upload_2021-10-6_20-8-59.png
    But what with optimatization with this type setting varialbes.When does Unity set varialbe as like this:
    private InputAccess m_inputAccess => GeneralScriptableObject.Instance.InputAccess; it call one time on start or in each time when some function which is using this varialbe. I think about beacuse I can also set variable in Start/ Awake function.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    it doesn't set anything. You are writing a property with a getter that runs that expression every time you call it. For example this is equivalent to your second property:
    Code (CSharp):
    1. private InputAction m_mouseDeltaInput {
    2.   get {
    3.     return m_inputAccess.MouseDelta.reference.action;
    4.   }
    5. }
    Which is also more or less equivalent to a getter method like this:
    Code (CSharp):
    1. InputAction GetMouseDeltaInput() {
    2.   return m_inputAccess.MouseDelta.reference.action;
    3. }
     
    Grzp likes this.
  3. Grzp

    Grzp

    Joined:
    Jan 25, 2018
    Posts:
    31
    So better will be something like this?
    Code (CSharp):
    1.   private InputAccess m_inputAccess;
    2.     private InputAction m_mouseDeltaInput;
    3.     private PlayerStats m_playerStats;
    4.  
    5.     private float m_mouseSensitivity;
    6.  
    7.   private void Start()
    8.     {
    9.         Cursor.lockState = CursorLockMode.Locked;
    10.        
    11.         m_playerStats = GeneralScriptableObject.Instance.PlayerStats;
    12.         m_inputAccess = GeneralScriptableObject.Instance.InputAccess;
    13.         m_mouseDeltaInput = m_inputAccess.MouseDelta.reference.action;
    14.         m_mouseSensitivity = m_playerStats.MouseSensitivity;
    15.     }
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    No.

    That's not to say that you're wrong, exactly - that code does the thing you think it does. But, you're optimizing for a nanosecond during execution (if anything*), while making your script more annoying to read, write, and maintain. And if any of those values could possibly ever change at runtime, you'll be accessing stale data.

    "Premature optimization" is the term for the thing you're doing right now. Optimize for performance after you've profiled and found a performance bottleneck, not before. Computers don't think in ways that are intuitive for humans, and optimizing the performance of these tiny tiny things is an absolute waste of time.

    * You may not be saving any time at all, because it's very possible that those properties are exactly as fast as accessing the value itself.
     
    Grzp likes this.
  5. Grzp

    Grzp

    Joined:
    Jan 25, 2018
    Posts:
    31
    Ok, I understand thanks for replies.