feat:上传mcguide-开发指南部份

This commit is contained in:
Othniel su
2024-12-23 10:57:59 +08:00
parent 7292166c88
commit 0dc59fa4f0
3297 changed files with 63375 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
# 网络同步
接下来,我们为您来介绍零件的网络同步的功能以及如果在零件中进行使用
## 功能简介
网络同步功能用于简化零件开发中服务端和客户端数据同步,使用网络同步功能后,当服务端零件中指定的字段进行修改(包括字典和列表中子元素的增加、删除和修改)后,客户端零件中的相关字段也会同步的进行修改。
## 支持的数据结构
网络同步功能支持的数据结构为 Number数字, String字符串 List列表 Tuple元组 Dictionary字典。以及上述数据之间的组合嵌套。
## 使用方法
我们以模板零件NetworkReplicatedPart为例来介绍如何使用网络同步功能。
使用网络同步功能只需要您在初始化时把需要使用进行同步的字段名字加入零件的replicated字段中如下列代码所示
```python
def __init__(self):
super(NetworkReplicatedPart, self).__init__()
self.name = "网络复制测试零件"
self.intProperty = 0
self.intProperty2 = 0
self.strProperty = "0"
self.strProperty2 = "0"
self.listProperty = ["0", "0", "0"]
self.listProperty2 = ["0", "0", "0"]
self.dictProperty = {"key": 0}
self.dictProperty2 = {"key": 0}
self.tupleProperty = ("0", "0", "0")
self.tupleProperty2 = ("0", "0", "0")
self.flexProperty = ("0", "0", {"key": [0]})
self.flexProperty2 = ("0", "0", {"key": [0]})
# 同步intProperty, strProperty, listProperty, dictProperty, tupleProperty, flexProperty
self.replicated = ["intProperty", "strProperty", "listProperty", "dictProperty", "tupleProperty", "flexProperty"]
self.tickIndex = 0
```
在上图的代码中我们为intProperty、strProperty、listProperty、dictProperty、tupleProperty、flexProperty这六种不同类型的字段使用了网络同步功能。
接下来我们在服务端的tick中修改相关字段的值并在客户端的tick中打印出来
```python
def TickClient(self):
self.tickIndex += 1
if self.tickIndex % 150 == 30:
print("TickClient", self)
def TickServer(self):
self.tickIndex += 1
if self.tickIndex % 150 == 0:
self.intProperty += 1
self.intProperty2 += 1
self.strProperty = str(self.intProperty)
self.strProperty2 = str(self.intProperty)
self.listProperty[0] = self.strProperty
self.listProperty2[0] = self.strProperty
self.dictProperty["key"] = self.intProperty
self.dictProperty2["key"] = self.intProperty
self.tupleProperty = tuple(self.listProperty)
self.tupleProperty2 = tuple(self.listProperty)
self.flexProperty[2]["key"].append(self.intProperty)
self.flexProperty2[2]["key"].append(self.intProperty)
print("TickServer", self)
```
使用开发测试进行测试后,脚本调试界面日志如下(为了方便查看,对无用日志进行了省略,调整了日志缩进)
`[17:11:17] ('TickClient', {`
`......`
`"dictProperty": {"key": 2},`
`"dictProperty2": {"key": 0},`
`"flexProperty": ["0","0", {"key": [0, 1, 2]}],`
`"flexProperty2": ["0", "0", {"key": [0]}],`
`"intProperty": 2,`
`"intProperty2": 0,`
`"listProperty": [ "2", "0", "0"],`
`"listProperty2": ["0", "0", "0" ],`
`"name": "NetworkReplicated",`
`"partType": "NetworkReplicatedPart",`
`"replicated": [ "intProperty", "strProperty", "listProperty", "dictProperty", "tupleProperty", "flexProperty"],`
`"strProperty": "2",`
`"strProperty2": "0",`
`"tickEnable": true,`
`"tickIndex": 300,`
`......`
`"tupleProperty": [ "2", "0", "0" ],`
`"tupleProperty2": [ "0", "0", "0"`
`]`
`})`
*注tuple类型在客户端依然是tuple类型打印为tuple原因为打印值为零件结构的json字符串
可见客户端零件中intProperty、strProperty、listProperty、dictProperty、tupleProperty、flexProperty这六个字段的值被服务端零件同步到了客户端中未写入replicated的字段没有进行同步。
## 注意事项
- 1、需要同步的字段需要在replicated字段之前初始化
- 2、字段必须为支持的数据结构否则会无法同步而且报错
- 3、使用网络同步功能会产生额外的性能损耗请根据实际需要进行使用
- 4、客户端同步的数据会有延迟一般为1帧 + 网络传输的时间。