1

I'm taking a tutorial on building a simple behavior Ai. It's 'brain' class is abstract and contains states as in "running","success","failure". Now in the my ai unit - droid class i have a method to start the droid up

public void update(){

    if(Routine.getState()==null){
        Routine.start();
    }
    Routine.act(this, board);

}

Eclipse tells me that the methods from Routine are not valid because:

Cannot make a static reference to the non-static method getState() from the type Routine

Which i can do for getState but i have to change the RoutineState into static as well, but i can't do for start or act as they containt this.

This is the brain of the ai so far:

public abstract class Routine {

    public enum RoutineState{
        Success,
        Failure,
        Running
    }


    public RoutineState getState(){
        return state;
    }

    protected RoutineState state;

    protected Routine() { }

    public void start(){
        this.state = RoutineState.Running;
    }

    public abstract void reset();

    public abstract void act(droid droid, board board);

    public void succed(){
        this.state = RoutineState.Success;
    }

    public void Fail(){
        this.state = RoutineState.Failure;
    }

    public boolean isSuccess(){
        return state.equals(RoutineState.Success);
    }

    public boolean isFailure(){
        return state.equals(RoutineState.Failure);
    }

    public boolean isRunning(){
        return state.equals(RoutineState.Running);
    }



}
Faris Kapo
  • 346
  • 9
  • 30
  • Your question is unclear, please let me know if the linked duplicated question answers your question. – Maroun Feb 28 '16 at 18:36

1 Answers1

3

If state is a class field, it should be static like below.

Otherwise, you'll need an object to get its own state.

public static RoutineState getState(){
    return state;
}

protected static RoutineState state;
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Well i can do that for getState() and have no errors returned. But for start() or act() i can not use it because of abstract – Faris Kapo Feb 28 '16 at 18:39
  • Because you can not instantiate an abstract class. You'll have to make anotther class extend this one and override the abstract methods. – Yassin Hajaj Feb 28 '16 at 18:48