BenjaFriend
BenjaFriend

Reputation: 664

Server RPC is not called by client for some reason

So I have just started working on a new project in Unreal which is intended to be a simple networked multiplayer game. What I have done so far is make a Weapon class that handles the spawning of projectiles, and give the player character an instance of a weapon on BeginPlay.

The weapon class is simple, but the problem that I am having is that the Server RPC I have for spawning projectiles is not being called from clients, only servers.

Here is how I am calling this RPC:

The Player Character has an OnFire method, that is bound to an input action. This then calls the Fire method on the current weapon that the player has.

// Bind fire event
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &AMultiplayerFPSCharacter::OnFire);

 ...

 void AMultiplayerFPSCharacter::OnFire()
 {
      // Call our weapon's fire method
      if (CurrentWeapon)
      {
          CurrentWeapon->Fire(GetControlRotation());
      }
 }

Now, the weapon's Fire method simply calls the Server RPC to actually handle spawning the projectiles.

 void AWeapon::Fire(const FRotator SpawnRotation)
 {
      UE_LOG(LogTemp, Warning, TEXT("LOCAL Fire"));


      Server_Fire(SpawnRotation);
 }

 void AWeapon::Server_Fire_Implementation(const FRotator SpawnRotation)
 {
    UE_LOG(LogTemp, Warning, TEXT("SERVER RPC: Called RPC"));


    // try and fire a projectile
    if (ProjectileClass != NULL)
    {
        // Spawn Projectile
        ....
    }
 }

I also have the validation method for the Server RPC, which simply returns true for this so I can make sure it actually works.

This implementation is all fine and good on the server or Listen-Server, but when I call it from the client I only get the local Fire method on the Weapon. The client never even calls the server RPC.

One work around that I think I have found is to make the Player Character's OnFire method a Server RPC as well, but this feels like it is not the best way to go about doing this.

Could anyone explain why this is happening?

Upvotes: 0

Views: 5862

Answers (1)

Bas in het Veld
Bas in het Veld

Reputation: 1312

A client can only call a server RPC on a net-owned actor. For example, a client net-owns his PlayerController, so you can add a RPC to that, and call it. If you call it on a server-owned object, like I'm assuming your AWeapon actor is, the log will show something like "can't call RPC on non-owned object".

Upvotes: 1

Related Questions