Files
netease-modsdk-wiki/docs/mcdocs/1-ModAPI/接口/实体/自定义属性.md
2025-03-18 14:46:12 +08:00

264 lines
6.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
sidebarDepth: 1
---
# 自定义属性
## GetAttr
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.modAttrCompServer.ModAttrComponentServer
- 描述
获取SetAttr设置的属性值
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| paramName | str | 属性名称str的名称建议以mod命名为前缀避免多个mod之间冲突 |
| defaultValue | any | 属性默认值,属性不存在时返回该默认值,此时属性值依然未设置 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| 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)
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.modAttrCompClient.ModAttrComponentClient
- 描述
获取SetAttr设置的属性值
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| paramName | str | 属性名称str的名称建议以mod命名为前缀避免多个mod之间冲突 |
| defaultValue | any | 属性默认值,属性不存在时返回该默认值,此时属性值依然未设置 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| any | 返回属性值 |
- 备注
- defaultValue不传的时候默认为None
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.GetAttr('health')
```
## RegisterUpdateFunc
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.modAttrCompClient.ModAttrComponentClient
- 描述
注册属性值变换时的回调函数,当属性变化时会调用该函数
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| 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
<span style="display:inline;color:#ff5555">服务端</span>
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
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.modAttrCompServer.ModAttrComponentServer
- 描述
设置属性值。服务端设置后会自动同步到客户端可以用客户端的GetAttr获取。默认不会存档需要存档的话可以设置needRestore=True。
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| 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'})
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.modAttrCompClient.ModAttrComponentClient
- 描述
设置客户端属性值
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| 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
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.modAttrCompClient.ModAttrComponentClient
- 描述
反注册属性值变换时的回调函数
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| paramName | str | 监听的属性名称 |
| func | function | 监听的回调函数 |
- 返回值
- 备注
- 需要传注册时的同一个函数作为参数
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateModAttr(entityId)
comp.UnRegisterUpdateFunc('health', self.jumpingText)
```