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不会被调用


### UE4 C++ RepNotify Usage and Issues In Unreal Engine 4 (UE4), `RepNotify` plays a crucial role in networked gameplay by allowing developers to define custom behavior that should occur when a replicated property changes on clients. This feature ensures consistency between server and client states. #### Defining Properties with RepNotify To use `RepNotify`, properties must be marked as both `UPROPERTY(Replicated)` and `UPROPERTY(ReplicatedUsing = "FunctionName")`. The specified function will then get called whenever this property's value changes due to replication from the server to clients[^2]. For example: ```cpp UCLASS() class MYGAME_API AMyActor : public AActor { GENERATED_BODY() public: // Property which triggers NotifyHealthChanged() upon change. UPROPERTY(Replicated, ReplicatedUsing = OnRep_Health) float Health; protected: virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override; private: UFUNCTION() void OnRep_Health(); }; ``` The implementation of `GetLifetimeReplicatedProps` is necessary so that the engine knows about these properties during lifetime management: ```cpp void AMyActor::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME(AMyActor, Health); } ``` When implementing the notification method (`OnRep_Health`): ```cpp void AMyActor::OnRep_Health() { // Code executed only after receiving new health values over the network. // For instance, playing sound effects or updating UI elements based on changed health status. } ``` This setup allows for efficient synchronization while providing hooks into specific moments where updates happen across machines participating in multiplayer sessions. #### Common Issues Encountered With RepNotify One common issue arises when forgetting to implement `GetLifetimeReplicatedProps`. Without properly registering what needs replicating within this function, no data would actually transfer between peers despite having set up everything correctly at first glance. Another frequent problem involves not accounting for potential discrepancies caused by latency; sometimes immediate actions taken locally might conflict with incoming authoritative information coming later through replication channels. Handling such cases gracefully often requires careful design around prediction mechanisms and correction strategies. --related questions-- 1. How does one ensure smooth visual transitions when correcting predicted movements post-replication? 2. What are some best practices for minimizing bandwidth consumption using conditional replications effectively? 3. Can you provide examples demonstrating how to handle out-of-order packet arrivals affecting game state integrity? 4. In what scenarios could leveraging server authority improve overall player experience compared to client-side predictions?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值