[Help]: How to determine event order (Queue or Coroutine)?

Hello everyone! I have a fairly broad question I was hoping to get some direction with. I’m not expecting anyone to write any code, I just need help determining what I need to learn in order to implement a new system into my game.

So I’m making a card game in Unity (C#) in my spare time as a learning exercise and have been going at it since last summer (on and off during school down time). I’m currently trying to figure out how to queue up events and reorder them based on a variable then execute them in sequence. This system is similar to magic the gathering’s ‘stack’.

I will explain a brief in-game scenario: I attack my opponent’s creature → they cast a spell that kills my creature → I cast a spell that counters their spell. I need a system that will take these events and put them in order based on each spell’s speed variable (some spells are faster then others etc.) then execute them in order.

I originally thought I would be creating some type of queue system but upon further reading have determine that I may be looking at a coroutine scheduler. Any guidance on this issue would be greatly appreciated. Also, any resources you could point me to would be great!

Thank you very much!

sounds like you want to look into the command pattern. a design pattern that encapsulates commands themselves as separate classes which can then be stored in a collection. its how many programs implement an undo/redo system. so you would make an a base command class that other classes would inherit from, with examples as a CastSpellCommand class, a CounterSpellCommand class, and SummonMonsterCommand class. It may seem weird making classes of actions, but doing so will give you the functionality you want

these classes should implement an interface (ICommand for instance) that has functions like Execute() and Undo() you then have an iterator traverse a collection of commands calling execute on each, or reversing direction when undo is called (with a class’s undo manually making the calls that would undo what was done in the execute). so for example CounterSpellCommand may call a previous command’s Undo as it’s execute (while not refunding that spell’s resources)

Since you are using an iterator you can run it in a coroutine or via an event call and it shouldn’t matter. You’ll likely want to use a List so that you can iterate in both directions (execute or undo).

@JoshuaMcKenzie Thank you very much for your reply!

I looked into the creation of a command design pattern briefly and it seems as though it would work for my purposes on paper. But, before I invest the next week into learning and implementing this system I think I should describe to you what I already have written in code.

Currently all attacks, spells and abilities are run through a game state machine. It accepts actions only one at a time. So for instance if you click on a creature that is able to attack it will switch to ‘finding target’ then you click the target and if it’s a valid target the attack goes through and it returns to a neutral state. Is there a way to have it so if the opponent chooses to respond to an action the game state switches to ‘response mode’ then when responses are finished it switches to ‘resolution mode’? I suppose I worded that poorly, I feel as thought its possible, but is it practical?

I picture the abilities, effects and attacks being saved within the gameobject under a standard script ‘resolution’ where it will save all of the targets of the attacks and abilities. Then having all the gameobjects be added to a list and re-ordered based on their resolution sequence and finally executed. A lot of this seems a little similar to what a command design pattern is already trying to accomplish, but I wanted to make it more flexible I suppose.

I should also mention that I have coded card attributes in a modular fashion. That is to say that I have separate scripts for health, death, attack, enter play abilities (if the card has one) etc. Then I add all the scripts to the card and I edit their individual values through prefabs in the editor.

Let me know what you think!

I’d have a history card pile (as a list) and a resolving card pile (as a stack). the history pile is meant to simply document the order in which cards were played, and are not meant to be enacted on through the history pile.

when a player plays a card it goes into both the resolving pile and the history pile. if the opponent doesn’t challenge the top card in the resolve pile, then the pile resolves itself and pops off each card from the top of the stack (usually its just a single card) to perform it’s action

if the player does challenge the card (counter spell for example) the opposing player’s card is added to the top of the stack (and to the history pile), and then any player (if there are more than two players for example) can choose to challenge that top card (possibly starting a challenge war).

when the final card is no longer challenged the resolve pile will resolve itself and pop off each card from the top, performing their action. in the case of a counter spell, it’ll force the next card in the stack to be removed immediately (so that when resolve pile pops the next card, the card that was countered is already removed).

Card prefabs that can challenge the resolve pile should likely have a script implementing a IChallengable interface with a function bool CanChallenge(Card cardToChallenge) so that you can easily have different cards able to challenge different (or very specific) cards. so a CanChallengeSpells script will return true is the last card in the resolve pile is a card classified as a Spell card.