第一个客户端脚本
在前文中,我们已了解服务端
的开发流程。由于客户端
的开发方式大致相同,因此可以快速上手,进入实战阶段。
注册客户端
回到最初的modMain.py
,注册一份客户端脚本,其参数与服务端一致(使用clientApi
)。
python
@Mod.Binding(name = "TestMod", version = "1.0.0")
class MyMod(object):
@Mod.InitServer()
def serverInit(self):
serverApi.RegisterSystem("test", "ser1", "TestMod.Server.MyServer")
# 添加客户端初始化逻辑,注册我们的客户端脚本
@Mod.InitClient()
def clientInit(self):
# 客户端加载时执行
clientApi.RegisterSystem("test", "cli1", "TestMod.Client.MyClient")
编写客户端脚本
如下文演示代码,客户端的编写方式与服务端大致相同。(文件:BH/TestMod/Client.py
)
python
# -*- coding: utf-8 -*-
import mod.client.extraClientApi as clientApi
ClientSystem = clientApi.GetClientSystemCls() # 获取客户端系统基类
playerId = clientApi.GetLocalPlayerId() # 获取客户端当前玩家ID(客户端独占)
class MyClient(ClientSystem):
def __init__(self, namespace, systemName):
ClientSystem.__init__(self, namespace, systemName)
mcNamespace, mcSystemName = clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName()
self.ListenForEvent(mcNamespace, mcSystemName, "ClientJumpButtonReleaseEvent", self, self.ClientJumpButtonReleaseEvent)
# 监听用户按下跳跃按钮并松手,无需参数
def ClientJumpButtonReleaseEvent(self, _={}):
# 为本地玩家添加一个y轴上的瞬时速度(mc坐标系中y轴是向上)
comp = clientApi.GetEngineCompFactory().CreateActorMotion(playerId)
comp.SetMotion((0, 5, 0))
运行上述代码,玩家在游戏中点击并松手跳跃键将被扔到空中。
引用接口
请自行查找ClientJumpButtonReleaseEvent
事件与SetMotion
接口的功能。
补充
部分API接口可能需要用到levelId,该数据可通过 clientApi.GetLevelId()
获取。
总结
客户端与服务端系统类开发大致相同,并能直接拿到用户相关的数据(如本地玩家id)。