Search Unity

Best way to control game flow in Unity

Discussion in 'Getting Started' started by knobby67, May 31, 2016.

  1. knobby67

    knobby67

    Joined:
    Aug 30, 2015
    Posts:
    389
    Hi All,

    I'm new to Unity and C#, but old to c++ and games. So I'm trying to get my head around unity concepts and hope people can get me on the right track. From what I can understand Unity is event driven ( bit like QT some windows systems etc ). The way I currently program games is I use a finite state to control where I'm at. Basically I have a two dimensional array of states ( functions ). I then use a pointer to control the state. Each state returns where it needs to go to next depending on events. So at a ( very )basic level,


    load graphics and sounds to demo state

    |

    run demo if no start rerun demo, if start game state

    |

    game state if player alive game state, if dead demo state


    There's much more states than this of course but I hope this helps show what I do. In unity should I think about a totally new way to implement game flow? Can I use state machines? If so how do I get around the issue of Unity scripts, do I turn of events eg say something like

    void FixedUpdate()

    {

    if( state == PLAYER_DEAD ) return( DIE_STATE );

    else

    {

    player presses keys ect;

    }

    }


    Or is there a better way to handle states. Do any plug ins allow me to design states and add my own code scripts? I've seen playmaker but think this is over kill for what I need and would rather program all game logic myself. But it would be nice to have a visualisation of the main state machine.

    So Total rethink, or tool / code to assist, or how to I link the current state with the script event functions?

    Thanks All.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You can certainly continue to use state machines in your Unity games. If you want to do it visually, you can useAnimator state machines, though this may be a bit of a bastardization of their purpose. Or you can find some assets in the Asset Store designed for this sort of thing.

    Personally, though, I just use code, much like what you wrote.

    I'm going to point out one article and one video I've made that I think may be useful to you, given your background. The article is 2D Animation Methods in Unity. Even if you don't care about 2D animation, check out how I code the state machine there as an example of how to do it in C#. Second, you may be interested in learning more about the UnityEvent type, and how to use it for more than just UI (which is where most tutorials are focused). See this video for that.

    Between the two, I think you'll be well-equipped to dig in and start building your first Unity game!
     
    knobby67 likes this.
  3. knobby67

    knobby67

    Joined:
    Aug 30, 2015
    Posts:
    389
    Excellent thanks.