for (int32 i = 0; i < CollectedActors.Num(); i++)
{
AACollectActor* TestPickup = Cast(CollectedActors[i]);
if (TestPickup && IsValid(TestPickup) && TestPickup->IsActive())
{
ABatteryCollectActor* const TestBattery = Cast<ABatteryCollectActor>(TestPickup);
if (TestBattery)
{
CollectPower += TestBattery->GetPower();
}
TestPickup->SetActive(false);
}
//TestPickup->WasCollected(); <- Triggering an abort
}
if (CollectPower > 0.f)
{
UpdatePower(CollectPower);
}
the implementation of void AACollectActor::WasCollected() in the parent class AACollectActor is empty, and the implementation of void ABatteryCollectActor::WasCollected() override in the child class is also empty. However, when the statement TestPickup->WasCollected() is uncommented, it will cause an crashed in UE5.3.1,why?Just because inheritance is used?
raw pasting code can be difficult to read if you put the code into a block using the non-Shift “Tilda” (the button commonly to the left of the “1” key) x3 on a new line to start the code block and then again on a new line after the code block to close
then you can get a block like this that even gets a scroll bar if over a certain line number.
these will even respect indents, and forced spaces
making them easier to read.
what is your inheritance chain for this? ABatteryCollectorCharacter : AACollectActor : AActor something like this?
how is the actor being instantiated, spawned, or held?
is this function being called internally to the class, or is it being called by something else as the result of a cast?
what does the declaration of the function look like? is this a virtual with an override or are you using the inheritance chain?
your if (TestPickup && IsValid(TestPickup) && TestPickup->IsActive()) these are all testing the same thing, technically the *->IsActive() has additional value for UObjects, but this can be wrapped into the same line as the declaration
if (AACollectActor* TestPickup = Cast<AACollectActor>(CollectedActor[i])
{
ABatteryCollectActor* const TestBattery = Cast<ABatteryCollectActor>(TestPickup);
if (TestBattery)
{
CollectPower += TestBattery->GetPower();
}
TestPickup->SetActive(false);
// "by putting this outside of the if check there is a chance the Cast failed, and therefore it would be a "nullptr" so it attempting to call a member function is an unhanded NullReferenceException"
TestPickup->WasCollected()
}
an additional positive of wrapping the declaration into the If check is that it ensures a kind of scope lock where you are designating “this variable shall only live inside and is only safe to access inside this if block, and nowhere else.” you can do the same with you TestBattery declaration and check.