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

229 lines
5.3 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
- 描述
获取属性值
- 参数
| 参数名 | <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
- 描述
获取属性值
- 参数
| 参数名 | <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']
```
## 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
- 描述
设置属性值
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| paramName | str | 属性名称str的名称建议以mod命名为前缀避免多个mod之间冲突 |
| paramValue | any | 属性值支持python基础数据 |
- 返回值
- 备注
- 注意tuple、set在同步时会转成list。建议优先使用数字和字符串等非集合类型。
- 示例
```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)
```