Files
boybook 760c2dd9ad 2.6
2025-12-01 20:59:16 +08:00

355 lines
8.9 KiB
Markdown
Raw Permalink 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
---
# molang
## EvalMolangExpression
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.queryVariableCompServer.QueryVariableComponentServer
- 描述
在实体上下文上执行molang表达式
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| expression | str | molang表达式 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| dict | 执行结果在"value"的键中,如果有执行错误,通过"error"的键返回错误信息 |
- 备注
- molang最初是为渲染设计的所以在服务端上大部分接口是无法获取到数据的。
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateQueryVariable(entityId)
result = comp.EvalMolangExpression('query.can_fly')
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.queryVariableCompClient.QueryVariableComponentClient
- 描述
在实体上下文上执行molang表达式
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| expression | str | molang表达式 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| dict | 执行结果在"value"的键中,如果有执行错误,通过"error"的键返回错误信息 |
- 备注
- 本接口执行时没有渲染上下文某些molang无法通过该种方式获取到正确的值如query.actor_count【获取最近一帧渲染实体数量】 会永远返回0。
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateQueryVariable(entityId)
result = comp.EvalMolangExpression('query.can_fly')
```
## Get
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.queryVariableCompClient.QueryVariableComponentClient
- 描述
获取某一个实体计算节点的值,如果不存在返回注册时的默认值
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| variableName | str | 节点名称,必须以"query.mod."开头 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| float | 节点的值 |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateQueryVariable(entityId)
result = comp.Get('query.mod.state')
```
## GetAllProperties
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.queryVariableCompServer.QueryVariableComponentServer
- 描述
获取实体属性列表
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| tuple(str) | 所有已注册properties每个str为properties的名称如果有错误返回None |
- 备注
- 可配合query.property使用EvalMolangExpression获取实体属性的值。在客户端只能获取到client_sync为true的属性
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateQueryVariable(entityId)
result = comp.GetAllProperties()
```
## GetMolangValue
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.queryVariableCompClient.QueryVariableComponentClient
- 描述
获取实体molang变量的值
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| molangName | str | molang变量名称如query.can_fly |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| float或long | 节点的值不存在返回None |
- 备注
- 因为没有渲染上下文某些molang无法通过该种方式获取到正确的值如query.is_first_person、variable.is_first_person等。
- 当molangName传入query.get_name或者query.owner_identifier等需要返回字符串的变量值时返回其hash64类型是个long可以用于与GetStringHash64获取的值比较。query.get_name返回生物的名字query.owner_identifier返回其owner的identifier
- 该接口无法进行molang运算例如无法计算query.average_frame_time(20)
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateQueryVariable(entityId)
result = comp.GetMolangValue('query.can_fly')
```
## GetStringHash64
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.queryVariableCompClient.QueryVariableComponentClient
- 描述
返回字符串变量的hash64
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| key | str | 需要计算的字符串变量,如“steve" |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| long | 结果值如果传入不是字符串则返回None |
- 备注
- 可以配合GetMolangValue使用可以获取query.get_name或者query.owner_identifier等返回hash64的是不是等于某个值
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateQueryVariable(levelId)
result = comp.GetStringHash64('steve')
```
## Register
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.queryVariableCompClient.QueryVariableComponentClient
- 描述
注册实体计算节点
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| variableName | str | 节点名称,必须以"query.mod."开头 |
| defalutValue | float | 默认值 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 注册是否成功 |
- 备注
- 注意节点的值使用单精度浮点数存储如果用来设置整数那么值大于16777216或小于-16777216时就会有误差
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateQueryVariable(levelId)
result = comp.Register('query.mod.state', 0.0)
```
## Set
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.queryVariableCompClient.QueryVariableComponentClient
- 描述
设置某一个实体计算节点的值
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| variableName | str | 节点名称,必须以"query.mod."开头 |
| value | float | 计算节点的值 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置是否成功 |
- 备注
- 注意节点的值使用单精度浮点数存储如果用来设置整数那么值大于16777216或小于-16777216时就会有误差
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateQueryVariable(entityId)
result = comp.Set('query.mod.state', 1.0)
```
## SetPropertyValue
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.queryVariableCompServer.QueryVariableComponentServer
- 描述
设置实体属性的值
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| propertyName | str | 属性名如minecraft:has_nectar |
| value | str/int/float/bool | 属性值支持int/float/bool/enum(str)支持molang表达式 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否执行成功 |
- 备注
- molang表达式需无副作用 例如不能设置molang变量的值例如"variable.moo = 1;return variable.moo;"。query仅支持query.has_property、query.property
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateQueryVariable(entityId)
result = comp.SetPropertyValue('minecraft:has_nectar', True)
```
## UnRegister
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.queryVariableCompClient.QueryVariableComponentClient
- 描述
注销实体计算节点
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| variableName | str | 节点名称,必须以"query.mod."开头 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 注销是否成功 |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateQueryVariable(levelId)
result = comp.UnRegister('query.mod.state')
```