hook preCreateToken update token name
I'm trying to update the token name when dropping a token on a scene.
I basically have something like this
I'm trying to follow the operation described here
https://foundryvtt.com/article/v11-actor-delta/
but getting an error related to
id
and embeddedCollection
how should I update a token in the preCreateToken
hookVersion 11 Token Changes | Foundry Virtual Tabletop
The official website and community for Foundry Virtual Tabletop.
Solution:Jump to solution
so switching the hook to
createToken
and keeping everything else the same seems to get the functionality i'm looking for.12 Replies
Are you updating the token name, or the actor name?
I'm trying to update the token name.
i've also tried
and every variation in between i can think of 😅
I essentially want this but within the
preCreateToken
hook where canvas.tokens.controlled[0]
is the token being created
OK, so
(1) You cant use an async function inside a "pre" hook like this.
(2) You can't use
update
to change a document before it has been created; you use updateSource
instead.
thanks.
though I just tried this to the same result 😕
I don't get any errors but the token name remains the same as the actor or prototype token
Did you make sure the script actually runs?
here it is on the line with a breakpoint
i can step in and through the update.
but the token still ends up on the canvas without that name
I still spy some
await
in there, so your hook callback is still async
, right?
Foundry does not await hooks, so your callback has to be synchronous. Giving an async
callback results in your function not running before the token creation data is sent to the server.ah okay i see. I'm trying to roll on a rolltable for a random name to apply to a token. is that not something I can do in a hook since it can't contain async calls?
Solution
so switching the hook to
createToken
and keeping everything else the same seems to get the functionality i'm looking for.Thanks for your help!