I’m using an AIPerceptionComponent in my AIController class that configures a sense.
Then on UAIPerceptionComponent::OnRegister(…) the component will call UAIPerceptionSystem::RegisterSenseClass(…).
Now if that sense is not registered yet it will call
UAISense* SenseCDO = GetMutableDefault<UAISense>(SenseClass);
SenseID = SenseCDO->UpdateSenseID();
It will eventually end up here:
FAINamedID(const FName& InName)
: Index(GetCounter().GetNextAvailableID()), Name(InName)
{}
According to the retrieved index it will add elements to the Senses array:
const int32 ItemsToAdd = SenseID.Index - Senses.Num() + 1;
Senses.AddZeroed(ItemsToAdd);
This works find except in the following case:
- Running the editor in debug mode start a pie session.
- compile the code again leading to a hot reload in the editor (make a small insignificant change if it won’t compile otherwise)
- play a pie session
This will go through the above mentioned steps of creating a sense.
But this time the SenseID.Index will be 1 (instead of 0) which leads to 2 items added to the Senses array.
Only the second entry is updated.
This leads to a crash in:
void UAIPerceptionSystem::StartPlay()
{
for (UAISense* Sense : Senses)
{
if (Sense->ShouldAutoRegisterAllPawnsAsSources()) // CRASH
It tries to access Senses[0] which will be null. Senses[1] contains the actually created sense.
I’m running Mac OSX version 10.10.5.
My guess is that it has something to do with the counter
static TCounter& GetCounter()
{
static TCounter Counter;
return Counter;
}
and
Type GetNextAvailableID() { return NextAvailableID++; }
Seems like the static counter is not reset when compiling for a hot reload… Odd.
Please let me know if you need more info.