工具
AddRepeatedTimer
服务端开发补充
按指定周期重复执行回调;保存返回的计时器实例,以便停止调度。
客户端开发补充
创建客户端重复执行的定时器。
服务端客户端
服务端接口
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
添加服务端触发的定时器,重复执行
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| delay | float | 延迟时间,单位秒 |
| func | function | 定时器触发函数 |
| *args | any | 变长参数,可以不设置 |
| **kwargs | any | 字典变长参数,可以不设置 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| CallLater | 返回触发的定时器实例,传入func不是函数类型时返回None |
- 示例
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
comp.AddRepeatedTimer(3.0,func,args,kwargs)客户端接口
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
添加客户端触发的定时器,重复执行
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| delay | float | 延迟时间,单位秒 |
| func | function | 定时器触发函数 |
| *args | any | 变长参数,可以不设置 |
| **kwargs | any | 字典变长参数,可以不设置 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| CallLater | 返回触发的定时器实例,传入func不是函数类型时返回None |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
comp.AddRepeatedTimer(3.0,func,args,kwargs)AddTimer
服务端开发补充
延迟执行一次回调;执行条件、状态及对象引用需由调用方维护。
客户端开发补充
创建客户端单次执行的定时器。
服务端客户端
服务端接口
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
添加服务端触发的定时器,非重复
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| delay | float | 延迟时间,单位秒 |
| func | function | 定时器触发函数 |
| *args | any | 变长参数,可以不设置 |
| **kwargs | any | 字典变长参数,可以不设置 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| CallLater | 返回单次触发的定时器实例,传入func不是函数类型时返回None |
- 示例
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
comp.AddTimer(3.0,func,args,kwargs)客户端接口
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
添加客户端触发的定时器,非重复
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| delay | float | 延迟时间,单位秒 |
| func | function | 定时器触发函数 |
| *args | any | 变长参数,可以不设置 |
| **kwargs | any | 字典变长参数,可以不设置 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| CallLater | 返回单次触发的定时器实例,传入func不是函数类型时返回None |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
comp.AddTimer(3.0,func,args,kwargs)CancelTimer
服务端开发补充
使用创建时返回的计时器实例取消后续回调。
- 仅取消指定计时器,不会回滚已发生的业务效果。
关联接口:AddTimer (server) · AddRepeatedTimer (server) · DelServerPlayerEvent (server)
客户端开发补充
取消客户端已创建的定时器。
服务端客户端
服务端接口
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
取消定时器
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| timer | CallLater | AddTimer和AddRepeatedTimer时返回的定时器实例 |
- 返回值
无
- 示例
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
comp.CancelTimer(timer)客户端接口
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
取消定时器
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| timer | CallLater | AddTimer和AddRepeatedTimer时返回的定时器实例 |
- 返回值
无
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
comp.CancelTimer(timer)CheckNameValid
服务端开发补充
检查昵称是否包含不允许的敏感词。
客户端开发补充
检查昵称是否包含不允许的敏感词。
服务端客户端
服务端接口
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
检查昵称是否合法,即不包含敏感词
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| name | str | 昵称 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | True:昵称合法 False:昵称非法 |
- 示例
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
isValid = comp.CheckNameValid("史蒂夫")客户端接口
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
检查昵称是否合法,即不包含敏感词
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| name | str | 昵称 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | True:昵称合法 False:昵称非法 |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
isValid = comp.CheckNameValid("史蒂夫")CheckWordsValid
服务端开发补充
检查语句是否包含不允许的敏感词。
客户端开发补充
检查语句是否包含不允许的敏感词。
服务端客户端
服务端接口
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
检查语句是否合法,即不包含敏感词
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| words | str | 语句 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | True:语句合法 False:语句非法 |
- 示例
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
isValid = comp.CheckWordsValid("creeper? Aww man")客户端接口
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
检查语句是否合法,即不包含敏感词
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| words | str | 语句 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | True:语句合法 False:语句非法 |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
isValid = comp.CheckWordsValid("creeper? Aww man")GetChinese
服务端开发补充
读取语言键对应的中文文本。
客户端开发补充
读取语言键对应的中文文本。
服务端客户端
服务端接口
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
获取langStr对应的中文,可参考PC开发包中\handheld\localization\handheld\data\resource_packs\vanilla\texts\zh_CN.lang
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| langStr | str | 传入的langStr |
- 返回值
| 数据类型 | 说明 |
|---|---|
| str | langStr对应的中文,若找不到对应的中文,则会返回langStr本身,字符编码为Unicode |
- 示例
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
# 获取"entity.wolf.name"对应的中文("狼")
Chinese = comp.GetChinese("entity.wolf.name")客户端接口
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
获取langStr对应的中文,可参考PC开发包中\handheld\localization\handheld\data\resource_packs\vanilla\texts\zh_CN.lang
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| langStr | str | 传入的langStr |
- 返回值
| 数据类型 | 说明 |
|---|---|
| str | langStr对应的中文,若找不到对应的中文,则会返回langStr本身,字符编码为Unicode |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
# 获取"entity.wolf.name"对应的中文("狼")
Chinese = comp.GetChinese("entity.wolf.name")GetClipboardContent
客户端开发补充
读取系统剪贴板文本。
客户端
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
获取系统剪贴板内容
- 参数
无
- 返回值
| 数据类型 | 说明 |
|---|---|
| str | 剪贴板内容,获取失败时返回None |
备注
如果剪贴板里的内容并非文本内容,或者剪贴板里没有内容,则返回空字符串。
示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
# 从其他地方复制一段文本后
# 获取剪贴板内容
content = comp.GetClipboardContent()GetFps
客户端开发补充
读取当前帧率。
客户端
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
获取fps
- 参数
无
- 返回值
| 数据类型 | 说明 |
|---|---|
| float | 当前fps值 |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
fps = comp.GetFps()GetHostPlayerId
服务端开发补充
能力:房主 · 玩家
读取房主的实体标识,与账号标识区分。
客户端开发补充
读取房主的实体标识,与账号标识区分。
服务端客户端
服务端接口
method in mod.server.extraServerApi
- 描述
获取房主的entityId
- 参数
无
- 返回值
| 数据类型 | 说明 |
|---|---|
| str | 房主/服主的entityId,若房主/服主未加入则返回None |
- 示例
import mod.server.extraServerApi as serverApi
serverApi.GetHostPlayerId()客户端接口
method in mod.client.extraClientApi
- 描述
获取房主的entityId
- 参数
无
- 返回值
| 数据类型 | 说明 |
|---|---|
| str | 房主/服主的entityId,若房主/服主未加入则返回None |
- 示例
import mod.client.extraClientApi as clientApi
clientApi.GetHostPlayerId()GetMinecraftEnum
服务端开发补充
取得文档中的引擎枚举定义。
客户端开发补充
取得文档中的引擎枚举定义。
服务端客户端
服务端接口
method in mod.server.extraServerApi
- 描述
用于获取枚举值文档中的枚举值
- 参数
无
- 返回值
| 数据类型 | 说明 |
|---|---|
| minecraftEnum | 枚举集合类 |
- 示例
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreatePlayer(playerId)
# 使用枚举值作为其他接口的参数
comp.SetPlayerGameType(serverApi.GetMinecraftEnum().GameType.Survival)客户端接口
method in mod.client.extraClientApi
- 描述
用于获取枚举值文档中的枚举值
- 参数
无
- 返回值
| 数据类型 | 说明 |
|---|---|
| minecraftEnum | 枚举集合类 |
- 示例
import mod.client.extraClientApi as clientApi
# 使用枚举值与事件参数比较
class BattleUI(ScreenNode):
def OnButtonTouch(self, args):
touchEventEnum = clientApi.GetMinecraftEnum().TouchEvent
if touchEvent == touchEventEnum.TouchUp:
pass
def Create(self):
self.AddTouchEventHandler("/panel/test_btn", self.OnButtonTouch, {"isSwallow":True})GetModConfigJson
服务端开发补充
读取对应端侧资源包或行为包配置目录中的配置文件。
客户端开发补充
读取对应端侧资源包或行为包配置目录中的配置文件。
服务端客户端
服务端接口
method in mod.server.extraServerApi
- 描述
以字典形式返回指定路径的json格式配置文件的内容,文件必须放置在行为包的/modconfigs目录下
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| path | str | 指定路径的json文件 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| dict | 配置内容的字典,当读取文件失败时返回空字典 |
- 示例
import mod.server.extraServerApi as serverApi
data = serverApi.GetModConfigJson("modconfigs/monster.json")客户端接口
method in mod.client.extraClientApi
- 描述
以字典形式返回指定路径的json格式配置文件的内容,文件必须放置在资源包的/modconfigs目录下
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| path | str | 指定路径的json文件 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| dict | 配置内容的字典,当读取文件失败时返回空字典 |
备注
服务端同名接口只能读取行为包的modconfigs目录下文件
示例
import mod.client.extraClientApi as clientApi
data = clientApi.GetModConfigJson("modconfigs/monster.json")GetServerTickTime
服务端开发补充
读取服务端引擎上一帧消耗的时间。
服务端
method in mod.server.extraServerApi
- 描述
获取服务端引擎上一帧的帧消耗时间
- 参数
无
- 返回值
| 数据类型 | 说明 |
|---|---|
| float | 时间,单位为毫秒 |
备注
服务端引擎的帧率为20帧,而服务端脚本的帧率为30帧,因此当脚本每帧获取引擎帧消耗时间时,有可能连续获取到的是同一个引擎帧的时间
示例
import mod.server.extraServerApi as serverApi
print serverApi.GetServerTickTime()ImportModule
服务端开发补充
按字符串路径导入当前已加载模组中的模块。
客户端开发补充
按字符串路径导入当前已加载模组中的模块。
服务端客户端
服务端接口
method in mod.server.extraServerApi
- 描述
使用字符串路径导入模块,作用与importlib.import_module类似,但只能导入当前加载的mod中的模块
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| path | str | 模块的绝对路径 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| any | 模块对象,当前加载的所有mod中不存在该模块则返回None |
- 示例
import mod.server.extraServerApi as serverApi
serverApi.ImportModule("tutorialScripts.tutorialServerSystem")客户端接口
method in mod.client.extraClientApi
- 描述
使用字符串路径导入模块,作用与importlib.import_module类似,但只能导入当前加载的mod中的模块
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| path | str | 模块的绝对路径 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| any | 模块对象,当前加载的所有mod中不存在该模块则返回None |
- 示例
import mod.client.extraClientApi as clientApi
clientApi.ImportModule("tutorialScripts.tutorialClientSystem")SetClipboardContent
客户端开发补充
写入系统剪贴板文本。
客户端
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
设置系统剪贴板内容
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| content | str | 需要设置的剪贴板内容 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | 设置是否成功 |
- 示例
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
# 设置剪贴板内容为"Hello, minecraft"
comp.SetClipboardContent("Hello, minecraft")
# 可以粘贴到其他地方StartCoroutine
服务端开发补充
启动分段执行的协程;不等同于创建并行线程。
客户端开发补充
启动分段执行的协程;不等同于创建并行线程。
服务端客户端
服务端接口
method in mod.server.extraServerApi
- 描述
开启服务端协程,实现函数分段式执行,可用于缓解复杂逻辑计算导致游戏卡顿问题
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| iterOrFunc | generator或callable([],generator) | 传入带有yield的函数或传入生成器。如传入生成器则将从生成器中断位置开始执行,如传入函数将从头开始执行 |
| callback | function | 协程执行完后的回调函数,默认为None |
- 返回值
| 数据类型 | 说明 |
|---|---|
| generator | 函数生成器。如果传入的iterOrFunc不是带有yield的函数或者生成器时返回None |
- 示例
#通过生成器执行协程
import server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(serverApi.GetLevelId())
def callback():
print "callback"
def coroutineTest():
for i in xrange(1000):
print i
yield
generator = serverApi.StartCoroutine(coroutineTest, callback)
#执行1秒后停止协程
comp.AddTimer(1.0, serverApi.StopCoroutine, generator)
#5秒后传入StartCoroutine返回的生成器,则函数将从停止位置继续执行
comp.AddTimer(5.0, serverApi.StartCoroutine, generator, callback)
#----------------------------------------------------------------
#通过函数执行协程
import server.extraServerApi as serverApi
def callback():
print "callback"
def coroutineTest():
for i in xrange(1000):
print i
yield
#传入函数,函数将从头开始执行
serverApi.StartCoroutine(coroutineTest, callback)客户端接口
method in mod.client.extraClientApi
- 描述
开启客户端协程,实现函数分段式执行,可用于缓解复杂逻辑计算导致游戏卡顿问题
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| iterOrFunc | generator或callable([],generator) | 传入带有yield的函数或传入生成器。如传入生成器则将从生成器中断位置开始执行,如传入函数将从头开始执行 |
| callback | function | 协程执行完后的回调函数,默认为None |
- 返回值
| 数据类型 | 说明 |
|---|---|
| generator | 函数生成器。如果传入的iterOrFunc不是带有yield的函数或者生成器时返回None |
- 示例
#通过生成器执行协程
import client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(clientApi.GetLevelId())
def callback():
print "callback"
def coroutineTest():
for i in xrange(1000):
print i
yield
generator = clientApi.StartCoroutine(coroutineTest, callback)
#执行1秒后停止协程
comp.AddTimer(1.0, clientApi.StopCoroutine, generator)
#执行5秒后传入StartCoroutine返回的生成器,则函数将从停止位置继续执行
comp.AddTimer(5.0, clientApi.StartCoroutine, generator, callback)
#----------------------------------------------------------------
#通过函数执行协程
import client.extraClientApi as clientApi
def callback():
print "callback"
def coroutineTest():
for i in xrange(1000):
print i
yield
#传入函数,函数将从头开始执行
clientApi.StartCoroutine(coroutineTest, callback)StopCoroutine
服务端开发补充
停止已启动的协程。
客户端开发补充
停止已启动的协程。
服务端客户端
服务端接口
method in mod.server.extraServerApi
- 描述
停止协程
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| iter | generator | 需要停止的生成器对象,StartCoroutine的返回值 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | 是否成功 |
- 示例
import mod.server.extraServerApi as serverApi
serverApi.StopCoroutine(generator)客户端接口
method in mod.client.extraClientApi
- 描述
停止客户端协程
- 参数
| 参数名 | 数据类型 | 说明 |
|---|---|---|
| iter | generator | 需要停止的生成器对象,StartCoroutine的返回值 |
- 返回值
| 数据类型 | 说明 |
|---|---|
| bool | 是否成功 |
- 示例
import mod.client.extraClientApi as clientApi
clientApi.StopCoroutine(generator)