Search Unity

Question How to process two quick, successive C# events?

Discussion in 'Scripting' started by dkalkwarf, May 28, 2023.

  1. dkalkwarf

    dkalkwarf

    Joined:
    Mar 29, 2020
    Posts:
    13
    I am using .Net events. Two of my events fire in quick succession. The problem is that the second event fires before I am done processing the first, which causes problems.

    I realize I can use some sort of flag to ensure that the first event's processing is complete before processing the second, but this feels like a hack (?). Is there a good way to make sure that Unity finishes processing one event before moving on to the next?
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    No, at least not in a sense of something automatic, that's your job. What exactly is the problem?

    If the actual situation is too complicated for the forum try building a contrived example, and I can help you fix the issue.

    When presenting the problem try thinking beyond what you believe must be the best solution. Maybe the best solution is having two events and then tracking their order, maybe it is not.
     
    Bunny83 likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    An event will be fully called before returning to allow for the next event to be called.

    The only way it wouldn't is if you perform some sort of asynchronous logic within the event handler. Say you start a task or coroutine from the handler, or if you marked the handler 'async'.

    Don't do this if you expect the method to complete first.

    ...

    If I'm not understanding your question, as orionsyndrome pointed out, post a more detailed explanation or even a contrived example.
     
    Bunny83 and orionsyndrome like this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    I missed to clarify that, indeed
    This is not possible. The way I understood this is "I am processing the events in the wrong order" which is probably what happens to you. You cannot partially process a synchronous event and be interrupted in the middle of that.
     
  5. dkalkwarf

    dkalkwarf

    Joined:
    Mar 29, 2020
    Posts:
    13
    Sorry, I misspoke regarding the problem. I am not receiving another event before processing the first completes.

    I am refactoring to implement a different system for handling state. The system evaluates state transition in each frame (Update). Transitions are not complicated. For example, if I am in State A and condition X is true ( i.e. event XYZ), then transition to State B.

    The problem is that I am still in the same frame when the second event fires - I didn't get to the next Update so I could evaluate and make transition. This causes problems because I haven't transitioned to State B (still in State A) and the second event is illegal in State A.

    Can I force Unity to move to the next frame after processing an event?

    If it matters, this is a multiplayer networked game and the events eventually result in a ClientRPC call.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,193
    You can't force the engine to move to the next frame but with a coroutine you can wait till the next frame.

    Code (csharp):
    1. private IEnumerator MyCoroutine()
    2. {
    3.     // fire event A
    4.  
    5.     yield return null;  // wait a single frame
    6.  
    7.     // fire event B
    8. }
     
    Kurt-Dekker likes this.