【虚幻引擎5 I】RPC的使用

【虚幻引擎5 I】RPC的使用

anzai249 床主

关于RPC

Remote Procedure Calls(远程过程调用),是在本地计算机中调用,在另一台计算机中远程执行的函数。

  • 必须从Actor处调用。
  • 这个Actor必须被复制(replicated)。
  • 如果从服务端调用RPC以在客户端上执行,则只有实际拥有该Actor的客户端才会执行该函数。
  • 如果从客户端调用RPC以在服务端上执行,则客户端必须拥有调用RPC所用的Actor。
  • Multicast是例外:
    • 如果从服务端调用它们,服务端将在其本地执行它们,并在所有当前连接的客户端上执行它们。
    • 如果从客户端调用它们,它们将仅在本地执行,而不会在服务端上执行。
    • 目前我们有一个对此的限制机制:在给定Actor的网络更新周期内,multicast函数不会复制两次以上。(官方想从长远来看对此进行改进)

Multicast指的是从服务器调用,然后在服务器以及所有当前连接的客户端上执行。

在服务端和客户端的调用详细表格。

使用

这是一个开枪特效的例子,原本只会在客户端调用,客户端自己可以看到,其他客户端和服务端都无法看到。

1
2
3
4
// CombatComponent.h
// 使用前
protected:
void FireButtonPressed(bool bPressed);
1
2
3
4
5
6
7
8
9
10
11
12
// CombatComponent.cpp
// 使用前
void UCombatComponent::FireButtonPressed(bool bPressed)
{
bFireButtonPressed = bPressed;
if (EquippedWeapon == nullptr) return;
if (Character && bFireButtonPressed)
{
Character->PlayFireMontage(bAiming);
EquippedWeapon->Fire();
}
}

使用Multicast RPC和Server RPC结合,添加一点点代码。

1
2
3
4
5
6
7
8
9
10
// CombatComponent.h
// 使用后
protected:
void FireButtonPressed(bool bPressed);

UFUNCTION(Server, Reliable)
void ServerFire();

UFUNCTION(NetMulticast, Reliable)
void MulticastFire();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// CombatComponent.cpp
// 使用后
void UCombatComponent::FireButtonPressed(bool bPressed)
{
bFireButtonPressed = bPressed;
if (bFireButtonPressed)
{
ServerFire();
}
}

// 会红,但是别怕,能编译,在服务端上运行会复制到所有客户端。
// 但是在客户端上调用只会在客户端上执行。
void UCombatComponent::MulticastFire_Implementation()
{
if (EquippedWeapon == nullptr) return;
if (Character)
{
Character->PlayFireMontage(bAiming);
EquippedWeapon->Fire();
}
}

// 也会红,只在服务端上运行。
void UCombatComponent::ServerFire_Implementation()
{
MulticastFire();
}

嘿嘿……

  • 标题: 【虚幻引擎5 I】RPC的使用
  • 作者: anzai249
  • 创建于 : 2023-07-05 17:25:18
  • 更新于 : 2025-05-03 14:29:55
  • 链接: https://anzai.sleepingbed.top/archives/posts/bac6a1f4.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
Nickname
Email
Website
0/500
  • OωO
  • |´・ω・)ノ
  • ヾ(≧∇≦*)ゝ
  • (☆ω☆)
  • (╯‵□′)╯︵┴─┴
  •  ̄﹃ ̄
  • (/ω\)
  • ∠( ᐛ 」∠)_
  • (๑•̀ㅁ•́ฅ)
  • →_→
  • ୧(๑•̀⌄•́๑)૭
  • ٩(ˊᗜˋ*)و
  • (ノ°ο°)ノ
  • (´இ皿இ`)
  • ⌇●﹏●⌇
  • (ฅ´ω`ฅ)
  • (╯°A°)╯︵○○○
  • φ( ̄∇ ̄o)
  • ヾ(´・ ・`。)ノ"
  • ( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
  • (ó﹏ò。)
  • Σ(っ °Д °;)っ
  • ( ,,´・ω・)ノ"(´っω・`。)
  • ╮(╯▽╰)╭
  • o(*////▽////*)q
  • >﹏<
  • ( ๑´•ω•) "(ㆆᴗㆆ)
  • 😂
  • 😀
  • 😅
  • 😊
  • 🙂
  • 🙃
  • 😌
  • 😍
  • 😘
  • 😜
  • 😝
  • 😏
  • 😒
  • 🙄
  • 😳
  • 😡
  • 😔
  • 😫
  • 😱
  • 😭
  • 💩
  • 👻
  • 🙌
  • 🖕
  • 👍
  • 👫
  • 👬
  • 👭
  • 🌚
  • 🌝
  • 🙈
  • 💊
  • 😶
  • 🙏
  • 🍦
  • 🍉
  • 😣
  • 颜文字
  • Emoji
  • Bilibili
0 comments
No comment
目录
【虚幻引擎5 I】RPC的使用