Behavior tree task to happen once per call

Hi all

I’m really struggling to understand behavior trees, they just seem to fire off everything, all the time, which is ok if you’re doing AI for an FPS, but I’m doing a strategy/civ-style game. I know there’s a way to do this properly, but information/documentation seems pretty sparse, especially if you’re not doing an FPS-style game.

I had this all working nicely in a Blueprint, but then I discovered the behavior trees, and thought it would be better to do AI stuff in here. So far, I have 2 nights work on these things and nothing to show for it :frowning:

I really don’t know if this is even the correct way to do it, but what I’m going for is:

  1. During the day, if the city needs food, AssignHunters
  2. During the day, if the city needs shelter, BuildHouses
  3. When it becomes evening, check how much food we have. If we have an excess, increase population. If there’s a deficit, decrease population.

As it stands, NeedFood is false, so then it hits NeedShelter, and it just repeats it all day. I think this is because it doesn’t factor houses under construction into the equation, so I’m busy fixing that.

The big thing is, though, that once it becomes evening, AdjustPopulation just fires off the entire night, so population spikes as all the food is eaten, then everyone starves.

Is there some way to just fire AdjustPopulation once when the enum changes to evening?

Also, how can I check NeedFood and NeedShelter less often? It really doesn’t need to check food levels every tick!

Any help will be greatly appreciated!

It all sounds correct. When a BT is used to control AI’s logic the BT is traversed from the root to find first valid action. Then that action gets executed. Once that task is done the next task is performed. What is the next task depends on the structure of the tree. In your case there’s no “next task” so it just repeats the whole tree from scratch.

The solution to your problem is adding sequences and Wait tasks in appropriate places.

Cheers,

–mieszko

Awesome, thanks for clarifying that. I think I’m getting what Behavior Trees in UE are about now, thanks for the assistance, it’s working great.

For future reference, what I ended up doing was getting the length of a day, and then adding that as a delay, but let the parent node abort if it was no longer evening, so problem solved :slight_smile: