ue4 RepNotify ReplicatedUsing 区别

在ue4种,在服务器修改了某个变量,想触发回调,有两种方式,一个是在蓝图中,一个是在C++中,但这两种用法是有区别的

在蓝图中,变量标记为RepNotify,假设响应函数为OnRep_Fun1,则当变量改变时,OnRep_Fun1会在服务器和所有客户端调用,ue4文档原文如下:

https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/Networking/ReplicateVariable/Blueprints/index.html

This is called whenever the variable we assigned as RepNotify changes and will be executed on both the server and client machines

在C++中,想要达到这种效果,需要用ReplicatedUsing 标记,用法如下:

UPROPERTY(Transient, ReplicatedUsing=OnRep_TeamColor)
int32 TeamNumber;

但注意,在C++中,只有客户端的OnRep_TeamColor函数会被调用,服务器要想调用,需自己手动进行,参考如下:

https://forums.unrealengine.com/archive/index.php/t-3709.html


04-27-2014, 06:37 AM
One key difference between how blueprints and C++ handle the "rep notify" concept is that in blueprints any time a RepNotify variable is set, the OnRep function is automatically called on the server (and on the client when the update happens). In C++, the server does *NOT* automatically get the ReplicatedUsing function called, only the client does when it gets its update. If you want the server to call that function, you have to do so manually. I've had a decent number of requests to show porting the last 2 parts of the blueprint networking tutorial to C++, so I'm hoping I can do that some day soon when I find some time.

Also, whenever you are changing health, you'll want to make sure you're only doing so on the server. If you aren't already, the C++ equivalent of the blueprint macro node for checking authority is:

if (Role == ROLE_Authority)
由于以上原因,尤其是在进程既是服务器又是客户端时,需要自己去调用相关的处理,因为此进程的OnRep_TeamColor不会被调用


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值