Setting up periodic casting in DOTS

Hi everyone,
trying to get some basic understanding on how this works by building a quick example scene.

My goal is the following:
- Have the player auto-cast, in a loop, every X seconds
- Have the cast spell be defined as a ScriptableObject
- Have the X seconds interval defined as a property in my Stats component

I've seen that there is a Wait node but seems like I cannot "bind" it to my Stats component so I would have to build one manually.
My initial thought is tthat things are supposed to work more or less like this:
- A node is initialized with the current X seconds based on the Stats component
- The corresponding system decreases the time by DeltaTime on each tick
- If the time is <= 0 then the Task is successful and we can move to the next Task in the Sequence which will perform the casting

I don't know if this is correct and I'm looking for some guidance on the best practices for this.
I'm also planning to build this as an IJobEntity because the same logic will also apply to all the enemies on screen.

EDIT - additional question: how can I bake a projectile Prefab in my sub-tree (e.g. having it as a GameObject variable) so that it is stored in my custom component? The AddBufferElement doesn't allow to bake prefabs to entities...
 
Last edited:
I've seen that there is a Wait node but seems like I cannot "bind" it to my Stats component so I would have to build one manually.
Are you referring to the Property Bindings feature? Make sure you have a Float getter and setter:


EDIT - additional question: how can I bake a projectile Prefab in my sub-tree (e.g. having it as a GameObject variable) so that it is stored in my custom component? The AddBufferElement doesn't allow to bake prefabs to entities...
Entity tasks cannot reference GameObjects since it's a reference instead of a value. With non-Entity objects there isn't any baking.
 
Thanks for the answer, let me rephrase the second question in a different way.

Let's start from the Entity task sample that is bundled with your package and let's assume I wanted to have the Fire node shooting a random projectile while also making this projectile configurable (e.g. the node should receive the prefab as a variable and instantiate that prefab using an EntityCommandBuffer in its system).
How would you do that?

What I'm currently doing is having a ProjectileAuthoring that takes the prefab GameObject and bakes it into a ProjectileComponent that is then read by the Fire system to get the Entity to spawn, but I was wondering if there was a better way that didn't require writing custom authoring components at all.

EDIT: on a side note, with the authoring I'm using I can't get the behavior tree attached to the Projectile to be baked. Projectiles spawn fine but they don't have any of the behavior tree's components.

For reference, this is how the Projectile prefab is set:
1746558066374.png

this is the spawned Entity:
1746558173102.png

As you can see it misses all the BehaviorTree components, which are there if I manually place the Prefab in the scene instead of creating it via an ECB:
1746558246409.png

EDIT 2: sorry for the confusion but I'm still in the early stages with BD Pro :)
Quick recap of what I'm trying to achieve:
- My Fighter entity has a behavior where it executes an Attack every X seconds
- The Attack spawns a Projectile
- The Projectile entity has a behavior where it moves in a specific direction

All of this in DOTS and dynamic, as in I can change the Projectile prefab in my Fighter's authoring to a different one with a different attached behavior.
How would you do that?
 
Last edited:
Ahh, your current system makes sense. With that said, I haven't gotten as much feedback on the Entity workflow so I'm curious if you have any ideas to improve the workflow. Maybe a reference to the GameObject within AddBufferElement?

For baking, can you insert a breakpoint within BehaviorTreeBaker.Bake and determine where it is returning early?
 
So, my suggestions would be the following (as an high level discussion, I don't know the architecture of BD Pro!):
  • allow the usage of ScriptableObject variables in trees (optional but might bring some value given that ScriptableObjects are widely used in ECS)
  • Change AddBufferElement(World world, Entity entity) to AddBufferElement(World world, Entity entity, Baker<BehaviorTree> baker) so that we can bake GameObjects in our Buffer elements
  • As an alternative, bake all the GameObject variables as Entities during the BehaviorTree baking to make them available in the same way GameObject variables are available now
Generally speaking, what I think it's lacking is the ability to have other entities as variables for nodes. For example I might want to have a behavior to follow the movement of a specific entity and the only way to do that as for now is to tag that entity with a specific component and do the logic in the system.
But what if I wanted to build a generic "follow-entity" node where the target entity is simply a variable?

As for your second question, what I find odd is that baking works the first time I create the prefab but it stops working as soon as I stop the scene and re-play it.
On the first case the following code works, on the second case I can see that it goes in the continue instruction and it skips baking altogether.

C#:
if (!behaviorTree.InitializeTree(worlds[i], entity)) {
    continue;
}

I'm willing to provide any information you need but I really need to have this working as soon as possible so please let me know if even a repro project might help :)
 
  • allow the usage of ScriptableObject variables in trees (optional but might bring some value given that ScriptableObjects are widely used in ECS)
  • Change AddBufferElement(World world, Entity entity) to AddBufferElement(World world, Entity entity, Baker<BehaviorTree> baker) so that we can bake GameObjects in our Buffer elements
  • As an alternative, bake all the GameObject variables as Entities during the BehaviorTree baking to make them available in the same way GameObject variables are available now
Thanks, I think the new parameter for AddBufferElement makes the most sense. I'll change add this for the next update.

As for your second question, what I find odd is that baking works the first time I create the prefab but it stops working as soon as I stop the scene and re-play it.
On the first case the following code works, on the second case I can see that it goes in the continue instruction and it skips baking altogether.
Hmm, yeah, having some type of repro would help. Does the Entity sample scene exhibit the same behavior? There is an EntityAgent prefab. If you can list the steps to reproduce with that sample scene that would be great, otherwise a repro package with the steps to reproduce will also work.
 
Hey there, sorry for resurrecting this old thread but I had other priorities.

Did you add the new parameter for AddBufferElement yet?
I'm considering restarting the project and I'd like to test that.
 
I have it in version 2.1, but right now it's unstable due to some other changes. I decided to push it to version 2.1 since it's a breaking change and I didn't want to make that with a bugfix update.
 
Back
Top