Search Unity

What is the best of orginizing player controller scipts

Discussion in 'Scripting' started by scifresh, Sep 4, 2015.

  1. scifresh

    scifresh

    Joined:
    Oct 8, 2013
    Posts:
    23
    Hey everybody,

    Im an experienced programmer but really new to game programming. And my programming experience doesnt include timers or doing everything in an update method. so im pretty confused at this point.

    my player controller code is getting bigger and bigger every day and i seriously dont like it. there has to be a way to organize the code, for example state machines, events etc.

    so what is the professional way of handling events / actions / states ?

    right now i have different function in update: handleinput, handleanimation, handleinteractions, handlegui etc.

    i have made a research and came across to finite state machine but im struggling to understand it.

    the script i found is this : http://wiki.unity3d.com/index.php/Finite_State_Machine

    and last question : what is the difference between an event system and state machine ?

    thanks in advance for your time.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    A state machine is a pattern where there are multiple states, but the object can only be in one state at a time. The simplest case of this is simply an enum with a giant switch statement.

    An event system is a way to rely messages between objects, there are a ton of different implementations, including one built in in Unity.

    The easiest way to bring in some organisation is to break your script up into smaller components. Have one script that controls animation, one that controls movement, one for UI and so forth. Putting in interfaces between these components further helps with structure and organisation.

    Here is a video describing component based design. It's probably the most fundamental pattern for working in Unity.

     
  3. scifresh

    scifresh

    Joined:
    Oct 8, 2013
    Posts:
    23
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Unity gems is pretty great.