In my behavior tree, I have a custom composite task called JobChooser.
Just for the sake of it, here is the code:
There is nothing unusual about it.
The problem is that the CanExecute override consistently runs twice every behavior tree tick. I've tried letting the Behavior Manager do the ticks automatically, I've even tried to manually tick it once every 5 seconds. No matter what I do, CanExecute will always run twice in every tick, causing unwanted behavior (requesting a job for my character twice, instead of once).
I'm not sure why this happens - my understanding is that when CanExecute returns true or false, the Task should check what the currentJobIndex is and run the child task with that index.
Am I missing something completely obvious?
Thank you!
Just for the sake of it, here is the code:
C#:
using BehaviorDesigner.Runtime.Tasks;
using UnityEngine;
public class JobChooser : Composite
{
// The index of the child that is currently running or is about to run.
private int currentJobIndex;
// The task status of the last child ran.
private TaskStatus executionStatus = TaskStatus.Inactive;
private Human human;
public IJobInfo currentJob;
public override void OnAwake()
{
human = GetComponent<Human>();
}
public override int CurrentChildIndex()
{
Debug.Log($"Getting currentjobindex");
return currentJobIndex;
}
public override bool CanExecute()
{
if (!JobManager.IsJobAvailable(human))
{
Debug.Log($"Job isn't available, wandering");
currentJobIndex = (int) JobType.Wander;
return true;
}
// TODO RENAME JOBTYPE TO JOBINDEX
currentJob = JobManager.RequestJob(human);
currentJobIndex = (int) currentJob.JobType;
return true;
// if (Children[currentJobIndex].FriendlyName != currentJob.JobType.ToString())
// {
// JobManager.EnqueueJob(currentJob);
// throw new Exception("Job order in BT incongruent with JobManager Job enum!");
// }
}
public override void OnChildExecuted(TaskStatus childStatus)
{
// JOB IS COMPLETED HERE - CHECK CANEXECUTE AND KEEP LOOKING FOR JOBS
executionStatus = childStatus;
Debug.Log("CHanged task status");
}
public override void OnConditionalAbort(int childIndex)
{
// The job chooser was interrupted.
currentJobIndex = 0;
executionStatus = TaskStatus.Inactive;
Debug.Log("JOB CHOOSER WAS ABORTED CONDITIONALLY()");
}
public override void OnEnd()
{
// OnEnd() is called when there are no available jobs for this creature.
executionStatus = TaskStatus.Inactive;
Debug.Log($"JOB CHOOSER CALLED ONEND()");
}
}
There is nothing unusual about it.
The problem is that the CanExecute override consistently runs twice every behavior tree tick. I've tried letting the Behavior Manager do the ticks automatically, I've even tried to manually tick it once every 5 seconds. No matter what I do, CanExecute will always run twice in every tick, causing unwanted behavior (requesting a job for my character twice, instead of once).
I'm not sure why this happens - my understanding is that when CanExecute returns true or false, the Task should check what the currentJobIndex is and run the child task with that index.
Am I missing something completely obvious?
Thank you!