Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Does this work?

Discussion in 'Scripting' started by frankiwinnie, Aug 16, 2022.

  1. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Hello everyone,

    I have a method that runs when a UI Button is clicked. So what I want to do is that when the button is clicked the triggered method has to affect later in the runtime when some other thing happened. Example: In a turn based game the method will be executed the turn after the turn that the button has been clicked. So if I click the button on turn 1, the method has to run on turn 2.

    Will using a coroutine with WaitUntil work, if yes is it a good practice? I am considering something like this:

    Code (CSharp):
    1. public void ExecuteFunc() // this will run when the button is clicked
    2. {
    3. StartCoroutine(WaitUntilNextTurn())
    4. // Do something
    5. }
    6.  
    7. public IEnumerator WaitUntilNextTurn()
    8. {
    9. yield return new WaitUntil (() => nextTurnStarted = true;)
    10. }
    I considered that throughout my game I am using a lot of WaitUntil, I was wondering is it a good practice or not? And what other solution that I can implement for the method to be executed for a future event?

    Thanks a lot
     
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    192
    In turn based games you need absolute control of the execution order. Everything that matters has to run sequentially. You can not use coroutines. You need a manager which can store your functions in one way or another until they are needed.
     
  3. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,459
    Where exactly do you see the benefit of this solution vs simply having a global "executing_turns" variable (in some manager singleton) which decides whether the game should currently listen to UI inputs (if false) or do whatever actions for this turn have been queued up?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    The beauty of UI buttons is that they are just fancy delegates, and you can subscribe whatever method you want (with the correct signature).

    So no need to over complicate matters with co-routines. Just subscribe the means to advance the game state to the button. This can be done dynamically as well to re-use the same UI inputs.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Well, that's not true. Coroutines do implement a classical state machine and therefore can be used as such. In the past I made some custom yield instructions, for example to wait for certain events like an UI button press (pastebin).

    So yes, you can use coroutine for almost everything :) However I would not recommend them for the main game mechanic since they have one big issue: You would have a hard time to save and restore your current gamestate. It's fine for some dialog or action sequence since that's what coroutines actually do, executing things in sequence. Though I would not make much sense to represent the whole game state in a coroutine.

    A classical FSM would make more sense, mainly because it is more flexible. Coroutines run sequencially while a normal FSM allows you to jump to any state you wish. This is especially important for loading / reloading a game state.
     
  6. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Thanks to everyone replying