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
Hooks.on("preCreateToken", async function (tokenDocument) {
tokenDocument.actor.update({name: "some dude"});
});
Hooks.on("preCreateToken", async function (tokenDocument) {
tokenDocument.actor.update({name: "some dude"});
});
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 hook
Version 11 Token Changes | Foundry Virtual Tabletop
The official website and community for Foundry Virtual Tabletop.
Solution:
so switching the hook to createToken and keeping everything else the same seems to get the functionality i'm looking for.
Jump to solution
12 Replies
Zhell
Zhell•8mo ago
Are you updating the token name, or the actor name?
bluegreymouse
bluegreymouseOP•8mo ago
I'm trying to update the token name. i've also tried
Hooks.on("preCreateToken", async function (tokenDocument) {
tokenDocument.update({name: "some dude"});
});
Hooks.on("preCreateToken", async function (tokenDocument) {
tokenDocument.update({name: "some dude"});
});
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
canvas.tokens.controlled[0].document.update({name: "bla"})
canvas.tokens.controlled[0].document.update({name: "bla"})
Zhell
Zhell•8mo ago
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.
Hooks.on("preCreateToken", function(tokenDocument) {
tokenDocument.updateSource({name: "some dude"});
});
Hooks.on("preCreateToken", function(tokenDocument) {
tokenDocument.updateSource({name: "some dude"});
});
bluegreymouse
bluegreymouseOP•8mo ago
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
Zhell
Zhell•8mo ago
Did you make sure the script actually runs?
bluegreymouse
bluegreymouseOP•8mo ago
No description
bluegreymouse
bluegreymouseOP•8mo ago
here it is on the line with a breakpoint i can step in and through the update.
bluegreymouse
bluegreymouseOP•8mo ago
but the token still ends up on the canvas without that name
No description
Ethaks
Ethaks•8mo ago
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.
bluegreymouse
bluegreymouseOP•8mo ago
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
bluegreymouse
bluegreymouse•8mo ago
so switching the hook to createToken and keeping everything else the same seems to get the functionality i'm looking for.
bluegreymouse
bluegreymouseOP•8mo ago
Thanks for your help!
Want results from more Discord servers?
Add your server