开启辅助访问      

站内搜索

搜索
热搜: 下载 1.19 1.20

Minecraft(我的世界)苦力怕论坛

[MOD开发讨论] Add-On实体Entity Properties的用法

发表于 2021-8-11 03:19:14 | 显示全部楼层 |阅读模式 IP:台湾省
本帖最后由 Ivon852 于 2021-8-11 03:27 编辑

这篇文章参考自Bedrock Dev Wiki

Mineraft基岩版1.16.230.52引入了"Entity properties"这个新功能,可以为Add-On的实体添加属性(property)。
这么一来,实体就能够储存变数,可以是数字、字串、布林值。
比起过去用行为包的各种组件伪装成「变数」,再用各种query侦测更方便的多;同样的,方块也有属性可以使用。
新建属性
属性要在行为包的实体档案的description物件进行注册,命名空间一律以property:开头。
在属性中枚举出一系列的数值,然后再进行操作。

{    "format_version": "1.16.0",    "minecraft:entity": {        "description": {            "identifier": "kanmusu:boku",            "is_spawnable": true,            "is_summonable": true,            "is_experimental": false,            "properties": {                //數字範圍的屬性                "property:number_range": {                    "values": {                        "min": 0,                        "max": 100                    },                    "client_sync": true                },                //第二種數字範圍的屬性                "property:number_enum": {                    "values": [                        1,                        2                                    },                //字串陣列的屬性                "property:string_enum": {                    "values": [                        "在水中",                        "在陸地"                                    },                //布林值的屬性                "property:boolean_enum": {                    "values": [                        true,                        false                                    }            }        },        "components": {},        "events": {}    }}

针对每个新增的属性,还可以设定二个值:

default代表的是预设值,如果不填写,则会使用阵列第一个值。

"property:number_range": {    "values": {        "min": 0,        "max": 100    },    "default": 0}

client_sync则是让资源包也能读到这个属性。

"property:number_range": {    "values": {        "min": 0,        "max": 100    },    "client_sync": true}

存取、修改屬性
在行为包的event使用函数: "set_actor_property" 可修改属性的值。

"event:set_entity_property": {    "set_actor_property": {        "property:number_enum": 2,        "property:string_enum": "'在水中'",        "property:boolean_enum": "!query.actor_property('property:boolean_enum')"    }}

別名 (Alias) 的用法
可以为实体定义别名,用/summon就能够召唤预先设定好属性数值的实体。
也可以单纯只是别名,而不赋值,例如下面的default_alias。
目前新增别名,背包会出现多余的生怪蛋。

{    "format_version": "1.16.0",    "minecraft:entity": {        "description": {            "identifier": "kanmusu:boku",            "is_spawnable": true,            "is_summonable": true,            "is_experimental": false,            "properties": {                //字串陣列的屬性                "property:scale": {                    "values": [                        "變大",                        "變小"                                    }            },            "aliases": {                "entity:default_alias": {},                "entity:first_alias": {                    "property:scale": "變大"                },                "entity:second_alias": {                    "property:scale": "變小"                }            }        },        "components": {},        "events": {}    }}

排序 (Permutation) 的用法
permutation是位于minecraft:entity物件内,与components同层级。

{    "format_version": "1.16.0",    "minecraft:entity": {        "description": {},        "permutations": [            {                //條件                "condition": "query.actor_property('property:scale') == '變大'",                "components": {                    //內含的組件                    "minecraft:scale": {                        "value": 10                    }                }            },            {                //條件                "condition": "query.actor_property('property:scale') == '變小'",                "components": {                    //內含的組件                    "minecraft:scale": {                        "value": 0.1                    }                }            }        ],        "components": {},        "events": {}    }}

实际应用例子
整篇讲下来,让我们来看看实际例子。我制作的boku生物程式码如下:
{
    "format_version": "1.16.0",
    "minecraft:entity": {

        "description": {
            "identifier": "kanmusu:boku",
            "is_spawnable": true,
            "is_summonable": true,
            "is_experimental": false,
"properties": {
                //字串陣列的屬性
                "property:scale": {
                    "values": [
                        "正常",
                        "變大",
                        "變小"
                }
            },
  "aliases": {
                //別名1
                "boku:big": {
                    "property:scale": "變大"
                },
                //別名2
                "boku:small": {
                    "property:scale": "變小"
                }
            }
        },
"permutations": [
            {
                //變大的條件
                "condition": "query.actor_property('property:scale') == '變大'",
                "components": {
                    //內含的組件
                    "minecraft:scale": {
                        "value": 10
                    }
                }
            },
            {
                //變小的條件
                "condition": "query.actor_property('property:scale') == '變小'",
                "components": {
                    //內含的組件
                    "minecraft:scale": {
                        "value": 0.1
                    }
                }
            }
        ],
   "components": {
            //預設大小
            "minecraft:scale": {
                "value": 1.0
            }
            //...(其餘組件在此省略)
        },
  "events": {
            "boku:enlarge": {
                "set_actor_property": {
                    "property:scale": "'變大'"
                }
            },
            "boku:shrink": {
                "set_actor_property": {
                    "property:scale": "'變小'"
                }
            }
        }
    }
}


因为属性预设的值是"正常",permutation也没有写条件,所以正常生成是普通大小。
Minecraft 2021_8_11 上午 02_37_06.png

若使用/summon附加spawn event的指令,在召唤时执行赋值事件,这样会变大:
/summon kanmusu:boku ~ ~ ~ boku:enlarge
Minecraft 2021_8_11 上午 02_37_53.png

而用这条召唤,他会变小:
/summon kanmusu:boku ~ ~ ~ boku:shrink
Minecraft 2021_8_11 上午 02_38_21.png

permutation侦测到值的变化,就会自动执行设定好的组件,给予boku指定的大小。
当然我有定义别名,也可以直接用这二条:
/summon boku:second_alias
别名自动赋值,再交由permutation进行缩放。

苦力怕论坛,感谢有您~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

本站
关于我们
联系我们
坛史纲要
官方
哔哩哔哩
技术博客
下载
网易版
安卓版
JAVA
反馈
意见建议
教程中心
更多
捐助本站
QQ群
QQ群

QQ群

访问手机版

访问手机版

手机版|小黑屋|系统状态|klpbbs.com

粤公网安备 44200002445329号 | 由 木韩网络 提供云服务 | GMT+8, 2024-4-25 08:23

声明:本站与Mojang以及微软公司没有从属关系

Powered by Discuz! X3.4 粤ICP备2023071842号