C++ Emitter won't multicast

I setup a multicast that is executed when the player makes a LineTrace on the server, So that the weapon appears to make an effect. However even though the multicast is executed and the correct information is sent only the player that runs the fire command is seeing the effect. I can’t seem to get the effect to play on everyones client.

Here is the code for the multicast:

void ANewProjectCharacter::FireWeaponEffects_Implementation(const FVector& StartPoint, const FVector& EndPoint, const FVector& HitLoc) {
	DrawDebugLine(GetWorld(), StartPoint, EndPoint, FColor(255, 0, 0), true, 3.0f);
	DrawDebugLine(GetWorld(), HitLoc, EndPoint, FColor(0, 255, 0), true, 3.0f);
	UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, HitLoc, FRotator(0, 0, 0), false);
	GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Yellow, FString::SanitizeFloat(HitLoc.X) + FString::FString(" ") + FString::SanitizeFloat(HitLoc.Y) + FString::FString(" ") + FString::SanitizeFloat(HitLoc.Z));

}

Here is the .h multicast:

	UFUNCTION(NetMulticast, Reliable)
	void FireWeaponEffects(const FVector& StartPoint, const FVector& EndPoint, const FVector& HitLoc);

Are you calling FireWeaponEffects from the server? Multicast will only work if called by the server.

Yea I am, here is the code for the server function.

.h Code

	UFUNCTION(Server, Reliable, WithValidation)
	void FireWeaponOnServer();

.cpp code

void ANewProjectCharacter::FireWeaponOnServer_Implementation() {
	//
	TArray<FHitResult> hitresults;
	FVector StartTrace = ThirdPersonCamera->GetComponentLocation();
	FVector ForwardVector = ThirdPersonCamera->GetForwardVector();
	FVector EndTrace = (ForwardVector * 3500.0f) + StartTrace;
	//FCollisionObjectQueryParams ObjectQueryParams;
	//FCollisionQueryParams QueryParams("Tracking", true, this);
	GetWorld()->LineTraceMultiByChannel(hitresults, StartTrace, EndTrace, ECC_WorldStatic);
	for (FHitResult hit : hitresults)
	{
			FString NameOfObject = hit.GetActor()->GetName();
			FVector ImpactPoint = hit.ImpactPoint;
			//hit.GetActor()->Destroy(); Just testing if the code is able to destroy stuff
			//OnRep_WeaponEffects(ImpactPoint); testing stuff
			FireWeaponEffects_Implementation(StartTrace, EndTrace, ImpactPoint);
	}
}

I also have the validation setup with a return true; line of code just to make sure the code is validated.

I have tried setting up another function that calls the server one from the client and without it both do not seem to want to sync up for some reason. As you can tell from what I have posted I am trying to setup a basic fire system. Which works but only for the player firing the other player cannot see the effects. I have experience using blueprints and have been able to setup a game with a full deathmatch and respawning system perfectly fine however I wanted to expand my project using C++ instead which is where I have come across this issue.

I don’t actually know if this is a issue, it’s just something I have never done before:

Calling a function like this:

FireWeaponEffects_Implementation(StartTrace, EndTrace, ImpactPoint);

Because of how the server/client functions work (among others), you don’t have to have the _Implementation when you calling it. You can just do:

FireWeaponEffects(StartTrace, EndTrace, ImpactPoint);

If having the _Implementation is OK, I would look into make sure that the StartTrace, EndTrace, and ImpactPoint are “valid”, in the sense of non FVector::ZeroVector.

Ill try what you recommended but isn’t anything to do with the server doesn’t it need the implementation as with all of the other examples on the internet it shows that they use the implementation?

Ill quickly try it now.

Update on the test.

So from my test it shows that if the player is the server the effects now are correctly working however the client is able to shoot and see the effect but the server cannot see the effect still and neither the debug lines from the client. Which is extremely strange as the events are clearly going straight to the server to register the line trace to prevent cheating and then simply running the multicast that is clearly working as the client can see it playing the effects. The code is also compiling perfectly fine with zero errors and I am using VAX as well which should help pick up my errors yet it is saying nothing is wrong with the code.

Another thing I just noticed when testing it once more is that it appears the multicast does not multicast for the client. As the vector debug should appear twice if the multicast works as it is sending out the value from both the server and the client. When the client fires the value is only sent out once. So I am guessing it is to do with how the multicast is working, is this a bug or am I missing something?

So I was testing what you said and it was clear that the clients server code was not working correctly as when the dedicated server was introduced along with the multicast server check it stopped my clients from being able to fire.

However thanks to your answer earlier I did some looking over my code and the post you made earlier helped me solve it. As soon as I removed the _Implementation from the execution the multicast at least started working correctly for the server. I done the same thing on the execute on client FireWeaponOnServer and now every type of player including listen,dedicated or client now receives the correct vector data and the effect appears for everyone :smiley:

So essentially the thing to learn from this is to make sure you do not add _Implementation to any of your function executes as it will break how Unreal engine interprets the function. I am guessing it skips the start up sequence of checking that it is run on the server.

If you could give a brief write up of what I did wrong Ill mark it as the answer as without your help I honestly would not of been able to solve this. Also I would recommend that someone at Epic clearly states that you should never do what I did on the Documentation as it is extremely confusing for someone like me who is moving from blueprints to C++.

All I can say is I am glad that it is working and I can continue to produce my project. Thanks for all the help.

I misunderstood a bit. If you are using a Listen Server instead of a Dedicated Server, you will need to keep a few things in mind.

Multicasts need to always be called from the server, generally checked with (Role == ROLE_Authority).

Multicasts should be called on the server and then executed on the server and all currently connected clients. Ideally, in a situation where you are trying to spawn a particle system, you really should only need to give it the FTransform for the particle system to be created from. If you run things like traces on every client, each will have slightly different results. It’s often easier to have the server run the trace (or whatever) and then send the result through the multicast.

Mutlicasts are widely used, through many projects internally. It is incredible unlikely for a bug like this to be around.

Listen Servers are tricky because they are technically the server but also able to render and take client input, something a Dedicated Server doesn’t do. This means that whenever you have a listen server, you have to keep in mind that there may be instances were you need to treat the listen server like a server or like a client.

I would suggest running a dedicated server at first to make sure the RPC calls are working, then begin working on the flow to make sure it also works when a listen server is on.