Example 3
This example will be an extension of Example 2, so please firstly follow Example 2 before doing this one.
In this example we will add Saving Data when Subject is despawned and loading it when it is spawned back.
And we will also use ShouldCalculateIsSeen option.
Let's say our AI Character have Health variable that should be kept even when its spawned/despawned.
In case we injured it, moved away from it and later came back to it, AI should still be injured.
Firstly we need to create save data blueprint that will store variables of our AI character.
To do that, create new blueprint with parent class AIOData_Base and name it AIOData_AICharacter.
Open newly created blueprint and add Health float variable.
Go back to AI Character blueprint, and add events OnPreDespawn & OnPostSpawn.
These events are called right before subject is despawned and right after it is spawned ( After BeginPlayer ).
Now setup these events to save & load Health state.
This way whenever AI Character is being despawned, it will store Health state in data file, and will load it when it is spawned again.
Final setup of SubjectComponent will look almost the same as in Example 2, with only changes in ShouldCalculateIsSeen & DataClass
- 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.
- Priority doesn't matter in this case, it can be left at 0.
- Set DataClass that we have created before.
- We need 3 Optimization Layers - 1500/2500/3500 to optimize AI based on that.
- We want to adjust settings in optimization layers based on whether AI Character is visible by any Invoker or not, thus ShouldCalculateIsSeen is set to true.
According to plan of this example, final setup of function would look like that