Event System, callbacks, or any other solution?

Hello again guys,
I have another question about my game design.

Let’s say I’m working on a turn-based game in which two teams face each other. In each “round”, all the characters on the screen will take their turn, sorted by speed. I have designed a TurnManager class to control the turns and the question comes next:

  • Should I implement an EventSystem (which can be used for any other event present in the game) where it fires a TurnFinished event every time a character performs its action?
  • Should I follow a callback implementation, for example by adding a public Action to the Character class and referencing it in the TurnManager class: i.e. character.OnTurnFinshed += “Method of TurnManager class”?
  • Is there any other solution that I am not aware of?

I would like to know your opinions or suggestions on how to approach this because it is being a headache since I started to think about it.

Thank you very much in advance for your help!

promised based system… something like async:

Instead of having a callback, the method returns a Task/ValueTask/UniTask/whatever-a-promise-task. Then you can await it to be finished and you know the turn has completed.

public interface ITurnHandler
{
    Task TakeTurn(object turndata);
}

//just loop over your turn handlers on the turn in sequence awaiting each one taking their turn to simulate a "turn-based" system
foreach (var eligible in eligibleTurnTakers)
{
    await eligible.TakeTurn(this);
}

I had not thought about asynchronous programming for this… how interesting! The only thing I am not clear on is that, as in other turn-based games, the player characters will have to wait for the desired UI buttons to be pressed. My understanding of your solution is that I have to wait for an OnClick button event, but is this possible or does it make sense? I may have said this before, sorry for not being specific.

I only suggest it cause you mentioned callbacks… and async promises can sometimes be a good replacement for callbacks.

1 Like

You can use TaskCompletionSource to mix UI events with async code:

  • Create a TaskCompletionSource and await it’s task
  • Listen to the button click event, once the button was clicked, call the TaskCompletionSource SetResult method
  • The TaskCompletionSource task will be “completed” and the awaited code will continue
1 Like

This might be a worthwhile watch if you want to go down the async route:

A functional programming paradigm sounds like an interesting approach to a turn-based game. I imagine this might’ve been how older turn-based games operated.

1 Like

I haven’t actually made a turn based game, outside of console apps when I was first learning to code. But, I imagine you could have a queue that handles initiative, turn order, and rounds. That ordered collection of turns and actions that get queued up by player or AI controller decisions during a turn keeps repeating until a win/loss condition occurs.

Parts like HP, status, and defeat should work as usual. If a character dies they drop out of turn order. When one side is defeated, the other side wins. (You can come up with other fun win/loss conditions later.)

Then, you just have to define what constitutes a turn for your game, how do you define and modify what a character can do, and how do the player and AI controllers interact with characters during their turn.

Using events and callbacks in appropriate places is a good way to loosely couple, and easily link behavior. But, it won’t solve everything. The key is to set up a state machine that moves through the turn order, that works and plays the way you want it to. Start with simple characters. Then you can begin customizing new characters, items, and actions.

1 Like

Thanks for all your comments. After reading them, they may work but I am not able to see how I can implement them in my game. So I will try to show a diagram of what I have just to see if I can explain my problem in a better way:


In the picture above you can see some of the classes that I am using for my game. The question I have is how the TurnManager detects that the turn ends, since the execution of the BattleCommand is triggered from the UI part after pressing the desired UI buttons.
Since I have events and an EventManager that triggers them, the most obvious way should be this…but the EventManager is in a Common assembly that needs the Characters assembly, so anything inside the Characters assembly won’t be able to fire events.
I thought about creating events in BattleCharacter like:
public event Action OnTurnFinished.
And fill this event in the TurnManager during turn sorting but it’s like I’m using two different event implementations and it seems wrong to me.
Async could be another possibility but possibly due to my lack of programming knowledge I’m not able to see how to implement it since I’m sending an event from TurnManager to UIManager in the middle of the process.
Having said all this… can you think of any other possibilities? I don’t mind changing all my code if necessary as I want to learn how to do things in a way that makes sense.

