实体属性
实体属性(也称为Actor属性)允许在实体服务器端存储数据,而无需使用组件或属性(如minecraft:variant),其功能类似于方块状态。
定义属性
实体属性在行为包实体的description对象中定义:
BP/entities/properties_example.json
json
{
"format_version": "1.21.50",
"minecraft:entity": {
"description": {
"identifier": "wiki:properties_example",
"properties": {
"wiki:bool_property_example": {
"type": "bool",
"default": false
},
"wiki:enum_property_example": {
"type": "enum",
"values": ["first", "second", "third"],
"default": "first"
},
"wiki:float_property_example": {
"type": "float",
"range": [0, 3.14],
"default": 2.5
},
"wiki:int_property_example": {
"type": "int",
"range": [0, 4],
"default": 4
}
}
},
"components": { ... },
"events": { ... }
}
}默认值
通过default参数可以定义默认值,可以是特定值或Molang表达式。
字符串值将被视为Molang表达式(除非是枚举值)。Molang表达式会在实体首次生成时被计算。
minecraft:entity > description > properties
json
"example:enum_property_example": {
"type": "enum",
"values": ["first", "second", "third"],
"default": "math.random_integer(0, 1) ? 'first' : 'second'"
}客户端同步
若要在资源包(客户端)中访问属性,必须将client_sync参数设为true。该值默认为false。
minecraft:entity > description > properties
json
"wiki:is_sad": {
"type": "bool",
"default": false,
"client_sync": true
}让我们设置一个变量来存储该属性值,以便在动画中使用!
minecraft:client_entity > description
json
"scripts": {
"pre_animation": [
"v.is_sad = q.property('wiki:is_sad');"
]
}获取属性值
Molang查询函数
通过property查询函数可获取属性值。
Molang表达式
c
!q.property('wiki:bool_property_example')实体过滤器测试
实体过滤器
json
{
"test": "bool_property",
"domain": "wiki:bool_property_example",
"operator": "==",
"value": true
}命令选择器参数
命令
c
testfor @e[has_property={wiki:enum_property_example="second"}]脚本API
通过Entity.getProperty()方法可获取不同属性的当前值。
脚本
js
customEntity.getProperty("wiki:int_property_example") === 2;设置属性值
实体事件响应
通过实体事件,可以使用set_property事件响应来设置实体属性值:
minecraft:entity > events
json
"wiki:set_entity_property": {
"set_property": {
"wiki:integer_property_example": 2,
"wiki:enum_property_example": "second",
"wiki:boolean_property_example": "!q.property('wiki:bool_property_example')"
}
}脚本API
通过Entity.setProperty()方法可设置属性值。
脚本
js
customEntity.setProperty("wiki:int_property_example", 2);贡献者
编辑 实体属性本页面上的文本和图像内容根据 知识共享署名 4.0 国际许可协议
本页中的代码示例根据 MIT 许可证







