# Struggling to get AI to repeat infinitely

I am following this as a guideline to help me figure out a simple AI for an enemy as I am new to java.

https://www.javacodegeeks.com/2014/08/game-ai-an-introduction-to-behaviour-trees.html

https://github.com/obviam/behavior-trees/tree/master/src/net/obviam/bt

I am calling this function:

AI goombaBrain = Routines.selector(Routines.repeatInfinite(Routines.wander(Board.board, InitObject.goomba)));

InitObject.goomba.setRoutine(goombaBrain);


But once it finishes wandering to a random location once it doesn't automatically reset. Any ideas? if you need any more information let me know. (InitObject.goomba is the enemy)

I've done some code reading and now it's obvious...

you do the following with your code:

1. create a wander Routine
2. create a repeatInfinite Routine for the wander Routine
3. create a selector Routine for the repeatInfinite Routine

so far so good - but if you read the code for the Selector (the selector Routine) you'll notice the following:

// We need to progress the sequence. If there are no more routines
// then the state is the last routine's state. (Success for OR was already handled)
if (routineQueue.peek() == null) {
this.state = currentRoutine.getState();
} else {
currentRoutine = routineQueue.poll();
currentRoutine.start();
}


this part of the code will REMOVE the repeatInfinite Routine for the wander Routine from the selector Queue and hence it will be no longer executed. See LinkedList.poll() for details.

## Solution

if you really want to infinitely execute the repeatInfinite Routine for the wander Routine then you have to do it solely, without the selector:

AI goombaBrain =  Routines.repeatInfinite(Routines.wander(Board.board, InitObject.goomba));
InitObject.goomba.setRoutine(goombaBrain);

• Hmm still not working. I realized something though, the Wander routine never is properly finished, it just activates the MoveTo and that finishes but the wander routine is never told it succeeded/failed so it never resets. any ideas? unless I misunderstand what it means – MrEDog Jan 23 at 15:51
• WAIT I was missing some code in Wander!!!! But thanks for your fix anyway, even if I had found my error it still wouldnt have been working but now it is thanks to you :D – MrEDog Jan 23 at 16:07