Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Clean character state system.

Discussion in 'Scripting' started by Nemox, Dec 2, 2010.

  1. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    So I'm in the beginning stages of applying character states to my movement system, and I'm not entirely sure about how I want to do it yet. Right now what I'm thinking of is having a string that specifies what state the character is in. When I create a new ability or something, the state could change. So certain abilities will only be usable when in a certain state. "Dive Attack" could only be used while "Flying" 'n' such.

    I could also have a little cooldown timer that applies to all skills. That way there's a bare minimum time to finish something before moving on to another action. Also, that could leave certain states alone to allow more flexibility. To attack while running, don't change the state, just add to the cooldown.

    Then, in case any abilities use the same button by some mistake, only one of them will be activated.
    (Which one though? Would scripts be run alphabetically?)


    So my questions to you are, how would you handle character states? And would you consider mine a viable solution?
     
    Last edited: Dec 2, 2010
  2. thelastowl

    thelastowl

    Joined:
    Jul 30, 2010
    Posts:
    71
    i would have each state as a boolean so when if idle=true then run=false, it can also be added to use cooldown and key inputs as well, look at the example on unity wiki to stop double jumping it should give you a good starting point.
     
  3. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
  4. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
  5. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Yes, you should see a psychiatrist as soon as possible.
     
  6. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    Great, another psychiatric issue to deal with. :p

    "Roses are #FF0000,
    Violets are #0000FF,
    All my base are belong to you."


    Anyways, it turns out I already knew how to make a simple state machine without realizing it. That brings me back to the use of strings instead of Enums. It appears that an Enum can only use what's actually listed in it, so if I wanted a certain skill to bring the character to a different state, I'd have to add the state to the Enum, then call it with the skill.
    But since my game will be designed to be fairly modular, I can't rely on such a rigidly structured structure. So that's why I'd prefer to use a string; I can set the state to "dive" without actually changing the enum to include "dive". Everything is contained within the skill.

    Or am I just wrong about the use of Enums?