This example will be a bit more advanced than previous one.
Instead of just letting subsystem to spawn/despawn this subject, we will add layers and optimzie AI based on current layer that AI is in.
The goal is to make AI fully functional when Invoker is within radius 1500.
Disable some features like Actor Ticking, Shadows when Invoker is within radius 2500.
Disable more features like behavior tree/character movement/collision when Invoker is within radius 3500.
Despawn AI when invoker is beyond 3500 radius.
- We want AI to be updated by subsystem right after its spawn, thus CanBeUpdatedBySubsystem is set to true.
- We don't want to allow subsystem to spawn/despawn this subject because we will do it manually, thus AllowSubsystremToDespawn is set to false.
- AllowSubsystremToDespawn is set to false, this this value doesn't matter.
- Priority doesn't matter in this case, it can be left at 0.
- We don't save any data of AI so DataClass can remain empty.
- We need 3 Optimization Layers - 1500/2500/3500 to optimize AI based on that.
Is Beyound Last Layer - True if Invoker is in radius beyond radius of last specified Optimization Layer.
For example in this case if last Optimization Layer has 3500 radius
if closest Invoker is at 4000 distance to this subject, then it would be true
but if Invoker is at 3000 distance, then it would be false.
- Layer Index - Current Layer Index that this Subject is in, depends on location of closest Invoker.
- Is Seen - Whether this actor is currently seen by any Invoker ( in this case always true because we set to not calculate it ).
SubjectComponent has useful function that can be used with Characters - SetCharacterFeatures.
This function allows to specify which features should be enabled or disabled, and we can use it in this case.
- AIBrain - Enable/Disable behavior tree of AI Character.
- Movement Component - Enable/Disable Character Movement Component.
- Visibility - Show/Hide Actor.
- Collision - Enable/Disable Actor collision.
- Animations - Unpause/Pause animations of Character Mesh.
- ActorTick - Start/Stop Ticking of Character.
- Shadows - Show/Hide Shadows of Character Mesh.
According to plan of this example, final setup of function would look like that
To despawn this AI Character when it's beyond last layer ( above 3500 radius ), it used function DespawnSubject from AIOptimizerSubsystem.
OverrideSpawnRadius was left at -1, it means that it will be automatically calculated ( to be ~3500 ),
so when Invoker will get closer to this subject, subject will be respawned.