[英]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. 因此,我刚刚开始在Unreal中进行一个新项目,该项目旨在成为一个简单的网络多人游戏。 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.
到目前为止,我所做的是制作一个Weapon类,用于处理弹丸的生成,并为玩家角色提供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. 武器类很简单,但是我遇到的问题是,我没有从客户端调用服务器生成RPC的服务器RPC,而仅从服务器调用了RPC。
Here is how I am calling this RPC: 这就是我称之为RPC的方式:
The Player Character has an OnFire
method, that is bound to an input action. 玩家角色具有一个
OnFire
方法,该方法绑定到输入操作。 This then calls the Fire
method on the current weapon that the player has. 然后,这将对玩家拥有的当前武器调用
Fire
方法。
// 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. 现在,武器的
Fire
方法只需调用Server RPC即可实际处理生成的弹丸。
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. 我还有Server RPC的验证方法,为此它只返回true,因此我可以确保它确实有效。
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. 这种实现在服务器或Listen-Server上都很好,但是当我从客户端调用它时,只能在Weapon上获得本地
Fire
方法。 The client never even calls the server RPC. 客户端甚至从不调用服务器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. 我想我发现的一种
OnFire
方法是也将Player Character的OnFire
方法也设置为Server RPC,但这似乎并不是实现此目的的最佳方法。
Could anyone explain why this is happening? 谁能解释为什么会这样?
A client can only call a server RPC on a net-owned actor. 客户端只能在网络参与者上调用服务器RPC。 For example, a client net-owns his PlayerController, so you can add a RPC to that, and call it.
例如,客户端拥有自己的PlayerController,因此您可以在其中添加一个RPC并调用它。 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".
如果您在服务器拥有的对象上调用它,例如我假设您的AWeapon actor是,则日志将显示类似“无法在非拥有对象上调用RPC”的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.