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.
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.
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.
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.