Search Unity

C# best way to fire my own events

Discussion in 'Scripting' started by startas, Jun 18, 2015.

  1. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    I want to do some events in my mobile game, and i need to implement checking of variables all the time. Right now i have made a method that checks if variables values are enough to do event, and method is passed to invokerepeating with interval of 2 seconds. Is there a better/faster way to implement listening of variable values?
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Probably. There are generally two ways to check values: polling and events. You've implemented polling. Often, but not always, events are faster because you only check the value when it changes.

    Here's a C# code snippet of one way to implement events:
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4. public class MyClass : MonoBehaviour {
    5.  
    6.     public UnityEvent onScoreChanged;
    7.     public UnityEvent onWin;
    8.  
    9.     private int m_score = 0;
    10.  
    11.     public int score {
    12.         get {
    13.             return m_score;
    14.         }
    15.         set {
    16.             m_score = value;
    17.             onScoreChanged.Invoke();
    18.             if (value > 100) onWin.Invoke();
    19.         }
    20.     }
    21. }
    This class has a public property named score that has an internal backing value named m_score.

    In the Inspector, there will be two event handler sections, named "On Score Changed" and "On Win". You can assign methods to them.

    When you assign a new value to score, it will update the backing value and invoke onScoreChanged, which will call any methods that you've assigned to the "On Score Changed" section. It will also check if the value is greater than 100; if so, it will invoke onWin, which will call any methods that you've assigned to the "On Win" section.

    The difference between this and your current implementation is that this only checks the score when it changes, not every 2 seconds.
     
    karl_jones likes this.
  3. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Yes i know that way too, but my variables will be changing every frame, thats why i asked. Checking variables every frame will be too much of cpu waste.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    If your variables are changing every frame, and you are proposing to check it every two seconds, keep in mind that something somewhere (either in Unity3D's "multitasking" code (i.e., the Coroutine driver system) or else in your own code) has got to ask EVERY SINGLE FRAME, the following question: "has it been two seconds yet so I can check the other variables?"

    What I'm saying is, don't get too hung up on doing stuff every frame. That's what computers are good at. Only worry about doing big hairy inefficient things every frame (processing large arrays, allocating lots of little objects, etc.).

    Incrementing and checking variables is absolutely not going to affect your framework unless you're incrementing and checking hundreds of thousands or more of such a thing every frame.
     
    ThatGuyFromTheWeb likes this.