--- front: https://nie.res.netease.com/r/pic/20220408/fd58eff7-ab4c-4f98-94b5-87912c6e8e4d.png hard: 入门 time: 15分钟 selection: true --- # 多人联机适配规范 ## 前言 最近,在山头测试中,我们发现很多开发者的模组明明在单人游戏运行是非常正常的,但在山头、多人联机环境下就会出现功能无效或者异常的情况 只要遵循 ServerSystem 与 ClientSystem 严格分离 的原则,大部分兼容性问题都能自然消失。 ## 开发范例 > 下方代码为错误示范。仅供参考和通俗化解释,并不能实际运行! **modClient.py** ```python import mod.client.extraClientApi as clientApi class ModClient(clientApi.GetClientSystemCls()): def Register(self): self.NotifyToServer("test", {}) print("register successfully") # TODO def NoClientApiRegister(self): print("register successfully") # TODO ``` **modServer.py** ```python import mod.client.extraClientApi as serverApi from xxx.xxx.xxx import ModClient #错误引用客户端侧 class ModServer(serverApi.GetServerSystemCls()): def ServerRegister(self): ModClient.NoClientApiRegister() #在多人游戏出现功能无效或者异常 print("register successfully") # TODO ``` 可以看到 上面的 `modClient.py` 引入了 `clientApi` 而 ModServer 这边希望能够调用到 `ModClient` 类里的 `NoClientApiRegister` 方法 虽然这个方法仅仅只是简单的 print,并不涉及 `clientApi` 里的任何方法 但由于 `ModClient` import了 `clientApi`,此时 `ModServer` 在初始化的时候就会报错,类里的方法都不会被正常执行 由上述例子可以得出 在开发模组时,应该避免在 ServerSystem 下的子类引用客户端的接口和方法 如果无法满足要求,可以前往**开发者平台-反馈与建议** 或者 **[Github接口建议](https://github.com/MCNeteaseDevs/SDK-Campaign/issues) 提出接口建议 当你遵循上述开发原则,即客户端和服务端分离,您开发的模组基本天然兼容多人联机和我的山头的环境