a7321492391284
New member
Hey y'all,
I wanna use the plugin together with dependency injection (in my case zenject/extenject). I got it up and running, but I was wondering if this is the intended way to do things:
My injection code:
And I then just call
This kinda works fine, but it seems odd to wait for the tree to start and only then inject into the tasks?
Also, if I do
Then my code could just look like this (notice the
Maybe I am just overlooking something, or this is totally the way it's meant to work, but I'm a bit unsure about it.
Hope anyone can provide some guidance for this
Cheers!
I wanna use the plugin together with dependency injection (in my case zenject/extenject). I got it up and running, but I was wondering if this is the intended way to do things:
My injection code:
C#:
public static void QueueAllTasksForInject(this BehaviorTree tree, DiContainer container)
{
tree.CheckForSerialization(true);
tree.OnBehaviorStart += behavior => { InjectIntoBehavior(container, behavior); };
}
private static void InjectIntoBehavior(DiContainer container, Behavior behavior)
{
foreach (var task in behavior.FindTasks<Task>())
{
if (task is BehaviorTreeReference referenceTask)
{
foreach (var externalBehavior in referenceTask.GetExternalBehaviors())
{
externalBehavior.Init();
var tasks = externalBehavior.FindTasks<Task>();
tasks.ForEach(container.Inject);
}
}
container.Inject(task);
}
}
And I then just call
_behaviorTree.QueueAllTasksForInject(Container);
This kinda works fine, but it seems odd to wait for the tree to start and only then inject into the tasks?
Also, if I do
behavior.FindTasks<Task>()
before the behavior has started, Tasks in the external behavior tree referenced from the BehaviorTreeReference task are not included in the returned list.. I kinda just wish there would be a event for when the tree has finished to deserialize, and a method for getting all Tasks, including the ones in external sub trees.Then my code could just look like this (notice the
OnBehaviorInitialized
event and GetAllTasks()
method):
C#:
public static void QueueAllTasksForInject(this BehaviorTree tree, DiContainer container)
{
tree.CheckForSerialization(true);
tree.OnBehaviorInitialized += behavior => { InjectIntoBehavior(container, behavior); };
}
private static void InjectIntoBehavior(DiContainer container, Behavior behavior)
{
foreach (var task in behavior.GetAllTasks())
{
container.Inject(task);
}
}
Maybe I am just overlooking something, or this is totally the way it's meant to work, but I'm a bit unsure about it.
Hope anyone can provide some guidance for this
Cheers!