Search Unity

Does Unity Event fire only once during Update()? [Tactical Turn-Based RPG]

Discussion in 'Scripting' started by Jihaysse, Jan 15, 2021.

  1. Jihaysse

    Jihaysse

    Joined:
    Mar 29, 2020
    Posts:
    53
    Hello everyone,

    I'm currently working on a Tactical turn-based RPG on Unity. I made the mistake (I guess?) to switch the state in the Update() function. It kinda looks like this:

    Code (CSharp):
    1.      void Update() {
    2.      switch (state) {
    3.         case State.IDLE:
    4.            /// Do this
    5.         case State.MOVING:
    6.            /// Do this
    7.         case State.TURN:
    8.            /// Do this
    9.    }
    10. }
    Now I'm implementing the monster IA, I'm facing an issue that, for example, when they're in State TURN, they are able to attack multiple times at once (sometimes 2, sometimes 3 when they should only attack once). I guess this is because of the Update() function.

    I tried using a Coroutine but this doesn't work neither.

    I was wondering then if I should (finally) start learning about Unity Events? Will it resolve my issue?

    My goal is that when the monster begins his turn: 1) It checks for the player cell (it is grid based) 2) It moves next to the player cell 3) It attacks the player (as many times as he has enough Action Points)

    Thank you very much for your help!

    Kind regards
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Update() is gonna be called every frame. What you want is to decide that a state has changed and then start some sequence of events that constitute the turn.

    Sequences can easily be done in coroutines, or you can manage your own state machine to do it, such that while you're doing it, no other sequence can be started.