OnSphereOverlap in C++ causing crash

Hi guys, trying to get a Melee Attack for my AI in C++ working. I know it looks pretty bad right now but I’m just trying to get it working. Right now it’s not applying any damage to my character and the game is crashing.

I’m trying to apply damage just to the first character in the array, and my character blocks gametracechannel1, hence why I’m going after index 0.

here is the C++:

and the crash report:

edit: This takes place in multiplayer so not sure if I need to get the server to run this?

The problem is that the overlap doesn’t return any actora so the array is empty.

The if() should check if the array is empty, not index 0, then loop all indexes for the apply damage.

1 Like

Thanks I’ll add this in. Do you have any incline as to why the damage isn’t getting done? I’ve drawn a debug sphere and my character is overlapping. I use basically the same function in blueprints and it works, the only difference is I use a sphere component as the location in blueprints but I don’t think that matters.

Here is the bp that works:

Comments address a couple of details from your first post:

	check(GetWorld());
	const TArray<TEnumAsByte<EObjectTypeQuery>> ObjType = {
		UEngineTypes::ConvertToObjectType(ECollisionChannel::ECC_Pawn)
	};
	// Use GetOwner() if this is in a component. If from pawn use `this`.
	const TArray<AActor*> ActorsToIgnore = { GetOwner() };
	TArray<AActor*> OverlappedActors;

	UKismetSystemLibrary::SphereOverlapActors(
		GetWorld(),
		GetOwner()->GetActorLocation(),
		500.f,
		ObjType,
		AActor::StaticClass() /* or ASpaceMarine::StaticClass() */,
		ActorsToIgnore,
		OverlappedActors
	);

	// Range based loop will not execute body if array is empty.
	for (const auto& Itr : OverlappedActors)
	{
		// Use Itr to do stuff to overlapped actors.		
		UGameplayStatics::ApplyDamage(Itr, 10.0f, /* GetController() */, GetOwner(), UDamageType::StaticClass());
		
		// Debug - Print names of overlapped actors.
		const FString n = Itr->GetName();
		GEngine->AddOnScreenDebugMessage(-1, -1.0f, FColor::Yellow, *n);
	}

If you’re using a custom collision channel than that would be my #1 suspect. My next suspect is that it’s a replication issue, but I cannot help with that.


Hope this helps.

Thank you, changing the collision channel fixed it. Strange because I use that game trace channel for a lot of other damage functions and in blueprints and it works fine.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.