I hope I have made myself clear now and sorry for any previous misunderstandings.

Thank you very much in advance guys!

PS: The CharacterManager class registers a character when it spawns and unregisters it when it dies. This class is part of the Battle assembly so it is possible that it triggers events.

I would suggest holding off on moving parts of this overall system into separate assemblies until you have the whole thing working, at least at a basic level. If the common assembly and the Characters assembly both need the EventManager, then it sounds like EventManager should be in its own assembly that each can reference. But, like I said, I wouldn’t start breaking each module up into separate assemblies until you have the big picture working first. Even then, the question is always why are you separating the assemblies? What benefit are you getting? It’s good to have an idea of the dependencies between classes in mind, but things change so often in prototyping that it seems too early to be compiling pieces of the whole into separate assemblies.

Using standard C# events and delegates (and UnityEvents) is fine, even when you have your own EventManager system, but you’re right that you should have some sort of standard about when and why you use one or the other. It shouldn’t just be arbitrary which one you use, but using both in one code base is not inherently bizarre.

The character instances at run time should have some sort of action economy that they deplete during their turn. Usually the character or AI will determine that they choose to end their turn, but if it’s not possible to take further actions because you detect that there are no further resources for taking an additional action on that turn you could automatically end the turn depending on your design.

1 Like

I agree that you shouldn’t worry about splitting assemblies at this point and make it easier for yourself.

“The question I have is how the TurnManager detects that the turn ends, since the execution of the BattleCommand is triggered from the UI part after pressing the desired UI buttons.”

This is a personal preference, but for a turn based game I think differently about characters than in a real time or action game.

In a real time action game I add a “brain” (for example state machine) to every character and they have their own logic to execute their own abilities and do their own stuff etc.

In a turn based game I usually do it differently, there I have some kind of battle or turn class which has the logic and it uses the characters as “puppets”.
My turn class will know when a turn starts or ends and run all the code that should be executed in both cases, it will handle the ability selection (either tell the AI character to return a ability or show the UI for player characters), the UI will notify the turn class when a command is selected and then the turn class will run the logic of the command (by running the logic I mean, that I have some kind of interface for the commands and the turn class will use this interface to call some kind of start, update and cleanup methods, actual command logic is not implemented in the turn class itself).
So it will also know when the command is done.
The characters don’t have their own “brain” they will only do what the turn manager tells them to do, like play animation, reduce some stat values etc.

Personally I would use async paradigm (using the awesome UniTask library) like @lordofduct suggested, combined with a TurnSystem that finds and execute all processors you created, something like this:

public interface ITurnProcessor
{
    int ExecutionOrder { get; }
    UniTask<TurnProcessingResult> ProcessTurnAsync(TurnContext context);
}

public enum TurnProcessingResult
{
    Success,
    Failed
}

// A data class shared between all processors, like a Blackboard,
// where a processor can access, modify or set data for the next processors to use
public TurnContext
{
    public Player Player { get; set; }
    public List<UnitsDestinations> UnitDestinations { get; set; }
    public AnyOtherNeededProperty { get; set; }
}

Some examples of TurnProcessors:

public class MoveUnitsTurnProcessor : ITurnProcessor
{
    // ITurnProcessor implementation
    public int ExecutionOrder => 0;

    // ITurnProcessor implementation
    public async UniTask<TurnProcessorResult> ProcessTurnAsync(TurnContext context)
    {
        // Move each unit, one by one, to its destination.
        // if we want to move them all at the same time,
        // we use await UniTask.WhenAll() to run all tasks
        // in parallel
        foreach (var unitDestination in context.UnitsDestinations)
        {
            await MoveUnitAsync(unitDestination);
        }

        return TurnProcessorResult.Success;
    }

