skip to Main Content

Hi i am trying to create a macro in foundry that looks for a health potion within an selected actor’s inventory, if its found i will do something to expend it as a use, if not found then return an error to foundry. The code i have below is how far i have got but it’s returning the error despite the item being in the actor’s inventory.

Can anyone point me in the correct direction?

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;
    }

From the console i can see the potion in the array but my code isn’t finding it.

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Thanks ooshp,

    I have the answer, i needed to drop the .value and just have item.name, spent hours on this


  2. I haven’t used Foundry in a long time, but it looks like you want this part:

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

    to be this:

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

    Also keep in mind you can log inside a loop if you’re not familiar with what you’re iterating through, to make debugging easier:

    let healthpotion = actor.items.find(item => {
        console.log(item);
        return item.value.name == "Potion of Greater Healing";
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search