`
aigo
  • 浏览: 2635780 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

[UE4]属性同步UPROPERTY replicated:GetLifetimeReplicatedProps和DOREPLIFETIME用法

UE4 
阅读更多

 

注意,DOREPLIFETIME需要头文件: Net/UnrealNetwork.h

 

NETWORK TIPS AND TRICKS

https://www.unrealengine.com/blog/network-tips-and-tricks

 

BASIC PROPERTY REPLICATION

I wanted to talk about a few tips that are good to know about when dealing with replicated properties in native code.

The full explanation of how to replicate a property is somewhat out of the scope of this article, but I can quickly go over the basics here.

To replicate a property you need to do a few things:

In the header of the actor class where the property is defined, you need to make sure you have the ‘replicated’ keyword as one of the parameters to the UPROPERTY declaration:

class ENGINE_API AActor : public UObject
{
	UPROPERTY( replicated )
	AActor * Owner;
};

 

In the implementation of the actor class, you need to implement the GetLifetimeReplicatedProps function:

void AActor::GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const
{
	DOREPLIFETIME( AActor, Owner );
}

 

In the actor’s constructor, make sure you have the bReplicates flag set to true:

AActor::AActor( const class FPostConstructInitializeProperties & PCIP ) : Super( PCIP )
{
	bReplicates = true;
}

 

That’s about it.The member variable ‘Owner’ will now be synchronized to all connected clients for every copy of this actor type that is currently instantiated (in this case, the base actor class).

 

 

CONDITIONAL PROPERTY REPLICATION

Once a property is registered for replication, you can’t unregister it (that’s where the lifetime part comes from). The reason for this is because we bake in as much information as possible, so we can take advantage of sharing work across many connections for the same set of properties. This saves a lot of computation time.

So how does one get more fine grain control over how this property replicates? That’s where conditional properties come in.

By default, each replicated property has a built-in condition, and that is that they don’t replicate if they haven’t changed.

To give you more control over how a property replicates, there is a special macro that allows you to add a secondary condition.

This macro is called DOREPLIFETIME_CONDITION. An example of its usage can be seen below:

void AActor::GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const
{
	DOREPLIFETIME_CONDITION( AActor, ReplicatedMovement, COND_SimulatedOnly );
}

 

The ‘COND_SimulatedOnly’ flag that was passed into the condition macro will cause an extra check to be performed before even considering this property for replication.In this case, it will only replicate to clients that have a simulating copy of this actor.

One of the big benefits of this is that it saves bandwidth; since we’ve determined that client that has the autonomous proxy version of this actor doesn’t need to know about this property (this client is setting this property directly for prediction purposes for example). Another benefit is that for the client not receiving this property, the server won’t step on this client’s local copy.

Here is a quick glance at the list of conditions currently supported:

COND_InitialOnly - This property will only attempt to send on the initial bunch

COND_OwnerOnly - This property will only send to the actor's owner

COND_SkipOwner - This property send to every connection EXCEPT the owner

COND_SimulatedOnly - This property will only send to simulated actors

COND_AutonomousOnly - This property will only send to autonomous actors

COND_SimulatedOrPhysics- This property will send to simulated OR bRepPhysics actors

COND_InitialOrOwner - This property will send on the initial packet, or to the actors owner

COND_Custom - This property has no particular condition, but wants the ability to toggle on/off via SetCustomIsActiveOverride

So far we’ve talked about conditions that are based off of state that is already known. This makes it easy for the engine to make the necessary optimizations while still giving you enough control over property replication.

But what if this isn’t enough control? There is one more thing to talk about on this subject. There is a macro called DOREPLIFETIME_ACTIVE_OVERRIDE, which gives you full control over when a property does and does not replicate, using any custom condition you want. The one caveat is that this is per actor, NOT per connection. So in other words, it’s not safe to use a state that can change per connection in your custom condition. An example can be seen below.

void AActor::PreReplication( IRepChangedPropertyTracker & ChangedPropertyTracker )
{
	DOREPLIFETIME_ACTIVE_OVERRIDE( AActor, ReplicatedMovement, bReplicateMovement );
}

 

The property ReplicatedMovement will now only replicate if bReplicateMovement is true.

Why not use this macro all the time? There are two main reasons to avoid this method:

If custom condition value changes a lot, this can slow things down.

You cannot use condition that can change per connection (don’t check RemoteRole here).

Property replication conditions give a nice balance of control vs. performance. They give the engine the opportunity to optimize the time it takes to check and send the properties for many connections, while still giving the programmer fine grain control over how and when properties replicate.

Now that we’ve given you a few networking tips and tricks, do you have any questions? Be sure to join us over in the forums to continue the discussion!

 

https://forums.unrealengine.com/showthread.php?1690-New-blog-post-Network-Tips-and-Tricks&p=9724#post9724

 

分享到:
评论

相关推荐

    ue4 通过ip连接Replicated server

    在UE4(Unreal Engine 4)中,创建和连接到Replicated服务器是网络多人游戏开发的核心部分。Replicated服务器是一种分布式系统,允许多个玩家通过网络连接到同一个游戏实例,实现同步的游戏体验。以下是关于“ue4...

    ue4 联网配置

    UE4使用基于Actor的网络同步模型,这意味着游戏中的每个对象(Actor)都可以具有网络同步的能力。网络同步是保持所有玩家在同一游戏状态的关键,它负责处理数据在网络中的发送、接收和更新。 1. **网络层**:UE4的...

    深入浅出UE4网络 - Leonhard

    #### UE4中网络高级用法 1. **复制对象引用**:在某些情况下,需要在不同客户端之间同步对象引用。UE4支持这一功能,但需要谨慎使用,以免造成内存泄漏或引用失效等问题。 2. **Role的深层次解读**:在UE4中,每个...

    ue4 分发消息(网络联机 )

    在UE4中,开发者通常使用Actor Component和Replicated Property等机制来实现网络同步。Actor Component是游戏对象中的可复用组件,可以包含可复制的属性,这些属性会在网络上发送,确保所有玩家看到相同的游戏状态。...

    django_replicated:Django DB路由器,用于有状态的主从复制

    将默认的django_replicated设置导入添加到您的settings.py : from django_replicated.settings import *在settings.py中,以标准方式配置您的主数据库和从数据库: DATABASES { 'default': { # ENGINE, HOST, etc....

    UE4_Network_Compendium_by_Cedric_eXi_Neukirchen

    在网络游戏中,实时同步和通信是至关重要的。UE4提供了强大的网络框架,使得开发者能够创建多人在线体验。本指南旨在帮助开发者理解UE4中的网络系统,以便更有效地构建游戏。 **UE4中的网络** 网络在UE4中的核心...

    UE4_Shooter:在UE4上测试游戏

    12. **版本控制**:项目开发通常使用Git或其他版本控制系统,确保团队协作时代码的同步和管理。 通过以上知识点的整合和实践,开发者可以在UE4上成功地构建、测试和优化"UE4_Shooter"这样的游戏项目。理解并掌握...

    ansible-ldap-replicated:主-主复制ldap的ansible部署

    ansible-ldap复制 在Ubuntu 14.04上对主-主复制LDAP进行Ansible部署 描述 这是双重复制,对其中一台... 和 ldap2.internal.tld:389 用户名: 密码:dc密码:example.com 编辑inventory/group_vars/all.yml以更改密码

    UE4-Multiplayer

    UE4提供了几种同步策略,如全状态同步、增量同步和预测性同步。全状态同步适用于简单场景,而复杂游戏可能需要更高效的同步方案。 6. **Client-Side Prediction**:为了减少延迟感,UE4支持客户端预测。这意味着...

    replicated:模板化的配置文件以及yml文件将构建设置屏幕,客户可以在其中选择他们想要设置的配置选项(身份验证,SSL等)

    复制的getELK的配置设置 这些是模板化的配置文件,以及用于构建设置屏幕的yml文件,在该屏幕上,getELK客户可以选择他们想要设置的配置选项(身份验证,SSL等)。 #Detailed文档有关如何为您的配置文件建立模板并...

    Unreal Engine 网络摘要

    - **如何使用复制**:开发者可以通过标记特定变量为“replicated”来启用复制功能。此外,还可以使用`RPCs`(远程过程调用)来同步更复杂的行为。 - **复制属性**:虚幻引擎支持对各种数据类型进行复制,包括基本...

    服务器数据实时同步

    除了上述工具,还有一些专门的数据同步解决方案,如DRBD(Distributed Replicated Block Device)用于Linux环境下的块级数据同步,以及商业的灾备软件如Veeam、Acronis等,它们提供了全面的备份和恢复功能,包括实时...

    Rapid resynchronization for replicated storage

    本文讨论了DRBD(分布式复制块设备)在实时应用程序中使用时,由于主节点意外离开集群(例如崩溃),导致重新同步所需时间过长的问题。DRBD是一个Linux内核模块和一些用户级工具的组合,其目的是实现在线存储复制。...

    Ehcache RMI Replicated Cluster(RMI集群)

    Ehcache RMI Replicated Cluster 是一种分布式缓存解决方案,它使用远程方法调用(RMI)技术在多个节点之间复制数据,以实现高可用性和负载均衡。在大型分布式系统中,缓存是提高应用程序性能的关键组件,因为它可以...

    异步复制与同步复制 以及ehcache的小tips和我的配置

    在IT领域,尤其是在分布式系统和缓存管理中,异步复制和同步复制是两种常见的数据复制策略,而Ehcache是一种广泛使用的Java缓存库。本文将深入探讨这两种复制模式以及Ehcache的相关配置和使用技巧。 首先,我们来...

    9、NIFI综合应用场景-通过NIFI配置kafka的数据同步

    - **PublishKafka_0_10**: 配置这个处理器以连接到Kafka集群,指定brokers的IP和端口,设置主题(topic),并选择合适的消息确认策略(如Guaranteed Replicated Delivery,相当于acks=all,提供最高级别的数据保障)...

    PyPI 官网下载 | flask_replicated-2.0.tar.gz

    标题 "PyPI 官网下载 | flask_replicated-2.0.tar.gz" 指示了这是一个从Python Package Index (PyPI) 下载的压缩包,名为 `flask_replicated-2.0.tar.gz`。这个包是 `flask_replicated` 的一个版本,版本号为2.0。...

    GetStarted:学习虚幻的工作

    - **UCLASS和UPROPERTY**:声明虚幻中的类和属性,实现类继承和属性暴露。 6. **网络同步**: - **Replicated Properties**:在网络多玩家游戏中,你需要标识哪些属性需要在客户端间同步。 - **Actor ...

    Replicated-Softmax-Model:在论文“ Replicated softmax”中重复实验

    dsl.py这是保存和加载数据的方法。 RSM_train_test.py:这是显示如何训练RSM模型,然后测试其困惑性的示例。 RSM_Experiment.py:其中有两个实验。 (1)比较LDA和RSM的困惑(2)比较LDA,RSM和TF-IDF的精度和...

Global site tag (gtag.js) - Google Analytics