---
sidebarDepth: 1
---
# 自定义属性
## GetAttr
服务端客户端
### 服务端接口
method in mod.server.component.modAttrCompServer.ModAttrComponentServer
- 描述
获取SetAttr设置的属性值
- 参数
| 参数名 |
数据类型
| 说明 |
| :--- | :--- | :--- |
| paramName | str | 属性名称,str的名称建议以mod命名为前缀,避免多个mod之间冲突 |
| defaultValue | any | 属性默认值,属性不存在时返回该默认值,此时属性值依然未设置 |
- 返回值
| 数据类型
| 说明 |
| :--- | :--- |
| 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设置的属性值
- 参数
| 参数名 | 数据类型
| 说明 |
| :--- | :--- | :--- |
| paramName | str | 属性名称,str的名称建议以mod命名为前缀,避免多个mod之间冲突 |
| defaultValue | any | 属性默认值,属性不存在时返回该默认值,此时属性值依然未设置 |
- 返回值
| 数据类型
| 说明 |
| :--- | :--- |
| 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
- 描述
注册属性值变换时的回调函数,当属性变化时会调用该函数
- 参数
| 参数名 | 数据类型
| 说明 |
| :--- | :--- | :--- |
| paramName | str | 监听的属性名称 |
| func | function | 监听的回调函数 |
- 返回值
无
- 备注
- 回调函数需要接受一个参数,参数是dict,具体数据示例:{'oldValue': 0, 'newValue': 1, 'entityId': ’-433231231231‘}
- 示例
```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
服务端客户端
### 服务端接口
method in mod.server.component.modAttrCompServer.ModAttrComponentServer
- 描述
设置属性值。服务端设置后会自动同步到客户端,可以用客户端的GetAttr获取。默认不会存档,需要存档的话可以设置needRestore=True。
- 参数
| 参数名 | 数据类型
| 说明 |
| :--- | :--- | :--- |
| paramName | str | 属性名称,str的名称建议以mod命名为前缀,避免多个mod之间冲突 |
| paramValue | any | 属性值,支持python基础数据 |
| needRestore | bool | 该属性是否需要存档,默认为False。 |
| autoSave | bool | 是否需要立刻存档,当needRestore为True时生效。调用接口频繁时,可设置为False时,再另调用[SaveAttr](#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
- 描述
设置客户端属性值
- 参数
| 参数名 | 数据类型
| 说明 |
| :--- | :--- | :--- |
| paramName | str | 属性名称,str的名称建议以mod命名为前缀,避免多个mod之间冲突 |
| paramValue | any | 属性值,支持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
- 描述
反注册属性值变换时的回调函数
- 参数
| 参数名 | 数据类型
| 说明 |
| :--- | :--- | :--- |
| paramName | str | 监听的属性名称 |
| func | function | 监听的回调函数 |
- 返回值
无
- 备注
- 需要传注册时的同一个函数作为参数
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.UnRegisterUpdateFunc('health', self.jumpingText)
```