This question is related to How to Queue and Call Actual Methods... Anyway, I've decided to (after all) go with the anonymous class idea. The problem is that when I ADD my anonymous class to the linked list, it's actually calling execute() immediately... and it shouldn't be. Execute() is to be called later. Anyway, this is what I have:
private LinkedList<AgentAction> actions;
public boolean blockingSensor;
this.actions.add( new AgentAction(this) {
public void execute() {
//setRotationalVelocity(0);
kinematic.setWheelsVelocity(0,0);
this.agent.setBlockingSensors(false);
this.agent.printLCD("Turn, blocking = "+this.agent.blockingSensor);
}
public Object getValue() {
return null;
}
});
//this is essentially the main()
public void performBehavior()
{
//make sure to only call run() each tick, not every ms
if ( this.oldCounter < getCounter() )
{
if ( !isWorking() )
{
run();
}
this.oldCounter = getCounter();
this.actions.removeFirst().execute();
}
}
abstract class AgentAction
{
SimbadAgent agent;
public AgentAction(SimbadAgent a)
{
this.agent = a;
}
public abstract void execute();
public abstract Object getValue();
}
run() is an abstract method that is implemented by a child class. I'm just not sure why it's printing when it's added, rather than executed. I understand this would imply that performBehavior() is actually being executed multiple times rather than once per tick, but that's not the case.