TL;DR: What are the guidelines to create a service vs a task for a specific action?
I’m looking into Behavior Tree and I’m looking into some guidelines on when to use a service vs task.
For example, I was thinking on using a task to find the AI enemies (like the player), using a perception component.
I then looked at the ActionRPG example, and noticed they have a service to doing something like that.
I then thought perhaps it’s about an actual action (like moving) vs updating the blackboard. But I found a node in the same ActionRPG example
with a task for “Finding new location round target”, which is the same thing.
So what is it? What are some points/guidelines that can help one decide if he should use a service or a task a behavior tree?
Have you seen what the documentation has to say about the issue? Maybe there is a snippet in there that clarifies the best way of implementing your idea. Unfortunately, I personally do not have an answer for you.
Yup, looked already. Tried to look all over the documentations and examples.
There doesn’t seem to be any guideline as far as I can tell, and I’m not really sure when to use which.
as far as I understand it, services can be attached to selectors and sequences whereas tasks can only be run as children of selectors and sequences. The benefit of this is that services can keep running while you are executing the children nodes: So Imagine you want an enemy to look for your player. If it spots you, it must move to you and attack. If you are out of range, it must then continue on with its patrol root.
So I use a task to look for your player:
selector.
/ \
Sequence task(move to next patrol point)
/ \
task look task attack.
The enemy will look for the player. It doesn’t see him, so it will move to its next patrol point.
Now you run up to the enemy while it is on its way back to it’s next patrol point. nothing will happen, because the tree is waiting for the enemy to reach its next patrol point before it attempts to look for you again.
On the other hand, lets say I use a service instead.
selector.
service(look for player)
/ \
attack task(move to next patrol point)
In the “look for player” service blueprint, We will override the event receive tick function. Now it will keep looking for your player on every tick, because a service runs parallel to its children. So the service will look for your player. If it doesn’t find him, the attack fails and the enemy then starts walking back to his patrol point, but look for player is still firing off on every tick. So when you get close to the enemy, the service will interrupt your (move to patrol point) task, and the enemy will attack the player right away