    private async UniTask MoveUnitAsync(UnitDestination unitDestination)
    {
        var unit = UnitDestination.Unit;
        var unitTransform = unit.Transform;
        var destination = UnitDestination.Destination;

        // Set unit destination (using NavMeshAgent for example...)
        unit.SetDestination(destination);

        // awaits until the Unit has reached its destination
        await UniTask.Until(() => Vector3.Distance(unitTransform.position, destination) < 0.05f);
    }
}
// UI/Event based TurnProcessor
public class WaitForUserSelectionTurnProcessor : MonoBehaviour, ITurnProcessor
{
    // Used to convert a normal event to Async...
    private TaskCompletionSource<TurnProcessingResult> _clickTaskCompletionSource;
    private TurnContext _turnContext;

    [SerializeFiled] private GameObject _selectionCanvas;
    [SerializeField] private Button[] _buttons;

    private void Awake()
    {
        foreach (var button in _buttons)
            button.Click += OnButtonClick;
    }

    private void OnButtonClick()
    {
        // button clicked, do some actions...

         // Set the async TCS result...
        _clickTaskCompletionSource.TrySetResult(TurnProcessingResult.Success);
    }


    // ITurnProcessor implementation
    public int ExecutionOrder => 1;

    // ITurnProcessor implementation
    public async UniTask<TurnProcessingResult> ProcessTurnAsync(TurnContext context)
    {
        // Show the buttons canvas...
        _selectionCanvas.SetActive(true);

        // Create the TCS:
        _clickTaskCompletionSource = new TaskCompletionSource<TurnProcessingResult>();

        // Set the current TurnContext, which can be used in the buttons click event
        _turnContext = context;

        // Awaits and get the TCS result, set from the buttons click event callback
        var result = await _clickTaskCompletionSource.Task;

        // Hide the buttons canvas:
        _selectionCanvas.SetActive(false);

        // Return the result
        return result;
    }

}

The TurnSystem:

public class TurnSystem : Singleton<TurnSystem>
{
    private readonly IReadOnlyList<ITurnProcessor> _allTurnProcessors = GetAllTurnProcessors();

    public async UniTask<bool> ExecuteTurnAsync(TurnContext context)
    {
        foreach (var processor in _allTurnProcessors)
        {
            var result = await processor.ProcessTurnAsync(context);

            if (result != TurnProcessingResult.Success)
            {
                // One of the processors failed, we just stop executing turns for example
                return false;
            }

            return true;
        }
    }

    private static IReadOnlyList<ITurnProcessor> GetAllTurnProcessors
    {
        // We used reflection here. We could also make processors ScriptableObjects
        // and assign them directly from the Editor, which will nicely solve
        // the execution order problem too.
        var result = new List<ITurnProcessor>();

        var processorsParent = new GameObject("Turn Processors (MonoBehaviours)");
 
        var interfaceType = typeof(ITurnProcessor);

        var allProcessorsTypes = interfaceType.Assembly.GetTypes()
            .Where(x => !x.IsAbstract && interfaceType.IsAssignableFrom(x));

        foreach (var processorType in allProcessorsTypes)
        {
            // if it's a MonoBehaviour, we create a game object.
            // We could also do a FindObjectOfType if the object is already in the scene.
            if (typeof(MonoBehaviour).IsAssignableFrom(processorType))
            {
                var go = new GameObject(processorType.Name);
                go.transform.SetParent(processorsParent.transform);
                result.Add((ITurnProcessor) go.AddComponent(processorType));
            }
            else
            {
                result.Add((ITurnProcessor) Activator.CreateInstance(processorType));
            }
        }

        return result.OrderBy(x => x.ExecutionOrder).ToList();
    }
}

Finally, in your “Execute Turn” button, you just do:

private async void ButtonClick()
{
    // Fill your turn context...
    var context = new TurnContext
    {

    };

    var turnSuccessful = await TurnSystem.Instance.ExecuteTurnAsync(context);
}
1 Like