I have the following code set up in a UDataAsset subclass:
.h
UPROPERTY(EditDefaultsOnly)
TMap<USkill*, UMasteryLevel*> MaxSkillMastery;
.cpp
AssetRegistry.OnAssetAdded() {
...
MaxSkillMastery.Add(Skill, nullptr);
MaxSkillMastery.KeySort(Func);
}
AssetRegistry.OnAssetRemoved() {
...
Iter.RemoveCurrent();
}
As objects of the Key type are created/removed, the map automatically updates itself to have 1 entry per object. The values are then set in a DataAsset in the editor. In my test case, I have 3 key objects and 3 value objects - in the DataAsset I assign 1 value per key.
I then have a unit test that runs in GameInstance::Init() with the following code:
UPROPERTY()
USkill* Chain;
UPROPERTY()
UMasteryLevel* Invalid;
static ConstructorHelpers::FObjectFinder<USkill> ChainFinder(TEXT("Skill'/Game/GameInfo/Skills/Chain.Chain'"));
Chain = ChainFinder.Object;
AssetRegistery.OnFilesLoaded() {
...
Invalid = Cast<UMasteryLevel>(Asset.GetAsset());
}
I do this for all the skills/masteries, and the pointers are all valid and I can access them and everything is great.
Finally, during the unit test I make the following calls:
auto X = MaxSkillMastery[Alchemy]; // success
auto Y = MaxSkillMastery[Sword]; // success
auto Z = MaxSkillMastery[Chain]; // crash sometimes
Sometimes the call with Chain asserts false in FindChecked (the [] operator), sometimes it doesn’t. If I change the DataAsset in the editor from Invalid to Basic and back to Invalid, it seems to no longer crash, but some time later it will start crashing again consistently, until it decides it’s done crashing and it won’t do it again for hours. I’ve also ran the following code:
for (auto X : CharacterClass->MaxSkillMastery) {
UE_LOG(LogTemp, Warning, TEXT("%p, %p, %i"), Skill, X.Key, X.Value->IntVal);
}
This code executes perfectly fine, and then immediately crash a few lines later when it gets to the [] operator. In fact, Skill and X.Key produce the same exact pointer value, and X.Value resolves properly without any nullptr issues. So I know that both the Key and Value are valid pointers, but for some reason FindChecked() is failing regardless.
Any thoughts at all would be appreciated.