Is variable initialization necessary?

Hello!

  1. If the code Emaer posted is correct there is a mistake in that if statement on 205 line, because now basically what you’re doing is checking if the CurrentTarget is NOT valid and then if that is true(if it’s not valid) then you check variables in it, so you’re trying to access nullptr and that is why you get EXCEPTION_ACCESS_VIOLATION error.

To sum up to fix it you need to delete “!” from the beginning of check.

if(UKismetSystemLibrary::IsValid(CurrentTarget) && 
((CurrentTarget->ActorHasTag("AIR") && AirOrGround==0) || 
(CurrentTarget->ActorHasTag("GND") && AirOrGround==1)))

Why is that enough? Well that’s a great question. If the “if” statements consist of more than one condition and they are seperated by AND(&&) the program will preform checks in a row and if one condition fails the whole check will be abandonded so your code after false validation won’t execute. That way we are safe from any more nulls :smiley: .

  1. Emaer is right at the moment your Array in that method is a copy of the one you’re passing, to fix that you need to pass a reference to the array.

That is how your method declaration should look like.

void AddActorToArray(TArray<AActor*>& ActorsInRange, AActor* OtherActor)
1 Like