Download

GetOverlappingActors not working properly in network setting.

I’m attempting to implement a melee system that simply takes the first overlapping actor of a box component and does damage. It work perfectly fine when the host is the one initiating the melee, however when the client does it the for loop for the array of overlapping actors is never entered (so I assume that it is empty and simply not getting the actors). Any idea how to fix this?


void AShooterWeapon::WeaponMelee()
{
	GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("WeaponMelee!!!"));
	
	if (Role < ROLE_Authority)
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("Client: Weapon Melee!!!"));
		ServerWeaponMelee();
	}

	if (Role == ROLE_Authority)
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("SERVER: WEAPON MELEE!!!"));
		ServerWeaponMelee();
	}

	GetWorldTimerManager().SetTimer(this, &AShooterWeapon::StopMeleeNew, TimeBetweenMeleesNew, false);
}

bool AShooterWeapon::ServerWeaponMelee_Validate()
{
	return true;
}

void AShooterWeapon::ServerWeaponMelee_Implementation()
{

	if (Role < ROLE_Authority)
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("WE ARE IN SERVERWEAPONMELEE BUT ARE NOT THE AUTHORITY"));
	}
	
	if (Role == ROLE_Authority)
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("WE ARE IN SERVERWEAPONMELEE AND ARE THE AUTHORITY!!!"));
	}

	// the damagy stuff happens here.

	
	GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("SERVER WEAPON MELEE IMPLEMENTATION!!!"));

	TArray<AActor*> ActorsOverlapping;
	MeleeBoxNew->GetOverlappingActors(ActorsOverlapping);


		for (int32 i = 0; i < ActorsOverlapping.Num(); i++)
		{
			GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Blue, TEXT("WE HAVE ENTERED THE FOR LOOP"));
			//Cast<AShooterCharacter>(Impact.GetActor())->EndZoom();

			if (ActorsOverlapping* == MyPawn)
			{
				GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Blue, TEXT("THIS IS US, GO TO NEXT OVERLAPPING ACTOR"));
				// WE ARE OVERLAPPING SELF, WHAT KINDA MORON WOULD HIT HIMSELF!!!!!
			}
			else if (Cast<AShooterCharacter>(ActorsOverlapping*))
			{
				// its a shootercharacter

				GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Blue, TEXT("Cast is good, we should be doing damage"));

				AShooterCharacter* MeleeTarget = Cast<AShooterCharacter>(ActorsOverlapping*);

				FPointDamageEvent PointDmg;
				PointDmg.DamageTypeClass = MeleeDamageType ;
				PointDmg.ShotDirection = GetShootDir();

				static FName WeaponFireTag = FName(TEXT("WeaponTrace"));
				FCollisionQueryParams TraceParams(WeaponFireTag, true, Instigator);
				TraceParams.bTraceAsyncScene = true;
				TraceParams.bReturnPhysicalMaterial = true;

				const FVector StartTrace = GetActorLocation();
				const FVector EndTrace = MeleeTarget->GetActorLocation();

				GetWorld()->LineTraceSingle(PointDmg.HitInfo, StartTrace, EndTrace, COLLISION_WEAPON, TraceParams);

				PointDmg.Damage = MeleeTarget->CalculateDamageToUse(MeleeDamage, PointDmg, MyPawn->Controller, this, 0.0f, MeleeShieldDamage);

				MeleeTarget->TakeDamage(PointDmg.Damage, PointDmg, MyPawn->Controller, this);

				break;


			}
			else
			{
				// isnt a shooter character, do nothing

				GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Blue, TEXT("IS NOT A SHOOTER CHARACTER SAD FACE"));
			}
		}
	

}

The first problem Is that SeverWeaponMelee(); is probably a ‘Server’ function am I right (check the properties of the UFUNCTION macro you defined it with). If it is, it’ll never run on the clients, instead a client will just ask the server to do it.

The idea is that you invoke the behavior in the ‘WeaponMelee’ function, and all ‘ServerWeaponMelee’ does is simply call that function on the Server for the client. See ShooterGame for an example.

ServerWeaponMelee() should literally just be this:



bool AShooterWeapon::ServerWeaponMelee_Validate()
{
	return true;
}

void AShooterWeapon::ServerWeaponMelee_Implementation()
{
        WeaponMelee();
}


I tried this setup as well and a similar problem occurs. The client hits the melee button and it goes through the string of functions. When it gets to WeaponMelee() it calls the ServerWeaponMelee() (which calls WeaponMelee()). The client’s go through WeaponMelee() draws all the debug strings onto the screen saying it should do damage, but the server’s pass through it says that the overlappingactors array was empty and therefore no damage is dealt.