2

I'm trying to write a macro in FoundryVTT v11 that will simply take an item I want from an installed compendium and put it in a character or npc's inventory.

I'm new to FoundryVTT so I'm not sure sure how to make this work. Based on this reddit post I'm supposed to call createEmbeddedDocuments on my actor and pass it an Item object.

Here is the 'Amber' item I'd like to add to a token's inventory: amber item from a compendium pack shown in foundryvtt application

This is what I have so far in my macro:

//Find all actors I have selected (i.e. I am controlling) as the DM
for (let c of canvas.tokens.controlled) {
    if (c.actor.type !== 'character') {
        console.log(c.actor.name, "is not a playable character");
        continue;
    }
//Get token
let characterToken = c.actor;

//Generate a reference to the Amber item
var item = { 
    _id: 'NzDxIISxSXSyQltk',
    id: 'NzDxIISxSXSyQltk',
    name: 'Amber',
    type: 'loot'
};

//Update the token's inventory
await characterToken.createEmbeddedDocuments('Item', [item]);

}

However this gives the character a new item with Amber as its name but no other details. How is this supposed to work exactly?

Disclaimer: I couldn't decide if this should go to stackoverflow.com or here but there's already a foundry-vtt tag in this site so I'm trying here first.

Sal
  • 141
  • 7
  • 1
    Welcome to the stack Sal, this is the part where we usually invite new users to take the [tour], which may not be necessary since you work here. You have found the right site, I imagine there are far more users interested in foundry here that will actually see your question than there are on SO. – Thomas Markov Feb 04 '24 at 20:49
  • 1
    We also have this meta discussion about the on-topicness of foundry and other vtt questions. – Thomas Markov Feb 04 '24 at 21:01

1 Answers1

2

Your issue is that createEmbeddedDocuments doesn't actually perform any kind of lookup. That's not how foundryvtt works. You'll need to get a copy of your item's Object/JSON and then give that to the actor. Which essentially creates a clone of it; it's not linked to the original item in the compendium or anything.

The way to do that would be to pull the item you want from the compendium API and give that to the token:

let pack = game.packs.get("full-compendium-pack-name");
let item = await pack.getDocument("NzDxIISxSXSyQltk");
await characterToken.createEmbeddedDocuments('Item', [item]);

To find your compendium pack name you can open the F12 console in FoundryVTT and run game.packs you should get an array of Maps that contains all the packs you have active:

example list of installed game packs shown after running command

In this case the pack name is: dnd-item-compendium-by-gwill.items-treasure-by-gwill so you would use that in the example above.

Additionally, if you have the ItemPiles module installed you could utilize its ItemPiles.API.addItems API in your macro as it's more flexible. Such as exposing an optional quantity field:

let pack = game.packs.get("dnd-item-compendium-by-gwill.items-treasure-by-gwill");
let item = await pack.getDocument("NzDxIISxSXSyQltk");
await ItemPiles.API.addItems(characterToken, [{"item": item, "quantity": 4}]);
Sal
  • 141
  • 7