E - type of the blackboard object that tasks use to read or modify game state@TaskConstraint public abstract class Task<E> extends Object
Task of a behavior tree has a status, one control and a
list of children.| Modifier and Type | Class and Description |
|---|---|
static class |
Task.Status
The enumeration of the values that a task's status can have.
|
| Modifier and Type | Field and Description |
|---|---|
protected Task<E> |
control
The parent of this task
|
protected Task<E> |
guard
The guard of this task
|
protected Task.Status |
status
The status of this task.
|
static TaskCloner |
TASK_CLONER
The clone strategy (if any) that
cloneTask() will use. |
protected BehaviorTree<E> |
tree
The behavior tree this task belongs to.
|
| Constructor and Description |
|---|
Task() |
| Modifier and Type | Method and Description |
|---|---|
int |
addChild(Task<E> child)
This method will add a child to the list of this task's children
|
protected abstract int |
addChildToTask(Task<E> child)
This method will add a child to the list of this task's children
|
void |
cancel()
Terminates this task and all its running children.
|
protected void |
cancelRunningChildren(int startIndex)
Terminates the running children of this task starting from the specified index up to the end.
|
boolean |
checkGuard(Task<E> control)
Checks the guard of this task.
|
abstract void |
childFail(Task<E> task)
This method will be called when one of the children of this task fails
|
abstract void |
childRunning(Task<E> runningTask,
Task<E> reporter)
This method will be called when one of the ancestors of this task needs to run again
|
abstract void |
childSuccess(Task<E> task)
This method will be called when one of the children of this task succeeds
|
Task<E> |
cloneTask()
Clones this task to a new one.
|
protected abstract Task<E> |
copyTo(Task<E> task)
Copies this task to the given task.
|
void |
end()
This method will be called by
success(), fail() or cancel(), meaning that this task's status has
just been set to Task.Status.SUCCEEDED, Task.Status.FAILED or Task.Status.CANCELLED respectively. |
void |
fail()
This method will be called in
run() to inform control that this task has finished running with a failure result |
abstract Task<E> |
getChild(int i)
Returns the child at the given index.
|
abstract int |
getChildCount()
Returns the number of children of this task.
|
Task<E> |
getGuard()
Returns the guard of this task.
|
E |
getObject()
Returns the blackboard object of the behavior tree this task belongs to.
|
Task.Status |
getStatus()
Returns the status of this task.
|
void |
reset()
Resets this task to make it restart from scratch on next run.
|
abstract void |
run()
This method contains the update logic of this task.
|
void |
running()
This method will be called in
run() to inform control that this task needs to run again |
void |
setControl(Task<E> control)
This method will set a task as this task's control (parent)
|
void |
setGuard(Task<E> guard)
Sets the guard of this task.
|
void |
start()
This method will be called once before this task's first run.
|
void |
success()
This method will be called in
run() to inform control that this task has finished running with a success result |
public static TaskCloner TASK_CLONER
cloneTask() will use. Defaults to null, meaning that copyTo(Task)
is used instead. In this case, properly overriding this method in each task is developer's responsibility but this gives you
the opportunity to target GWT.
For instance, if you don't care about GWT, you can let Kryo make a deep copy for you like that
Task.TASK_CLONER = new TaskCloner() {
Kryo kryo;
@Override
public <T> Task<T> cloneTask (Task<T> task) {
if (kryo == null) {
kryo = new Kryo();
kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
}
return kryo.copy(task);
}
};
protected Task.Status status
protected BehaviorTree<E> tree
public final int addChild(Task<E> child)
child - the child task which will be addedIllegalStateException - if the child cannot be added for whatever reason.protected abstract int addChildToTask(Task<E> child)
child - the child task which will be addedIllegalStateException - if the child cannot be added for whatever reason.public abstract int getChildCount()
public E getObject()
IllegalStateException - if this task has never runpublic void setGuard(Task<E> guard)
guard - the guardpublic final Task.Status getStatus()
public final void setControl(Task<E> control)
control - the parent taskpublic boolean checkGuard(Task<E> control)
control - the parent tasktrue if guard evaluation succeeds or there's no guard; false otherwise.IllegalStateException - if guard evaluation returns any status other than Task.Status.SUCCEEDED and
Task.Status.FAILED.public void start()
public void end()
success(), fail() or cancel(), meaning that this task's status has
just been set to Task.Status.SUCCEEDED, Task.Status.FAILED or Task.Status.CANCELLED respectively.public abstract void run()
public final void running()
run() to inform control that this task needs to run againpublic final void success()
run() to inform control that this task has finished running with a success resultpublic final void fail()
run() to inform control that this task has finished running with a failure resultpublic abstract void childSuccess(Task<E> task)
task - the task that succeededpublic abstract void childFail(Task<E> task)
task - the task that failedpublic abstract void childRunning(Task<E> runningTask, Task<E> reporter)
runningTask - the task that needs to run againreporter - the task that reports, usually one of this task's childrenpublic final void cancel()
protected void cancelRunningChildren(int startIndex)
startIndex - the start indexpublic void reset()
public Task<E> cloneTask()
TASK_CLONER the new task is
instantiated via reflection and copyTo(Task) is invoked.TaskCloneException - if the task cannot be successfully cloned.protected abstract Task<E> copyTo(Task<E> task)
cloneTask() only if TASK_CLONER is
null which is its default value.task - the task to be filledTaskCloneException - if the task cannot be successfully copied.Copyright © 2017. All rights reserved.