0

I have the following base class

public abstract class Character : MonoBehaviour
{
    protected abstract Enum CurrentState
    {
        get;
        set;
    }
}

and the following child class

public class Player : Character
{
    protected override Enum CurrentState
    {
        get
        {
            return (State)_anim.GetInteger("State");
        }
        set
        {
           _anim.SetInteger("State", Convert.ToInt32(value));
        }
    }

    private enum State
    {
        IDLE = 0,
        WALK = 1,
        JUMP = 2,
        FALL = 3,
        CLIMB = 4,
        LOOKING_DOWN = 5,
        NPC = 6,
        IMPATIENT = 7,
        LOOKING_UP = 8,
        STUCK = 9,
    }

    void FixedUpdate()
    {
        if (CurrentState == State.CLIMB)
        {

        }
    }

}

The line

if (CurrentState == State.CLIMB)

yields the following error: Operator '==' cannot be applied to operands of type 'Enum' and 'Player.State'

Any help? The getter works fine. So maybe I need to do a cast conversion in the set accessor? I'm not really sure...I'm kind of new to this...Any help would be more than appreciated.

Sean Carey
  • 799
  • 5
  • 12
  • try changing private enum to public enum that is outside of your class object –  May 10 '18 at 19:20
  • 1
    What is confusing you? `CurrentState` is type `Enum`, `State.Climb` is type `State` – maccettura May 10 '18 at 19:20
  • a concise answer for enum catch22's located here: https://stackoverflow.com/questions/22335103/c-sharp-how-to-use-get-set-and-use-enums-in-a-class –  May 10 '18 at 19:21
  • you can see how to compare enums here: https://stackoverflow.com/questions/19537083/c-sharp-enum-how-to-compare-value – Laura Vieira May 10 '18 at 19:22
  • I think you want a CurrentState property that sets/gets a value of type State. CurrentState shouldn't be an enum. – anu start May 10 '18 at 19:25
  • Does each character have its own state type? If so, you could make `Character` generic, i.e. `Character` with `CurrentState` of type `TState`. Specific types could then specify a type for `TState`, which could be whatever you like, incl. enum. – tukaef May 10 '18 at 19:33

4 Answers4

0

You are comparing an Enum with State. Which is illegal. You return type for CurrentState should be State. Something like -

protected override State CurrentState
{
    get
    {
        return (State)_anim.GetInteger("State");
    }
    set
    {
       _anim.SetInteger("State", Convert.ToInt32(value));
    }
}
Arpit Gupta
  • 1,209
  • 1
  • 22
  • 39
0

As noted in previous post, illegal operation of Enum. Do not do that.

 namespace ConsoleApplication1
    {
        public enum State
        {
            IDLE = 0,
            WALK = 1,
            JUMP = 2,
            FALL = 3,
            CLIMB = 4,
            LOOKING_DOWN = 5,
            NPC = 6,
            IMPATIENT = 7,
            LOOKING_UP = 8,
            STUCK = 9,
        }

        public abstract class Character : MonoBehaviour
        {
            protected abstract State CurrentState
            {
                get;
                set;
            }
        }


        public class MonoBehaviour
        {
        }

        public class Player : Character
        {      

            protected override State CurrentState
            {
                get
                {
                    return (State)_anim.GetInteger("State");
                }
                set
                {
                    _anim.SetInteger("State", Convert.ToInt32(value));
                }
            }



            void FixedUpdate()
            {
                if (CurrentState == State.CLIMB)
                {

                }
            }

        }

    }
  • So if another object besides Player inherits from Character, where should that object define its states? – Sean Carey May 10 '18 at 21:18
  • Enums can be namespaced to give clarity to which one you need. You can put them anywhere you want but keep in mind enum helps for clarity in your code as to which integer value you are expecting in English. –  May 11 '18 at 03:06
0

Try this:

if (Convert.ToInt32(CurrentState) == Convert.ToInt32(State.CLIMB))
            { 

            }

As Enum's cannot be compared as types you need to convert them for comparing.

Ajay Reddy
  • 113
  • 6
0

If I replace the property with two different accessors, it seems to work...

protected override Enum SetCurrentState
{
    set
    {
        _anim.SetInteger("State", Convert.ToInt32(value));
    }
}

private bool GetCurrentState(State state)
{
    return Convert.ToInt32((State)_anim.GetInteger("State")) == Convert.ToInt32(state);
}
Sean Carey
  • 799
  • 5
  • 12