Mineraft基岩版1.16.230.52引入了"Entity properties"這個新功能,可以為Add-On的實體添加屬性(property)。
這麼一來,實體就能夠儲存變數,可以是數字、字串、布林值。
比起過去用行為包的各種組件偽裝成「變數」,再用各種query偵測更方便的多;同樣的,方塊也有類似的properties屬性可以使用。
影片解說
1. 新建屬性#
屬性要在行為包的實體檔案的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
}
2. 存取、修改屬性#
Molang的"query.actor_property"可查詢屬性的值,以及確認有無該屬性的"query.has_actor_property"。
在行為包的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')"
    }
}
3. 別名 (Alias) 的用法#
可以為實體定義別名,用/summon就能夠召喚預先設定好屬性數值的實體。
也可以單純只是別名,而不賦值,例如下面的default_alias。
使用/summon entity:first_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": {}
    }
}
4. 排序 (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": {}
    }
}
5. 實際應用例子#
整篇講下來,讓我們來看看實際例子。我製作的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也沒有寫條件,所以正常生成是普通大小。

若使用/summon指令附加spawn event的指令,在召喚時執行賦值事件
/summon kanmusu:boku ~ ~ ~ boku:enlarge
而用這條召喚
/summon kanmusu:boku ~ ~ ~ boku:shrink
permutation偵測到值的變化,就會自動執行設定好的組件,給予boku指定的大小。
這樣的好處是不用寫那麼多add和remove組件到組件組。
當然我有定義別名,也可以直接用這二條:
/summon boku:first_alias
/summon boku:second_alias
別名自動賦值,再交由permutation進行縮放。
參考資料#
這篇文章參考自Bedrock Dev Wiki。


