Example 4
In this example we will setup AI Character that is spawned when player enters some region,
and is despawned when player leaves that region.
This is pretty common situation, when we want to spawn some actors/AI Characters only when player is inside a building,
changes condignation in a dungeon or something like that.
There is no need to have all AI Characters spawned and fully functional if we know player can't see or interact with them.
Before we set up AI Character, firstly we need some kind of spawner, for that case I created new Blueprint BP_SimpleActorsSpawner.
It simply takes number of actors to spawn and class of spawned actors.
And then it spawns then in random points within its Box Component.
We need another blueprint that will keep track whether player has enterted or left that room, it will be called BP_OptimizerVolume.
It will have a box component that gets notified when player begin or end overlaps with it, forcing AI Characters to be spawned or despawned.
It will also have a reference to BP_SimpleActorsSpawner to know about spawner subjects.
In the AI Optimizer system each subject ( AI Character ) has a unique Id called Handle.
It can be used to find subject data within the system, force his despawn or his spawn ignoring all other conditions like distance to Invoker etc.
BP_OptimizerVolume will operate on Handles of AI Characters.
Right after AI agents are spawned, it collects their Handles and stores them in the array.
Having the Handles it will despawn Subjects when Player leaves the room.
And spawn them back when Players enters the room.
Setup of subject component would look like that
- We don't need to update this subject at all because spawning/despawning will be done manually when player enters or leaves the room, thus CanBeUpdatedBySubsystem is set to false.
- We don't want to allow subsystem to spawn/despawn this subject because it will be done manually, thus AllowSubsystremToDespawn is set to false.
- Priority doesn't matter in this case, it can be left at 0.
- We don't save any data, thus DataClass can remain empty.
- We don't use any other optimizations based on distance, thus Optimization Layers is empty.
- We don't need to know whether subject is seen or not by Invokers, thus ShouldCalculateIsSeen is set to false.