What is Faster - Local Variables or BlackBoards for AI

Hey,

We have been discussing AI and have 2 options of sharing variables across multiple AI with different blackboards. Our AI will be wandering around waiting (Flocking) or coming straight for the player. Can be up to 10 groups x 10 enemies scattered around per level that will be active at one time for arguments sake. Each AI Character will have Local Variables to reference their group.

Each group will need to share the following variables for different behaviors:

  • Vector: TargetLocation
  • Bool: PlayerSeen
  • Bool: HasLeader (Used for different types of flocking behaviors)
  • Bool: CanFlee

First option is having is having each AI Character having their own Local variables of each of these. To share the information we will have each AI character iterate in their behavior tree through their local group array and look for each variable they need at any given time. This will of course need to have cast to the BaseEnemy to search for the variable.

The other Option is to have each AI character use a blackboard. So the same as option one they will need to search through their array but in this case they wont need to cast to BaseEnemy, but in turn have to search each actors blackboard and find the variable through the LiteralSearch option.

We don’t really know the ins and outs of UE4 engine and how it works so working out which is faster is a little difficult. Any help will be greatly appreciated.

Hopefully that all makes sense!

Thanks,

I can’t really comment on which is “faster”… I assume you mean which one processes more quickly, uses the least CPU cycles, and would cause less of a frame rate problem? Lets assume for the sake of argument that is your definition of “faster”.

In the end I’m not sure what you’ve described would really make much of a difference; doing things in Blueprints has been said to be about 10% slower than doing the same thing in C++, and my expectation is that doing AI with the Blackboard will generate a similar drawback. If you want it as fast as it can possibly be, there’s no substitute for simply writing it in C++.

In practice, though, you probably won’t actually notice a difference during gameplay unless your AI is exceptionally advanced, and maybe not even then.

Now, as far as actually building AI… I’ve found the Blackboard method to be overly complicated for making simple AI. It shouldn’t require making so many actors and all this extra Blackboard stuff just to get a character to wander around a space and aggro a player. While Blackboards are ideal for advanced AI, I feel it’s overkill for simple interaction. Instead, I’d suggest using the simple AI MoveTo functions, in conjunction with the AI Pawn Sensing components. You can basically get the entire Patrol/Aggro/Reset process down to three simple Blueprint snippets. Place the code inside your enemy’s Blueprint (which will need to derive from Character, and have a Pawn Sensing component as well as an AI Controller). I’ve made a screenshot of an implementation I’m currently using, which you can see below.

The “OnSeePawn” event gets called whenever the Enemy sees another actor. In this case, you cast to your player class, to check whether or not the enemy has seen the player. You then use the AI MoveTo function to continually and smoothly move toward the player. In the example above, I also set values from the Enemy’s AnimBP, so as to make him animate (in this case, now he runs).

If he can’t see the player (“InRange” bool), he’s in Patrol mode. This basically does the same deal, only instead of the player, it randomly selects a location inside of your NavMesh, and moves the enemy to that location. I set the “Moving” variable so that this function cannot be called again until the enemy arrives at his next location.

The “Return to Patrol” snippet simply checks for the distance between the enemy and the player. If the player is out of the enemy’s sight radius, the enemy returns to patrolling.

Hope that helps.

Hello ,

Sorry for the late reply.

Yes i meant faster as in processing wise. We know that C++ is quicker already but were just wondering how constantly accessing blackboards compares to accessing local variables since we couldn’t really find too much information on it. We had a feeling the performance difference would be very minimal but we thought what was the harm in asking.

Thanks for the reply :slight_smile: