Files
netease-modsdk-wiki/docs/mcdocs/1-ModAPI/接口/实体/自定义属性.md
2025-03-17 13:24:39 +08:00

5.3 KiB
Raw Blame History

sidebarDepth
sidebarDepth
1

自定义属性

GetAttr

服务端客户端

服务端接口

method in mod.server.component.modAttrCompServer.ModAttrComponentServer

  • 描述

    获取属性值

  • 参数

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

    数据类型
    说明
    any 返回属性值
  • 备注

    • defaultValue不传的时候默认为None
  • 示例

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

  • 描述

    获取属性值

  • 参数

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

    数据类型
    说明
    any 返回属性值
  • 备注

    • defaultValue不传的时候默认为None
  • 示例

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

RegisterUpdateFunc

客户端

method in mod.client.component.modAttrCompClient.ModAttrComponentClient

  • 描述

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

  • 参数

    参数名
    数据类型
    说明
    paramName str 监听的属性名称
    func function 监听的回调函数
  • 返回值

  • 备注

    • 回调函数需要接受一个参数参数是dict具体数据示例{'oldValue': 0, 'newValue': 1, 'entityId': -433231231231}
  • 示例

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']

SetAttr

服务端客户端

服务端接口

method in mod.server.component.modAttrCompServer.ModAttrComponentServer

  • 描述

    设置属性值

  • 参数

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

  • 备注

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

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

  • 描述

    设置客户端属性值

  • 参数

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

  • 备注

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

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

UnRegisterUpdateFunc

客户端

method in mod.client.component.modAttrCompClient.ModAttrComponentClient

  • 描述

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

  • 参数

    参数名
    数据类型
    说明
    paramName str 监听的属性名称
    func function 监听的回调函数
  • 返回值

  • 备注

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

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