Skip to content

自定义属性 ​

GetAttr ​

服务端自定义属性 属性读取 同步数据开发补充

读取通过自定义属性接口保存的值,与引擎属性区分。

关联接口:SetAttr (server)

客户端自定义属性 属性读取 同步数据开发补充

读取通过自定义属性接口保存的值,与引擎属性区分。

关联接口:SetAttr (client)

服务端客户端

服务端接口 ​

method in mod.server.component.modAttrCompServer.ModAttrComponentServer

  • 描述

获取SetAttr设置的属性值

  • 参数
参数名数据类型说明
paramNamestr属性名称,str的名称建议以mod命名为前缀,避免多个mod之间冲突
defaultValueany属性默认值,属性不存在时返回该默认值,此时属性值依然未设置
  • 返回值
数据类型说明
any返回属性值
  • 备注

  • defaultValue不传的时候默认为None

  • 示例

python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.GetAttr('health')
# 如果直接修改GetAttr出来的集合类型,需要重新调用一遍SetAttr确保有进行更新
testDict = comp.GetAttr('testDict')
testDict['key'] = 'newValue'
comp.SetAttr('testDict', testDict)

客户端接口 ​

method in mod.client.component.modAttrCompClient.ModAttrComponentClient

  • 描述

获取SetAttr设置的属性值

  • 参数
参数名数据类型说明
paramNamestr属性名称,str的名称建议以mod命名为前缀,避免多个mod之间冲突
defaultValueany属性默认值,属性不存在时返回该默认值,此时属性值依然未设置
  • 返回值
数据类型说明
any返回属性值
  • 备注

  • defaultValue不传的时候默认为None

  • 示例

python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.GetAttr('health')

RegisterUpdateFunc ​

客户端自定义属性 变化监听 回调注册开发补充

注册客户端属性变化回调。

客户端

method in mod.client.component.modAttrCompClient.ModAttrComponentClient

  • 描述

注册属性值变换时的回调函数,当属性变化时会调用该函数

  • 参数
参数名数据类型说明
paramNamestr监听的属性名称
funcfunction监听的回调函数
  • 返回值

无

  • 备注

  • 回调函数需要接受一个参数,参数是dict,具体数据示例:

  • 示例

python
import mod.client.extraClientApi as clientApi
# 这个entityId传的是所需要监听的对象的Id
comp = clientApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.RegisterUpdateFunc('health', self.jumpingText)
# 当脚本层的health属性变化时则会调用self.jumpingText
def jumpingText(self, data):
    entityId = data['entityId']
    oldValue = data['oldValue']
    newValue = data['newValue']

SaveAttr ​

服务端自定义属性 属性保存 持久化数据开发补充

保存通过自定义属性接口设置的数据。

服务端

method in mod.server.component.modAttrCompServer.ModAttrComponentServer

  • 描述

保存SetAttr设置的属性值

  • 参数

无

  • 返回值

无

  • 示例
python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateModAttr(entityId)
#SetAttr第三个参数标记属性需要存储,第四个参数设置为不立刻存储
comp.SetAttr('health', 1, True, False)
comp.SetAttr('test', 123, True, False)
#此时调用SaveAttr会将'health'和'test'进行存储
comp.SaveAttr()

SetAttr ​

服务端自定义属性 属性设置 同步数据开发补充

写入自定义属性;同步方向和存档选项以端侧合同为准。

关联接口:GetAttr (server)

客户端自定义属性 属性设置 同步数据开发补充

写入自定义属性;同步方向和存档选项以端侧合同为准。

关联接口:GetAttr (client)

服务端客户端

服务端接口 ​

method in mod.server.component.modAttrCompServer.ModAttrComponentServer

  • 描述

设置属性值。服务端设置后会自动同步到客户端,可以用客户端的GetAttr获取。默认不会存档,需要存档的话可以设置needRestore=True。

  • 参数
参数名数据类型说明
paramNamestr属性名称,str的名称建议以mod命名为前缀,避免多个mod之间冲突
paramValueany属性值,支持python基础数据
needRestorebool该属性是否需要存档,默认为False。
autoSavebool是否需要立刻存档,当needRestore为True时生效。调用接口频繁时,可设置为False时,再另调用SaveAttr进行统一存储,减少开销
  • 返回值

无

  • 备注

  • 注意:tuple、set在同步时会转成list。建议优先使用数字和字符串等非集合类型。

  • 注意:当调用成功且autoSave参数为True时,会将之前所有needRestore标为True的属性做存储。

  • 示例

python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.SetAttr('health', 1)
comp.SetAttr('testDict', {'key':'value'})

客户端接口 ​

method in mod.client.component.modAttrCompClient.ModAttrComponentClient

  • 描述

设置客户端属性值

  • 参数
参数名数据类型说明
paramNamestr属性名称,str的名称建议以mod命名为前缀,避免多个mod之间冲突
paramValueany属性值,支持python基础数据
  • 返回值

无

  • 备注

  • 注意:这里设置了只在本地有效,并不会同步到服务端和其他客户端

  • 示例

python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.SetAttr('health', 1)

UnRegisterUpdateFunc ​

客户端自定义属性 监听取消 回调移除开发补充

取消客户端属性变化回调。

客户端

method in mod.client.component.modAttrCompClient.ModAttrComponentClient

  • 描述

反注册属性值变换时的回调函数

  • 参数
参数名数据类型说明
paramNamestr监听的属性名称
funcfunction监听的回调函数
  • 返回值

无

  • 备注

  • 需要传注册时的同一个函数作为参数

  • 示例

python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.UnRegisterUpdateFunc('health', self.jumpingText)