KayelGee / token-attacher

MIT License
12 stars 18 forks source link

Macro fails to attach template after updating to 4.1.2 #46

Closed vScourge closed 3 years ago

vScourge commented 3 years ago

I recently updated to Foundry 0.8.8, and Token Attacher 4.1.2. I have a few macros that create spell effect templates, then attach them to the selected actor token. They worked fine prior to the update, now they fail to attach the template to the token. It's likely I'm just missing something.

Here is an example macro:

if ( canvas.tokens.controlled.length != 1 ) {
  ui.notifications.notify("Please select one token.");
} else {
   console.log("using token");
   var token = canvas.tokens.controlled[0];
   var pos_x = canvas.tokens.controlled[0].center.x;
   var pos_y = canvas.tokens.controlled[0].center.y;

   let new_template = await MeasuredTemplate.create({
     t: "circle",
     user: game.user._id,
     owner: game.user._id,
     x: pos_x,
     y: pos_y,
     direction: 0.0,
     angle: 1.0,
     distance: 15,
     borderColor: "#FF0000",
     fillColor: "#FF3366",
     texture: "textures/bats.webm",
     tmfxPreset: "Bloomed Texture",
     tmfxTextureAlpha: 0.45
   });

   if ( token ) {
      console.log("attaching to token");
      console.log(new_template);
      console.log(token);
      await tokenAttacher.attachElementToToken(new_template, token, false);
   }
}

And here's the F12 console spew when I run that macro:

The Document#_id property is deprecated in favor of Document#id or Document#data#_id. Support will be removed in 0.9.0
get _id @ foundry.js:9738
eval @ VM5333:18
callScriptMacroFunction @ Macros.js:170
renderMacro @ Macros.js:188
executeMacro @ Macros.js:209
_onClickMacro @ foundry.js:28059
dispatch @ jquery.min.js:2
v.handle @ jquery.min.js:2
foundry.js:9738 The Document#_id property is deprecated in favor of Document#id or Document#data#_id. Support will be removed in 0.9.0
get _id @ foundry.js:9738
eval @ VM5333:19
callScriptMacroFunction @ Macros.js:170
renderMacro @ Macros.js:188
executeMacro @ Macros.js:209
_onClickMacro @ foundry.js:28059
dispatch @ jquery.min.js:2
v.handle @ jquery.min.js:2
foundry.js:19230 You are calling PlaceableObject.create which has been deprecated in favor of Document.create or Scene#createEmbeddedDocuments. Support will be removed in 0.9.0
create @ foundry.js:19230
eval @ VM5333:16
callScriptMacroFunction @ Macros.js:170
renderMacro @ Macros.js:188
executeMacro @ Macros.js:209
_onClickMacro @ foundry.js:28059
dispatch @ jquery.min.js:2
v.handle @ jquery.min.js:2
backend.mjs:306 Foundry VTT | Created MeasuredTemplate with id [yVkMgOkomHQbWteD] in parent Scene [YrorbfWhEltHTvJ6]
VM5333:33 attaching to token
VM5333:34 [MeasuredTemplateDocument]
VM5333:35 Token5e {_events: i, _eventsCount: 2, tempDisplayObjectParent: null, transform: t, alpha: 1, …}
**Macros.js:212 TypeError: Cannot read property 'constructor' of undefined
    at Object.attachElementToToken (token-attacher.js:920)
    at Macro.eval (eval at callScriptMacroFunction (Macros.js:167), <anonymous>:36:27)
    at async Macro.executeMacro (Macros.js:209)**
executeMacro @ Macros.js:212
async function (async)
executeMacro @ Macros.js:209
_onClickMacro @ foundry.js:28059
dispatch @ jquery.min.js:2
v.handle @ jquery.min.js:2
KayelGee commented 3 years ago

You can see in your log VM5333:34 [MeasuredTemplateDocument]. The [ ] around mean that your create returned an array. attachElementToToken does not accept an array as a parameter.

Either call it like this await tokenAttacher.attachElementToToken(new_template[0], token, false);

or call attachElemtentsToToken await tokenAttacher.attachElementsToToken(new_template, token, false);

vScourge commented 3 years ago

Thank you, I missed that detail. It works now.