您的位置:首页 > 产品设计 > UI/UE

ue4 RepNotify ReplicatedUsing 区别

2016-04-27 12:58 3571 查看
在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不会被调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: