Download

Networking question related to RPCs

Hello,
I’m pretty new with the ue4 networking,so I am wondering if there is another way to do a simple communication between a client and a server that should do the same thing(to execute a function on both server and client without considering where is called).Right now i’ve succeeded doing two functions rpc (one for server and another for client(s)) that does the same thing and treat each one when to me called (if the role is server -> call the “clients” rpc otherwise the “server and other clients” rpc).

      header:

UFUNCTION(Server, Reliable, WithValidation)
	 void SpawnFireEffectToServer();
	 virtual void SpawnFireEffectToServer_Implementation();
	 virtual bool SpawnFireEffectToServer_Validate();

	 UFUNCTION(NetMulticast, Reliable)
	 void SpawnFireEffectToClients();
	 virtual void SpawnFireEffectToClients_Implementation();

  .cpp where is called:

        
	if (Role == ROLE_Authority)
		SpawnFireEffectToClients();
	else
		SpawnFireEffectToServer();

   .cpp implementations of rpc functions:

     
      void AWeaponBase::SpawnFireEffectToServer_Implementation() {
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("START FIRE ON SERVER "));
	createFireEffects();
      }

      void AWeaponBase::SpawnFireEffectToClients_Implementation() {
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("START FIRE ON CLIENT "));
	createFireEffects();
       }

       void AWeaponBase::createFireEffects()
      {
	FVector Location = WeaponMesh->GetSocketLocation(MuzzleSocketName);
	FRotator Rotation = WeaponMesh->GetSocketRotation(MuzzleSocketName);
	UGameplayStatics::SpawnEmitterAttached(ShotEffect, WeaponMesh, MuzzleSocketName, Location, Rotation, EAttachLocation::KeepWorldPosition, true);
	UGameplayStatics::PlaySoundAttached(ShotSound, WeaponMesh, MuzzleSocketName, Location, EAttachLocation::KeepWorldPosition, true, 1, 1, 0);
      }

  So in other words I'm not interested where the function is called(on client(s) or server), but i want to see the spawn effects on all clients and server.Also i've tried with replication the vars ShotEffect and ShotSound but it didn't work.
  Any help will be really appreciated.

And what is the issue? This is how networking is done in C++, theres no simpler way of doing it :slight_smile:

First of all thanks for reply.
But there is still a problem, if I have more than one client(server not included), if I shot from a client(call from the client) the other clients can’t see it.(it only works fine for 2 players client-server).
If this is the way how to implement it,what I am doing wrong ?:confused:

FYI the NetMulticast functions can ONLY be called from the server or they will not work correctly (if at all). Do not call it from the client. You could call the run on server RPC from the client and then call the multicast from that implementation.

Yep it works perfectly! :slight_smile: Thank you.

So I’ve made the call


SpawnFireEffectToServer();

made the changes in RPC server implementation


void AWeaponBase::SpawnFireEffectToServer_Implementation() {
if (Role == ROLE_Authority)
SpawnFireEffectToClients();
}



void AWeaponBase::SpawnFireEffectToClients_Implementation() {
	createFireEffects();
}