Turn-based 4X game with unity

Discussion in 'Unity Gossip' started by blaine, Apr 28, 2012.

  1. blaine

    blaine

    New Member

    Joined:
    Apr 25, 2012
    Messages:
    4
    How well suited is Unity to create one? Would it need unnecessarily amount of programming to achieve it or does Unity support that genre too?
  2. rab236

    rab236

    Member

    Joined:
    Jan 17, 2009
    Messages:
    2,307
    Unity supports any genre of game and is well-suited for any genre. If it requires a certain amount of coding, I wouldn't call that coding unnecessary.The amount of programming that goes into it would really depend on that you want the game to have. There is no magic button to make a turn-based game (or any kind of game, for that matter). The ability to make said game relies on your programming skills, patience, and willingness to work on the game.
  3. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69
    are there any links to tutorials or articles on making turn based games in Unity?
  4. JRavey

    JRavey

    New Member

    Joined:
    May 12, 2009
    Messages:
    2,377
    We just went over this.

    A turn based game is not significantly different to code than a real-time game (which still operates in discrete time increments). You only permit limited actions to happen which are dependent on the current state of the game. Instead of constantly polling input, just let it get to a point where the player, whether AI, human, local, remote, or a combination, is permitted to submit requests which are checked against the rules and if found to be compliant the action is accepted and the state is updated.
  5. blaine

    blaine

    New Member

    Joined:
    Apr 25, 2012
    Messages:
    4
    Thanks for the quick answers! Got all the info I needed.
  6. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69

    great, now can you link to specific examples of how this is done?
  7. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    There are none as this is nothing you can do in a few lines of code and examples normally cover exactly that.

    You might have some luck looking for game starter kits for partially related genres on the asset store though where the amount required to do anything like this is more justified. Just be warned though that it will pretty surely not work out as a base for a 4x game, only as a general 'showing the concept' of said genre, a learning experience.
  8. JRavey

    JRavey

    New Member

    Joined:
    May 12, 2009
    Messages:
    2,377
    Let's use Candyland as an example.

    On turn 0, we begin our game just as with any other game. All of the objects are in place, just hanging out. With two players in our game, we will basically take turns.

    Player1 will draw a card and examine the color and move his piece to the next location of that color. Since we are on a computer, when the card is drawn, the Referee object will move (or direct it) Player 1's piece to move to the appropriate square. When this action is completed, the Referee will change its active player value to Player 2.

    Player2 will draw a card and examine the color and move his piece to the next location of that color. Since we are on a computer, when the card is drawn, the Referee object will move (or direct it) Player 2's piece to move to the appropriate square. When this action is completed, the Referee will change its active player value to Player 1.


    A game could do this in various rules-based ways. A fancy game might have simultaneous declarations of intent and then simultaneous executions of actions, or it might be round-robin.

    Honestly, the best way to do this is pick a fairly simple game you know and love, such as Yahtzee, War, etc that doesn't even have a game board and code it.
  9. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69
    well Ive coded the better part of Poker, but coding a game with turn based rules and coding a game with discrete game phases and animations that occur with input turned off duruing them is much harder.

    You speak of a referee, but how would you setup classes for this?

    Is there a good unity chess or poker tutorial?
    How about Tic-Tac-Toe?
  10. JRavey

    JRavey

    New Member

    Joined:
    May 12, 2009
    Messages:
    2,377
    Poker is a turn-based game.

    Forget about the animations of the objects, why do they matter for a turn-based game? They're essentially on autopilot. If I have my little pieces just sit down when it's not their turn it has nothing to do with gameplay based events. Imagine if the referee calls an event of newplayerturn(playerID) and each player's character on the board subscribes to this event, if it's their turn they stand up, if it's not their turn they do nothing.

    Using Unity, since we are on the Unity forums, if you made your poker game, the players could still fidget and animate just the same. Then, when it's their turn they could play the relevant animations just as you would with a normal game. If I call the bit, you could have my avatar move his hands out and drop some chips on the table. Easy peasy. In a normal game, we don't expect animations and such to stop just because they're not actively receiving input, why would they in a turn-based game?
  11. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69
    I mean like cards moving across the table or flipping over. This kind of thing is generally state-based. How do you approach this? do you have an examples thereof?

    What about something more complex, a card game like Magic or a game like Final Fantasy Tactics?

    These are complicated projects. Just saying 'oh you do it like this' is not really an answer. The question being asked is 'how do you build a house?" you are saying 'with a hammer and nails, you just measure, cut and hammer them together. Its a complicated multi-layered thing. People go to school for years or work under a skilled master to know how to build houses (I worked with my dad for years as a carpenter's assistant).

    So, saying 'you make turn based games the same as you make any game, and you use these basic patterns' is a bit high level of an answer. A 4X game is even more complex, with numerous modes, screens and datasets that must be dealt with. Could you describe in detail how to build say 'pente'? or 'gomoku'? And how would we setup the project to change say 'missing spaces on the board from a set of patterns to make it interesting and maybe selecting a certain image for our pieces. Then say a results screen that had a win condition and some data from the last match. Add in a high-score table. Dont forget, the pieces need to slide or flip or such onto the board, how are the game states done so that the movement of the pieces onto the board occur outside of selecting whereto put them, turns should be near instantaneous, we need the illusion of tactile space. How about having possible places to put the tile light up? how are those selected?

    See its a bigger question than you are answering, I am curious how you would solve it, what patterns and such you might use.
  12. OmniverseProduct

    OmniverseProduct

    New Member

    Joined:
    Feb 26, 2012
    Messages:
    1,569
    Check this website out. http://www.codeproject.com/KB/game/ . None of the code there is unity based, or mono for that matter, but it can all easily transfer over to unity. They have a lot of articles and sourcecode downloads not just for C# but a wide variety of languages.

    On a side note, and don't take this the wrong way, but if you cannot figure out how to make a simple turn based game, you probably need to dial it back a notch and learn/relearn the basics.
  13. Tempest

    Tempest

    New Member

    Joined:
    Dec 10, 2008
    Messages:
    1,286
    To the OP, @blaine,

    Yes, Unity can handle 4X games. I'm working on a simple space/mining-themed one at the moment.


    @rqpaine -

    Your questions are very general, and range from simple to difficult to solve.

    To answer what appears to be the largest issue with a real-time vs. turn-based game is the issue of events.

    The player does something, and before they (or the other player) can do another thing, you need that 'something' to be completed. This could be an animation of a chess piece moving across the board.

    The simple answer is that a 'state' for the game is kept, and iterated through, keeping track of each phase of each turn.

    1. Player Move
    2. Piece Animation
    3. Check Win Condition
    4. Switch to other Player

    This can be done through the regular update loop, or through Coroutines. What matters is that at the end of a phase, it knows the next phase to go to.

    Things like highlighting possible moves, Computer-players, Leaderboards, etc, are all extra bells and whistles. These and many other features can be added, but aren't the core of the turn-based system. I think Magic: The Gathering and other card games do an excellent job at outlining phases of a system like this.

    1. Turn Start
    2. Upkeep (Untap)
    3. Draw
    4. Play Card
    5. Card Resolution (Win Condition check)
    7. Combat
    8. Combat Resolution (Win Condition check)
    9. Turn End (Change Active Player)

    Start with developing a basic system that goes through phases of a turn, and that will be an excellent start for you.



    @dreamora - Off topic, but I like the profile picture.
  14. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69
    you know I hear a lot of people saying its very easy and outlining its structure, but no one really explains it. I have written basic turn based games. I have gotten pretty far into complex card games. I have a degree in game programming and 3 or so classes in C#. I have written A* in unity on a tile based board.

    I still dont see examples or articles or tutorials other than 'its really easy' and 'you should learn the basics'.

    If its so easy, lay it out step by step. Unity is geared for immersive 3d worlds. It can be used for other things. At its heart, unity is a realtime 3d game engine. Making a turn based structure over that is doable, but it has nuances that are not easily pinned down. Where are the examples and codebase that show how its done. Not just high level statements, but real examples.

    Im not even asking anyone to do it, just link to relevant articles and tutorials. If someone wants to show it that would be great. Just Tic Tact Toe or Pente. Maybe an example for 'go fish' or rummy.
  15. George Foot

    George Foot

    New Member

    Joined:
    Feb 22, 2012
    Messages:
    399
    rqpaine, the trouble is I think your questions are nothing to do with "turn-based" games - they are general questions about pretty much anything stateful (and some things that are nothing to do with state), and this comes up a lot in all games. I think it is true that Unity doesn't help much with this kind of thing - even something logically simple like implementing a pause menu is actually quite hard, regardless of whether the game is turn-based or not. Unity doesn't help you abstract input - routing it in different ways in different places, or responding to events differently depending on state.

    Coroutines can help a lot, though, and your other major tool is creating and applying new components to represent different states. There are pros and cons all over the place, though, so while you could ask to see an example of this stuff in action, it wouldn't mean that's the best way to do it in all cases, or even in the case being solved - I think a lot of it comes down to taste.

    So my general approach to all this stuff is to have a scene manager object which orchestrates things. It's responsible (directly or not) for all the UI and input processing. Other objects in the scene may respond to mouse clicks and so on, but they do so by just interpreting the click (which tile was clicked?) and telling the scene manager, which then deals with the click depending on the current state. Similarly, when RTS units are clicked, they don't automatically do anything with the click - they just tell the scene manager "I was clicked". It then handles selection, attack orders, or whatever else the click might mean given the current state.

    For one project I used polymorphism to overload these input responses - the scene manager had a list of active handlers, which implement things like "world position click" and "unit click". I had a badly-designed UI layering scheme in there too, so these handlers could grab control of the whole UI, or just render status information in the sidebar, or action buttons, that kind of thing.

    For really strict state machines, though, where there's no state overlap, I think coroutines must be the best choice. You can write clear code, using loops and so on where you want to, while (!SomebodyHasWon()), that kind of thing. In effect it's like nested Windows message loops - you can pop up a dialog on top of everything else, and know that the calling code won't continue until the dialog completes. The yield syntax writes the state machine for you. This is not Unity-specific, I've also found it to be a natural way to write robust flow code in XNA.
  16. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    You are aware of the Model-View-Controller design pattern and the concept of the Finite State Machine, then? Good. Because those are the basis of a turn-based game...

    A turn-based game is, like most tools and utility applications (e.g. a word processor or spreadsheet) simply a game that uses the MVC design pattern. In a word processor, the computer's "turn" involves running the inline spellchecker and painting in those red squiggly lines under unknown words, which the View then displays. The user's "turn" is far more complex, making this a very one-sided "game", but the principles are transferrable.

    You decouple the game's model from the rendering process, leaving only GUI interaction in place, so players can always access the main menu and the like. In Unity, the Model will usually comprise of some C# classes and scripts that live in a separate GameObject attached to the Scene. (Often with a name like "GameManager" or "LevelManager". Or both, depending on your particular game.)

    Animations are often standalone entities consisting of one or more Finite State Machines attached to the GameObject or Prefab and instantiated or triggered through the Model or Controller scripts.

    The virtual camera contains the View components (as you'd expect).

    The Controller is, like the Model, a bunch of scripts that live in a separate GameObject (although many real-time games just add the scripts to the camera GameObject, along with the View scripts, as the View and Controller elements are often very closely linked in real-time genres.)

    The Controller handles user input. When a user is waiting for another player to take his turn, the Controller simply prevents all interactions with the game's "pieces" until it's the user's turn again, while retaining access to the UI—e.g. panning the camera around the game board, or allowing access to settings menus.

    The Model is the game logic. Here be Finite State Machines. Often quite a lot of them. (PlayMaker is a good third-party extension for building these as it's very focussed on building FSMs. Other, more general, visual scripting tools are also available.)

    This is because it is literally impossible to provide articles and tutorials on every single possible use case for Unity. It's also used for architectural walk-throughs and even movie pre-viz work. It's not limited solely to games.


    You already have a degree in game programming. What the hell more do you want? This is very basic stuff.

    I'd recommend the 3D Platformer Tutorial (which I wrote). It won't teach you turn-based games specifically, but you will have a good idea how to architect a turn-based game by the time you've finished it as it does cover all the basics of scripting in Unity.


    If enough people want one, I can write a simple Nought Crosses ("Tic-Tac-Toe") tutorial tonight. I'm pretty bored.
  17. Bluntweapon

    Bluntweapon

    Member

    Joined:
    Feb 24, 2012
    Messages:
    156
    Well do it anyway, I'm sure at some point someone else is going to be asking the same questions. Better yet upload it to the Unify wiki or something.
  18. Tempest

    Tempest

    New Member

    Joined:
    Dec 10, 2008
    Messages:
    1,286
    I had a memory/matching game that was turn-based available for a couple years. I'll re-host it soon. I also have partially done game that's turn based (the logic/board mechanic is complete, it's just not fun).

    I would recommend you try to implement a basic state machine script. Don't worry about game logic. It doesn't matter how complex the board/card game is, just work on a script that remembers the 'state' of the game, who's turn it is and what phase in that turn it is. That will be a great start.
  19. blaine

    blaine

    New Member

    Joined:
    Apr 25, 2012
    Messages:
    4
    I usually like to try and figure things out myself first, but I'm sure I would learn a lot from any kind of tutorial you guys would make. So, thanks in advance for doing it (Stimarco)!
  20. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69
    this is what I was looking for. this answer will help countless people in the future.
    could you recommend a design pattern besides MVC that might work? especially one that works intuitively with component based architecture? or how you would setup MVC within unity's component based architecture?

    you said that a couple of times. I might put one together myself. Now the question is where do you put it? In Start? in Update? On each game object or the scene manager (I use a 'Dealer' GO) etc etc?
  21. Tempest

    Tempest

    New Member

    Joined:
    Dec 10, 2008
    Messages:
    1,286
    Singleton Pattern. There should only be one script which determines the state of the game.

    Awake/Start is used for initialization, but the checking/advancing/win condition of the state would be done in the Update.
  22. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    'Tis done.

    I have some spare website space, but I've never used the Unify wiki much, so I've no idea how to upload there.

    I'll look into it on the morrow. (I may add some minor UI tweaks too as it's a bit lacking in places. Also: it has programmer art and no AI, so (a) get a sick bucket, and (b) please don't complain about either. It's strictly a two-players-only demo project. Written from scratch over a single evening.

    I do have some errands to run, but I'll be back by the afternoon and will upload then.
    Last edited: Apr 30, 2012
  23. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    So much for getting the errands over early. Still, better late than never; here it is. This release is only intended as a WebPlayer build. I've not tested it thoroughly on other targets yet, but I imagine the GUI elements will look rather lonely on setups with huge, widescreen displays at high resolutions.

    The link to the project's ZIP file is just below the WebPlayer itself.

    [UPDATE: I've just uploaded a slightly smaller ZIP file, but it's still 9.9MB. Do feel free to share this amongst yourselves.]

    This is NOT the final release. It can be polished up a bit more, and some new features could be added quite easily, but that's something I'll leave until later, when I have more time and a spare artist lying around the place.

    A quick overview of the structure:

    This game applies the "MVC" design pattern:

    - The "Model" part is in the "GameModel" GameObject.
    - The "View" script is nailed to the main camera.
    - The "Controller" is attached to the "GameController" GameObject.

    Note that neither the GameModel nor the GameController GameObjects have any renderable components. The game board is generated procedurally by instantiating the "BoardCell" Prefab. The background is just a big texture slapped onto a simple "Plane" GameObject.

    The GUI is built using the Unity 3.x Immediate GUI system. (The default GUISkin is used.)

    I've tested the "Model" code with different board sizes (of equal width and height) and the logic is generic enough to work with any arbitrary setting of the "boardSize" variable. However, the camera needs to be moved—or the Prefab modified—to allow different board sizes to fit into the viewport. As this is beyond the scope of the demo and would only serve to complicate the code further, I've omitted this for now, but will probably add it in a later revision. I'd also like to add an AI player or two as well.

    I'll do a write-up on it later, when I have more time. In the meantime, the code is heavily commented throughout in accordance with my usual coding style.

    *

    Some additional notes:

    - The code is not particularly optimised.

    In particular, the tests for a victory condition are currently run inside an Update() function, but would be better off in an external function that gets called only after the player has actually clicked on a tile. I'll sort this out later. In the meantime, do let me know if there are any performance issues.

    - I wrote this from scratch over a single evening, and it looks it. Don't expect awesome introductory cut-scenes, or big-name voiceover actors. It doesn't even put up a big box telling the next player to prepare to play, although there is a small text label under the game's title that will tell you who's turn it is to play.

    - All graphics either by me, or from CGTextures.

    - I have only tested the game on my late-2010 MacBook Pro running OS X "Lion". It's a pretty decent spec laptop, so I have no idea if the game will perform well on, say, an Intel GMA950-series machine. All reports appreciated.

    - That heavily commented code is how my code normally looks. I have a truly lousy short-term memory and I find writing things down helps with retention. You have been warned.

    *

    Finally, this was built using Unity 3.5.1 Pro, but the project makes no use of any of the usual standard or Pro asset bundles and should therefore work on Unity Free. Please do let me know if it doesn't.
    Last edited: Apr 30, 2012
  24. Tobias J.

    Tobias J.

    New Member

    Joined:
    Feb 21, 2012
    Messages:
    423
    Sweet! Thanks Stimarco :D

    I'm very happy to see I've done almost exactly the same in my implementation. Your 'yielding' and checking of states in your Gui-Script is pretty nifty I think. I'll have to read it again in the morning ;-)

    Your comments really are excellent. If only all code had a quarter as much, we'd all be coding soon. You get away easily here, though, if you're aiming for a real tutorial:

    Code (csharp):
    1. // Player 1 always plays "X". Changing this is left as an exercise for the reader.
    Without some more guidance, this might be a tall order. Maybe add a few hints, like 'make a new class', or 'requires another button'.

    Anyways, great reading almost as much as deciphering for once :D
  25. npsf3000

    npsf3000

    Member

    Joined:
    Sep 19, 2010
    Messages:
    3,722
    Is it just me or are you basically insulting yourself?

    For me at least, a programmer is somebody that at a basic level 'figures things out'... and yet you seem to be spending all your time not figuring things out? I'm in the process of releasing a bunch of applications right now. They are event-based and in Unity3d. They involve a bunch of different features I've never done before, under a tight deadline and some serious feature creep.

    So I sat down, figured out what the actual requirements were, figured out what I knew, what I didn't know and what I needed to know and got them out the door.

    The advice/info I had is the same advice info I'd give anyone. Know what the requirements are. Learn and know useful paradigms to help maintain the projects maintainability. Know the API's [Scripting reference, MSDN] and available resources [e.g. asset-store, Google] to avoid reinventing the wheel. Get it done.

    Making turn based games in Unity is easy - and as a programmer the lack of any 'example projects' shouldn't daunt you in the slightest. Sure, it'll take you a tad longer to figure out what works well and what doesn't - but that's the job.
  26. OmniverseProduct

    OmniverseProduct

    New Member

    Joined:
    Feb 26, 2012
    Messages:
    1,569
    not to mention the code project website I linked which he seemed to ignore.
  27. Tobias J.

    Tobias J.

    New Member

    Joined:
    Feb 21, 2012
    Messages:
    423
    jeez. All he asked for was a link. :rolleyes:

    Now, thankfully, there is.
  28. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69
    @ Omniverse
    Im familiar with the code project. Its a great site.

    @ NPSF
    you are really full of yourself. great for you that you can do so much. I however came to a discussion forum to discuss problems, practices and relevant topics, not just talk down to people about how great I am. For the record,I have never taken a class that covered design patterns. I learned about them in a chat room and from searching the web after reading other peoples posted code. It simply wasnt taught at my school. I can however write a webpage, a database, create a shopping cart, and inventory system or a store in the real world, make a chat room, write a forum from scratch, create anything in 3d out of a box, rig, texture and animate it, code a basic 3d engine, write AI that will guide missile, flocking and a half dozen other things. I dont actually know much about design patterns, I do know about 6 languages at a decent level. now instead of wandering these forums casting down your elitist sense of self worth, which I have seen you do in a half dozen threads, you might actually share some wisdom. You gave nothing relevant to this thread, instead you attempted to bolster your own ego at another's, my, expense. This is a discussion forum, where people talk about things they dont understand or are looking for different ways of doing. Now while I am familiar with MVC, I find it to be a real pain to work with. Its confusing, kludgy and overly compartmentalized. Im intersted in turn based games. What wasn't covered by stimarco, though he got most everything, was implementing a computer player. One of the aspects of a turn based game that got me the most, is creating an AI and sequencer to play out actions in logical fashion that doesnt occur automatically without using a bunch of timers or counters. After looking into things here, seeing what people have done that works and what other recommend, Ill look into coroutines and yield. That seems to be in fashion and not terribly hard to do. I will not however be paying much attention to you. I find your statements to be fruitless and demeaning. I wasn't trying to insult myself, but you certainly did. good luck in your endeavors, I hope you make a million bucks.
    Last edited: May 1, 2012
  29. Arowx

    Arowx

    Member

    Joined:
    Nov 12, 2009
    Messages:
    2,326
    OK a quick guide, not actual code but the simplest approach to a turn based mechanic in Unity I can think of (think draughts).

    You should be able to work out or find the code for each step here or on Unity Answers.

    Step 1: Build a grid of cubes for the board (setup an array to hold them)

    Step 2: Use Raycasts to work out which square your mouse is pointing at.

    Step 3: Add in a Sphere as a piece and get it to move to the new location.

    Step 4: Add another Sphere as the other players piece

    Step 5: Setup logic to only allow one player to move at a time and some gui text to show who's turn it is next.

    Step 6: Get iTween to add nice smoother moves.

    Step 7: Then change the spheres to 3D models instead of spheres.

    Ta Da a turn based game in Unity*

    * But you also need to represent the game world so rules can be applied, a simple array as the board and values as the pieces would be the quick way, but probably better to create a Unit class that can have stats, armour, moves, moves left ect.

    And as already mentioned the logic will get more complex for what happens next so best to use good OOP programming and divide the problem out into smaller chunks. e.g

    Unit
    Board
    Player
    Game
    ect.

    Hope that helps b4n
  30. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    For those who missed my (rather long) post: I wrote a Tic-Tac-Toe demo project on Sunday evening and posted a link to it yesterday (Monday) evening, along with a WebPlayer version so you can see what you're getting.

    Here's the linky.


    @Tobias J: The OP was asking for links to existing tutorials.

    The link I posted is to a brand new tutorial written especially as a result of this thread. I decided to write it after trawling about online and through the Asset Store and realising that there really isn't much out there showing how to build turn-based games in Unity. Gap in market spotted. Gap filled. :)

    I'm considering polishing it up and posting it on the Asset Store as it would relieve me of the need to host a 9MB+ ZIP file + Unity WebPlayer project myself; Unity has over a million registered developers now, so that's a lot of potential bandwidth I may lose.


    @rqpaine: your point about the lack of an AI player is a good one. I do plan to implement it in the final version of the tutorial, but I won't have time to do it before the weekend.

    Also, thanks for spotting that erroneous comment. (It was originally correct, but adding a second button wasn't difficult.) I've fixed it now and I'll re-upload a new ZIP of the project as the comments are all the tutorial there is at present.


    Also, on a completely tangential note: if anyone else reading this has ever written a Tic-Tac-Toe game, did you find yourself having to fight the urge to use "Y" instead of "O"? Damn you, Cartesian coordinates!
    Last edited: May 1, 2012
  31. npsf3000

    npsf3000

    Member

    Joined:
    Sep 19, 2010
    Messages:
    3,722
    Yes, yes I am. Yes, yes it is so great. Yes, yes I hope so too.

    What I gave here was the basic 'truth' behind programming - at least as I know it. It's up to you to figure it out. And from the looks from it, you've actually been doing that.

    The problem I saw is that this is you've now made ~7 posts where you've either claimed turn based games are hard, or asked for detailed tutorials. Programming isn't about having the answer handed to you, but being able to figure things out for yourself - and this is the complete opposite of what you've portrayed. This is the 'wisdom' you asked me to give, and I have - just explicitly pointing it out here.

    Note to all: There is nothing wrong in wanting tutorials - in fact it's a vital part of programming. The ability to utilize existing resources is critical to success. However, it's never a prerequisite, and it's something that you should be able to find by yourself - not repeatedly ask for in a thread. Lastly, it's only a valuable assistance - you should be able to painfully and slowly DIY regardless.

    Now, just to shut you up, I'll spend 5 mins outlining a basic turn-based game with AI.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. class Checkers : MonoBehaviour
    6. {
    7.     void Start()
    8.     {
    9.         StartCoroutine(RunGame());
    10.     }
    11.  
    12.     IEnumerator RunGame()
    13.     {
    14.         yield return StartCoroutine(SetupBoard());
    15.  
    16.         while (true)
    17.         {
    18.             yield return StartCoroutine(PlayerTurn());
    19.             if (WinCondition()) break;
    20.             yield return StartCoroutine(AITurn());
    21.             if (WinCondition()) break;
    22.         }
    23.  
    24.       StartCoroutine(WinScreen());
    25.     }
    26.     IEnumerator SetupBoard()
    27.     {
    28.         //Animate peices returning to positions.
    29.         yield return null;
    30.     }
    31.  
    32.     IEnumerator WinScreen()
    33.     {
    34.         //Display win screen [same screen for both AI and player win for now]
    35.        
    36.         yield return null;
    37.     }
    38.  
    39.     IEnumerator PlayerTurn()
    40.     {
    41.         //Change GUI to show Player turn.
    42.  
    43.         //While input not valid move
    44.         ////Wait for Input
    45.         //Make move
    46.           yield return null;
    47.     }
    48.  
    49.     IEnumerator AITurn()
    50.     {
    51.         //Change GUI to show AI turn.
    52.  
    53.         //for each piece
    54.         CalcPossibleAttack(/*piece*/);  //and store the results...
    55.        
    56.         //Make a move - pick the highest damage piece.  If there are multple pieces, pick one randomly.
    57.  
    58.         yield return null;
    59.     }
    60.  
    61.     int[,] CalcPossibleAttack(/*Piece p*/)
    62.     {
    63.         //find all possible movements.
    64.         //if movement is possible and allows futher movement
    65.  
    66.         //recursively call this.. assuming the move had been made.
    67.  
    68.         //if there are multiple paths a piece can make, pick the first in the following order highest damage -> shortest jumps -> random number.
    69.  
    70.         //return how many jumps this piece can make as int[0]
    71.         //return how much damage this piece can do as int[1];
    72.         //in the real implementation these will probably be structs or somethign instead of a two values array.
    73.         //This may be put inside a piece or board classes that I'd probably generate.        
    74.         return new[,] { {0}, {0} };
    75.     }
    76.     bool WinCondition()
    77.     {
    78.         //return true if the game has been won
    79.         return false;
    80.     }
    81. }
    I hope your happy with this. Of course, the fact that you couldn't do this - whether it be using co-routines, events, loops with counters, locking etc. by yourself - let alone the inability to use google to find existing tutorials probably means that I did you no great service. The problem here was never that we couldn't help you... but that with your considerable programming experience you couldn't help yourself - even after you were given suggestion along the right direction!
  32. JRavey

    JRavey

    New Member

    Joined:
    May 12, 2009
    Messages:
    2,377
    This makes me wonder how people could possibly program without examples showing them how to do what they wish to do. How did programs get written before tutorials? Is such a thing possible? This is a question probably better left for cosmologists.
  33. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    I learned to program back in the days when Google came in printed form, with the word "Encyclopaedia" stamped on the side. My parents had a lovely set of encyclopaedias dating back to the 1950s. The term "Computer" was defined as "A man who computes."

    The only book on AI that I ever owned was a thick tome focusing primarily on Chess. Lots of talk about heuristics and openings. Not a bloody sausage about A*. Bastards! I ended up reinventing A* from scratch without even realising what I'd done until much later. (Considering I'd only applied to the publisher to be a graphics artist and had written a map editor to show off the tiled graphics I'd made, getting hired by said publisher to write a full-on turn-based strategy game for the Atari ST and Amiga instead was a bit weird in hindsight.)

    Then again, in my discussions with my peers, my opinion is that programming is taught very badly in most institutions. The old, crusty, bearded professors who lecture in universities tend to be very parochial and conservative. They are often fanatical about their preferred development environments, even to the extent of insisting all their students use the same tools the lecturer learned to use 20 years earlier. (Hence—and I know this won't win me many friends—the Cult of Unix. But that's another rant.)

    But (burp!) I digest...

    Programming is still being taught today as if it were a branch of mathematics and thus subordinate to that field. I disagree with this approach. In my experiences teaching IT about 10 years ago, I found I got much better results when I simply bit the bullet and explained how computers 'think'. Incidentally, this might also explain why there's such a gender division in this field: many of my students back then were female, not male. However, even the male students in my classes found my approach easier than the other teacher, who was sticking much more closely to the official curriculum.

    (Some context: I was teaching at a school that catered almost exclusively to foreign students—mostly from Zimbabwe as many had fled that country when Mugabe lost the plot around 2000. Some of these students had come from such tiny rural villages that they weren't even familiar with concepts like hot and cold running water, or electricity.)

    Basically, within a few lessons, all my students understood how computers 'thought'. I even taught them about binary through stealth, without ever mentioning number bases or powers of two.

    To me, programming is just a form of translation. Maths doesn't even make a cameo appearance: mathematics is itself a (man-made) language, just like C++ and Boo, so the fact that you can express programming in mathematical terms is an utter irrelevance; you can also express music in mathematical terms, but nobody teaches music students to perform an Orchestral Analysis before sitting down and architecting the Composition Specification, which then gets passed down to the Notators for implementation in the final score.

    How the hell this myth has persisted for so dammed long escapes me, but the fundamental fact is this: if you can understand the concept of "cause --> effect", you can program. If you can learn another "natural" language, like Italian or Swedish, you can program. It really is just translation. C# is just another language, like Dutch or Swahili. Treat it that way, learn how computers actually think, and programming is a piece of cake.

    I have very little skill in maths or physics, but I do know where I can look stuff up when I need to. And that was enough to keep me in gainful employment as a professional programmer for well over a decade.

    I used to rail against people like rqpaine who claim to have years of formal education, but, after many years of working in the education sector, I am no longer surprised by it. Some minor rearranging of the deck-chairs aside, Western education institutions and techniques have barely changed since the 1800s, and they have not aged well.
  34. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69
    I have a degree in game dev, but you have to understand that covers everything, modeling, programming, ai, texturing, animation including cloth and liquid sims etc. With around 100 credit hour at 4.0 Im good at what I do and know a lot, but I am still far from being an expert. Its also spread over 6 languages and two focuses, one being games, the other web design. I also have about half the classes I need for a business degree. I have never taken trig or calculus. I have never taken a class on data structures and algorithms. I still have a lot I want to learn.

    I am at heart a designer, with a lot of focus on writing and management.
  35. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    From your description, that sounds more like a glorified "Media Studies" degree that focuses on multimedia rather than traditional (i.e. linear) media. It might have been an attempt to teach game design skills, as knowing how all the various crafts work and are blended together is a major part of that. Even so, teaching six programming languages in three years is just stupid: Good programming skills are language-agnostic; you shouldn't have to be exposed to six damned languages in three years. Not even degree-level natural language courses do that.

    I wouldn't worry about trig or calculous: I can't even do long division or basic mental arithmetic in my head* and it's never caused me any problems with my programming. Similarly, data structures and algorithms are also things you can look up. Remember the concepts and the odd bit of terminology so you know what to look for, but don't try to remember every last bit of it, because only someone with an eidetic memory could perform that kind of feat.

    Programming, as with any linguistic skill, is mostly a matter of practice, not memorising dozens of algorithms by rote. Algorithms are like recipes: you can look them all up in a book.


    * (My short term memory really doesn't like numbers. I have to constantly repeat them over and over in my mind like a mantra just to keep the damned digits fixed in the right order and keep them fading away. It's not dyscalculia as I can read numbers just fine; I just can't remember them.)
  36. rqpaine

    rqpaine

    New Member

    Joined:
    Oct 6, 2011
    Messages:
    69
    six languages in six years, took HTML/CSS and was headed for a webdesign degree, dreamweaver, flash (ver 8), intro to graphics(PS, InDesign, Illustrator, etc) some gen ed, at 1 year in, year and a half, the opened the game design degree so I switched over.
    Took Java as an Intro class, then C#. After that was 2D programming in Allegro, where I learned a lot, then 3D game dev, 3D modeling, 3D animation, AI, flash game dev, I also took PHP/SQL and Python. I learned AS3 on my own.

    I havent used Unity in just over a year now, maybe two. Im about ready to get back into the swing of things.
  37. DigitalDuane

    DigitalDuane

    New Member

    Joined:
    Dec 13, 2006
    Messages:
    39
    I agree with you 10,000%, this is Unity's M.O for years! New features released with no tutorials....but it's really easy! Osmosis I guess!
  38. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    In fairness, teaching is a skill, just like programming or graphic design. You can't expect a company formed in Denmark and with a staff that has few native English speakers among its development crew—only the marketing and support people are based in English-speaking countries—to be able to crank out great (English) tutorials as well. They have to hire writers in for that.

    I'm one of the writers they've hired over the years. This tutorial was written by me to serve as both an introduction to Unity, and to illustrate the basics. Unity has moved on quite a bit since then—it was written for the Unity v2.0 release—but it still holds up well, even if mine is hardly an unbiased opinion!

    The fact remains, however, that UT don't have a "Unity University" or similar formal training department in place, so they're very reliant on third-party tutorial writers. I've suggested setting up such a department as it would also help lift their profile in the education sector. (Teachers will happily murder their own siblings for free course materials. The less grunt work they have to do, the more energy they can focus on actually teaching their students. As Unity changes quite a bit over time, that means courseware often has to be done over every year or so, which most teachers hate.)

    Will Goldstone is currently UT's "go-to" writer, if memory serves. He's also written a book on Unity, so that may be a good starting point for many newcomers, but there are also a ton of video tutorials on Vimeo and YouTube that are very good too.

    I hate the sound of my own voice, so I don't do video tutorials, but writing that Tic-Tac-Toe tutorial was surprisingly fun, so I'm happy to create small illustrative projects to demonstrate a particular concept or technique and intend to do some more. In the interests of avoiding massive bandwidth fees, I'll most likely put up a series of these on the Asset Store for a couple of quid each.

    I believe the best tool for teaching people about Unity is Unity.
    Last edited: May 4, 2012
  39. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    Tutorial Update News:

    I've finally had some time to go back over the tutorial and have given it a good kicking. The GUI and layout have been changed so all the buttons are on the left instead of the right. This makes it much easier to cope with different hardware and display sizes. The camera is now set to Orthographic mode instead of normal 3D, so it feels more like a 2D game, and the victory graphics are now much simpler as the purpose of the tutorial is to show how to create a turn-based game, not demonstrate Unity's Animation Curve Editor.


    I've made some architectural tweaks—it's MVP, not MVC now, although the difference is subtle—and, yes, I am going to add an AI player or two to it next. I'll then give the code a bit of a clean-up, check the comments are still accurate, then write up the tutorial document itself before posting it all onto the Asset Store sometime around the end of next week.

    I've enjoyed making this so I'm going to accept requests for new tutorials once it's posted. I'll announce it in a new thread here.

    *

    Device Compatibility:

    The project has been built and tested for the new iPad. I plan on testing on other iOS devices, including the iPhone 4, the original iPad, and a 3rd-gen (pre-Retina display) iPod Touch. If it runs on all of those, it should run on Android devices with similar specs too.
  40. rgbgames

    rgbgames

    New Member

    Joined:
    May 9, 2012
    Messages:
    26
  41. AstaSyneri

    AstaSyneri

    New Member

    Joined:
    May 12, 2012
    Messages:
    25
    Stimarco - mille grazie for sacrificing your time to build a tutorial like that. I am in a comparable bind as rqpaine - I am in the process of learning Unity with a specific (turn-based, of course) game in mind. Not coming from a formal background (I learned Basic and Pascal about 25 years back, even wrote my own game back then), I am having difficulties to wrap my head around these concepts (and all those functions that C# already provides and that you basically have to know how they work).

    Tutorials that explain non-real-time games and how to implement it in Unity with C# are really rare (most examples I found - in books or online - use JavaScript and I'd like to stick with one language and C# appears to be the more powerful one).

    So - thanks again - and I am looking forward to the next part, where you show us how to program an AI ;-).

    BTW: If your e-book on non-real-time Unity games was available on Kindle for under $20 / 18 € I'd absolutely buy it ;-).
    Last edited: May 29, 2012
  42. Tanel

    Tanel

    Member

    Joined:
    Aug 31, 2011
    Messages:
    308
    Endless Space is one 4X game made with Unity. It's in alpha now, sold on Steam. Just thought I'd mention it.
  43. kalamona

    kalamona

    Member

    Joined:
    Aug 16, 2011
    Messages:
    651
    Fixed it for ya :)
    (i hope this is it...)
  44. Tanel

    Tanel

    Member

    Joined:
    Aug 31, 2011
    Messages:
    308
    Yeah, that's the one. I figured anyone who's interested can google it themselves :p.
  45. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    It's coming along. I have a 'dumb' AI—it just picks an empty cell at random—and am working on a 'perfect' AI which should, in theory, always either win or draw against a human player. And I'm going to add a third player that sits between the two and supports different difficulty levels.

    I've not had as much free time as I'd hoped this past week, but I should have the AI stuff done for this coming weekend. After that, it's just writing up the tutorial document itself.

    I intend to put the whole lot onto the Unity Asset Store, with the tutorials supplied in PDF, ePUB 2 and Apple's iBook formats. (Apple's "iBooks Author" application can export to both ePUB 2 and PDF as well as its native "iBook" format, so I get those formats effectively for free.) The eBook will be included with the project files, for roughly $5.

    And then I'll start on another tutorial.

    A Kindle version means I can't use animations, and my diagrams and illustrations would have to be designed to look good on both colour and monochrome displays. Not a big issue for this project, but it could be an issue for later tutorials.

    I'm curious to know how many forum readers here have an ebook reader and how many prefer PDFs and printed paper. Perhaps it's worth having a poll.
    Last edited: May 30, 2012
  46. AstaSyneri

    AstaSyneri

    New Member

    Joined:
    May 12, 2012
    Messages:
    25
    That's fine - I forgot about the Asset Store as a platform for good tutorials. I just recently fell in love with me new Kindle and like the idea of having a "book" on the side while my screen estate is all reserved for Unity and MonoDevelop. But switching applications isn't that big of a hassle ;-).

    LASTLY: Last night another thought occurred to me: There are quite a few requests about turn-based games in this forum, but they are scattered all over. Wouldn't it be better to have a dedicated sub-forum where all these questions could be bundled? It seems like there is a significant subset of Unity users that targets these types of games rather than 3D real-time ones... What do you think?
  47. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    Can't you side-load Kindle-format books onto the Kindle? I plan on providing multiple formats, not just the one. (At the very least—as I have an iPad 3—I intend to make ePUB 2 and PDF versions, as well as the experimental iBook one. Calibre can convert and transfer books across multiple devices, including the might Kindle, and it's free, so you can still use your Kindle to read. Judging by my poll's results so far, it looks like PDF and print are in the lead, though that's only after 35 votes, so I'm not going to bother with animated or video features except for the iBook version. And I'm only producing one of those to teach myself the iBooks Author application.

    I only see three such threads at the moment, including this one, but that's arguably three too many. There's also the incessant "Can I make [GENRE] games in Unity?" threads. I'm not sure there's enough of a distinction between real-time and turn-based 3D games for a sub-thread to be worthwhile just for those, but they do fall under that "How do I make [GENRE] games in Unity?" category.

    I don't see an obvious alternative on the main Forum page for posting such threads, so "Gossip" has clearly become the "wildcard" forum for any questions that don't fit obviously into the others. Perhaps a specific forum discussing using the use of Unity for specific genres—"Genre Support in Unity" perhaps?—is needed. It does seem to be a very common query category.
  48. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    Okay, this took me a little longer than expected (again), but it's done, the docs are written up and I've just finished submitting it to the Asset Store. Assuming all goes well, it'll be up in about a week. If they bounce it back pending some changes, I'll post an update here.

    Regarding the docs: it's going to be PDF only this time around. Forty pages of it.

    I had major issues with ePUB 2 conversion: I can't get the sidebars / boxouts to work at all—this sort of thing seems to be unsupported in ePUB 2—and it screws up the TOC too for some bizarre reason. And that's after trying both the Apple Pages ePUB export option and the latest version of Calibre. If there's a big demand for alternative file formats, I'm going to have to throw some money at Adobe.

    The PDF doesn't take the traditional "step-by-step" approach. I've written so many of those I couldn't face the prospect of writing another, so I've written it more in the style of a "behind the scenes" documentary about the making of the project. Everything is covered, and then some.

    Once the tutorial is live, I'll open a thread in the Assets Asset Store forums.
  49. OmniverseProduct

    OmniverseProduct

    New Member

    Joined:
    Feb 26, 2012
    Messages:
    1,569
    I look forward to it as I like these type of games.
  50. stimarco

    stimarco

    Member

    Joined:
    Oct 17, 2007
    Messages:
    704
    Did I mention it includes 100% Pure Programmer Art™?

    And, yes, all the 3D assets I created for it are provided entirely royalty-free. All one of them. :)