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.
Here is how I spawn the weapon on the player inside of BeginPlay:
if (StartWeapon != NULL)
{
UWorld* const World = GetWorld();
if (World)
{
// Set the spawn paramaters
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
// Spawn the pickup
CurrentWeapon =
World->SpawnActor<AWeapon>(StartWeapon, SpawnParams);
}
}
// Attatch the weapon to the grip point on the player
if (CurrentWeapon != nullptr && Mesh1P != nullptr)
{
if (CurrentWeapon->GetMesh() != nullptr)
{
const FName GripSocketName = FName(TEXT("GripPoint"));
CurrentWeapon->GetMesh()->AttachToComponent(
Mesh1P,
FAttachmentTransformRules::SnapToTargetNotIncludingScale,
GripSocketName);
UE_LOG(LogTemp, Warning, TEXT("Setup the Current Weapon!!"));
}
}
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?