- Home /
How would I make a turn based system with simultaneous turns? (C#)
I've found a few good tutorials for driving a round-robin turn system with events and states in C#, but I'm having trouble applying that to what I want to do.
What I want for my game is a system with a "Planning Phase" where each player (and each AI controlled character) all describe simultaneously what they want to do, and then an "Execution Phase" where the planned actions occur, and the game engine resolves conflicts.
The best way I can think of is for there to be some data structure that each player's planned actions are submitted to, and every time someone submits it checks if they were the last one and ends the planning phase if that's the case. The execution phase is pretty trivial by comparison because it doesn't involve player action.
However, I have absolutely no idea how to even start to rig that up, and I'm probably wrong about it too. Any help at all would be very appreciated.
Answer by TonyLi · May 05, 2013 at 10:33 PM
This is actually complicated stuff, so take your time researching and designing. (Also, FYI, since this isn't specifically Unity-related, the mods may close this question.)
The execution phase isn't necessarily so trivial, because it has to handle conflicts. Let's say player A moves out of location (0,0) in the same turn that player B attacks A in (0,0). Does player A evade the attack, since he moved away? Or does player B stop A by attacking? Two typical solutions are:
Run a time-based simulation. If player A can move in 1 second, but it takes player B 2 seconds to start an attack, then player A evades.
Resolve conflicts with some kind of hierarchical rule-based system that sets rules such as "attacks always take precedence over movement".
Getting your conflict resolution down right is critical to the playability of your game, since they're going to happen all the time. After all, tactics are only interesting when they involve more than one party.
The planning phase is easy for human players, since they do all the work; you just need to provide a pretty interface for them to input their commands.
AI planning is the subject of limitless pages of AI research. I suggest starting at http://aigamedev.com/ - they have lots of really good articles available for free.
No, please do NOT close this question. All the parts I'm wondering about are unity-related.
You're posing interesting questions, but they're questions I've solved. I have pages of design documents for this project, software design is my day job. But I need to actually start getting some code down, and that's what I suck at.
So, with respect, if you can answer the question that I asked regarding how to code this for Unity, that would be really helpful, but I dont need help on the design aspects of the game.
No worries -- I'm not a moderator, so I can't close anything. With so few design details it sounded more like a data structures and algorithms question. Can you provide more details?
Otherwise, generally speaking, you might want to use a singleton object for your turn manager (e.g., an game object named Turn$$anonymous$$anager). An example implementation is: http://wiki.unity3d.com/index.php/Singleton. If you want the singleton to persist between level changes, use DontDestroyOnLoad().
Each player component could register itself with the singleton by calling a static method on the singleton (e.g., Turn$$anonymous$$anager.RegisterPlayer()). The singleton would maintain a list of players (e.g., System.Collections.Generic.List).
The singleton's Update() method or a coroutine (http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html) could loop through all registered players and check a "action submitted" boolean flag. If they're all true, it runs the actions and clear the flags. Depending on your design and needs, you might need to do some locking. An easy approach would be to simply prevent submission of new actions if the player's "action submitted" bool is already true.
You'll probably want to make the Turn$$anonymous$$anager singleton into a prefab (http://docs.unity3d.com/Documentation/$$anonymous$$anual/Prefabs.html). Likewise for the player objects, which you can instantiate at runtime (http://docs.unity3d.com/Documentation/$$anonymous$$anual/InstantiatingPrefabs.html).
$$anonymous$$eep in $$anonymous$$d that, depending on your design, not everything needs to be a represented as a game object in the scene. The turn manager singleton is a good example; it's more an abstract entity, not a visible object. However, you might find it convenient to make it an empty game object with a Turn$$anonymous$$anager.cs component so you can view it in the Inspector. This is really helpful at runtime. I do this with my AI components so I can see what they're thinking in the Inspector. If you do this, you can make a custom inspector to show whatever information you want (http://docs.unity3d.com/Documentation/Components/gui-ExtendingEditor.html).
As far as Unity-specific implementation, the link below is for a fantastic article. It provides coding tips for manager singletons, organizing components, and much more. I didn't write it, but I can't recommend it enough.
http://devmag.org.za/2012/07/12/50-tips-for-working-with-unity-best-practices/
Your answer
![](https://koobas.hobune.stream/wayback/20220613101602im_/https://answers.unity.com/themes/thub/images/avi.jpg)