尝试从铸造厂vtt内的演员库存中返回找到的物品

问题描述 投票:0回答:1

嗨,我正在尝试在铸造厂中创建一个宏,在选定演员的库存中寻找生命药水,如果找到,我将做一些事情来将其用作用途,如果没有找到,则将错误返回给铸造厂。我下面的代码是我已经走了多远,但它返回错误,尽管该项目位于演员的库存中。

有人能指出我正确的方向吗?

main()

async function main() {

// Is a token selected? If not, error
    console.log("Tokens: ", canvas.tokens.controlled)
    if(canvas.tokens.controlled.length == 0 || canvas.tokens.controlled.length > 1){
        ui.notifications.error("Please select a single token")
        return;
    }
    let actor = canvas.tokens.controlled[0].actor
    console.log(actor)
// Does the token have a health potion?
    let healthpotion = actor.items.find(item => item.value == "Potion of Greater Healing")
console.log(healthpotion)
    if(healthpotion == null || healthpotion == undefined){
        ui.notifications.error("No Health Potions left");
        return;
    }

从控制台我可以看到数组中的药水,但我的代码没有找到它。

enter image description here

javascript arrays macros
1个回答
0
投票

我已经很久没有使用Foundry了,但看起来你想要这个部分:

let healthpotion = actor.items.find(item => item.value == "Potion of Greater Healing")

成为这样:

let healthpotion = actor.items.find(item => item.value.name == "Potion of Greater Healing")

另请记住,如果您不熟悉正在迭代的内容,可以登录循环内部,以使调试更容易:

let healthpotion = actor.items.find(item => {
    console.log(item);
    return item.value.name == "Potion of Greater Healing";
});
© www.soinside.com 2019 - 2024. All rights reserved.