Search Unity

Void Update and best practices

Discussion in 'Scripting' started by baazul, Aug 2, 2019.

  1. baazul

    baazul

    Joined:
    Jul 29, 2018
    Posts:
    42
    Hello,

    I have few values like "attack damage", "armor" and "experience points" displayed on the main screen that are updated often from various source like Gathering X stats points, equiping or unequiping an armor, getting a buff from whatever...

    - The easiest way to update the value displayed on screen would be to have it in Void Update. This requires only 1 line of code for each
    - Another way would be to update the value displayed on screen whenever there is a change. This require the same line of code multiplied by all the events changing the value.

    Even if my game is quite super small and will probably not require a lot of resources I wonder if updating values in a Void Update is a good practice or not.

    What do you think ?

    Thank you
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Doing it in update is not an unforgivable sin -- my rule of thumb is to what is easy and works until it doesn't. It's more important to stay flexible than it is to craft perfect code.

    Side note, "void" isn't part of the method name -- it's the return type. We will know what you're talking about if you just say "Update". :p
     
  3. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    In this scenario, you assume that classes that are modifying that value are responsible for letting the GUI know. In reality, your game logic should know nothing of your UI. Your UI should be responsible for updating itself.

    One way of doing this is reading the value every frame like you're doing, but that is poor design because so many of the read calls are going to be useless.

    You can do it this way ;

    Whatever class that contains the stats data should be responsible for emitting events when said data is modified. Say your armor is increased by 10, it basically goes : Attention attention, to all listeners, the armor was just modified.

    Your UI classes listen to events that modify the data they're responsible for displaying. Your armor view would listen to that call, and when it's called, it'd update itself.

    See, super fun stuff. If you do it this way, you can delete your entire UI in the scene and not run into a single runtime error. You can delete your entire UI scripts collection and not run into a single compilation error.
     
    Eralven, ModLunar and baazul like this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Updating UI Text every Update is bad for performance in that it is generally unnecessary and probably creates GC you don't need to. Something bad for performance doesn't necessarily mean it is bad for your game though. In my game I do tons of things that are technically bad for performance out of expediency because to do everything in the best performing way could tack on another year to my already behind schedule development time.

    What matters in the end is if the game performs within your requirements/expectations without egregious bugs, and is not unnecessarily difficult to maintain your code. If what you wrote satisfies that, then you're fine IMO.

    Note that an alternative to updating UI every Update is to update UI on a timer. For example, if you update your UI text once per second instead of every frame, the user may hardly notice the difference. But at 60 FPS you end up doing 1/60th the number of UI updates.
     
    wmadwand and baazul like this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    There is a third option available to you. Simply combine the two of them. Checking if two values are equal (or in the case of a float approximately equal) has more performance than constantly updating the UI, but at the same time doesn't require you to set up events, set up code for the events, verify they're all working, etc.

    Code (csharp):
    1. float lastAttack;
    2.  
    3. void Update()
    4. {
    5.     if (Mathf.Approximately(attack, lastAttack) == false)
    6.     {
    7.         // instruction to change text value goes here
    8.  
    9.         lastAttack = attack;
    10.     }
    11. }
     
    Last edited: Aug 2, 2019
    Eralven, wmadwand and baazul like this.