Call task inside another task

Haytam95

Active member
Hi

I've a question about tasks, trying to simplify how my AI tree grows.

1590790515640.png

I have this simple branch that set's a Bool Shared Variable called "canSeeTarget" true or false depending if a character can be seen.

This in code could be reduced to a single task that simply does:


C#:
public class MyCustomTask : Action
{
public SharedBool canBeSeen;
private CanSeeObject canSeeobjectTask;

// Initialize canSeeobject somewhere in start or awake.


public override TaskStatus OnUpdate() {
    canBeSeen.Value = canSeeobjectTask.OnUpdate() == TaskStatus.Successful;
   
    return TaskStatus.Sucessful
}
}

Is this possible? I tried it, and all serialized fields of Can See Object called NullPointerException (Because I didn't initialized them), but if I assign them think it could work.

Is this a correct aproach? Or I'm over complicating things?
 
Hi

I've a question about tasks, trying to simplify how my AI tree grows.

View attachment 3076

I have this simple branch that set's a Bool Shared Variable called "canSeeTarget" true or false depending if a character can be seen.

If both conditions are looking for the same object, you don't need the right branch to check the condition again. This is what selector is for...

111.PNG 222.PNG

This in code could be reduced to a single task that simply does:


C#:
public class MyCustomTask : Action
{
public SharedBool canBeSeen;
private CanSeeObject canSeeobjectTask;

// Initialize canSeeobject somewhere in start or awake.


public override TaskStatus OnUpdate() {
    canBeSeen.Value = canSeeobjectTask.OnUpdate() == TaskStatus.Successful;

    return TaskStatus.Sucessful
}
}

The code approach can also be simplified. Just inherit from CanSeeObject and implement additional logic.

C#:
namespace BehaviorDesigner.Runtime.Tasks
{
    public class CanSeeObjectAndDoStuff : CanSeeObject
    {
        [SharedRequired]
        public SharedBool canBeSeen;

        public override TaskStatus OnUpdate()
        {
            TaskStatus outcome = base.OnUpdate();

            if (canBeSeen != null)
            {
                canBeSeen.Value = outcome == TaskStatus.Success;
            }

            return outcome;
        }
    }
}
 
Last edited:
@abc123 Thanks for your response!

If both conditions are looking for the same object, you don't need the right branch to check the condition again. This is what selector is for...

Thanks, I missed that.

The code approach can also be simplified. Just inherit from CanSeeObject and implement additional logic.

That was the first that I thought, to resolve that particular problem.

Then I reworded my question, because the final objective of this is design an aggro system which uses Can see object; Can hear object and probably will need to set some variables, so it doesn't look right to extend from can see object.

It's a task that require from others, but doesn't have any added value to create a tree managing all possibilities, it's just a few scripting lines with a some conditions. (It's just easier to write it down instead of create the branch and can be used again in other places)


I will keep trying, could be interesting to see it working (And can help to reduce Behavior tree sizes)
 
You're a genius for pointing Conditional Evaluator. It actually does that: Receives a conditional task and calls it's life cycles manually to make it work.

I can do the same with whatever task (or tasks) that I want, as I initially thought.
 
Glad you got it figured out. Just for future info but you can also select a task within the editor by making the task reference serializable/public. This will then show the Select button:

1590834791600.png
 
Top