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
Solution:
so switching the hook to createToken and keeping everything else the same seems to get the functionality i'm looking for.
Version 11 Token Changes | Foundry Virtual Tabletop
The official website and community for Foundry Virtual Tabletop.
Z
Zhellβ€’45d ago
Are you updating the token name, or the actor name?
B
bluegreymouseβ€’45d 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"})
Z
Zhellβ€’45d 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"});
});
B
bluegreymouseβ€’45d 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
Z
Zhellβ€’45d ago
Did you make sure the script actually runs?
B
bluegreymouseβ€’45d ago
No description
B
bluegreymouseβ€’45d ago
here it is on the line with a breakpoint i can step in and through the update.
B
bluegreymouseβ€’45d ago
but the token still ends up on the canvas without that name
No description
E
Ethaksβ€’45d 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.
B
bluegreymouseβ€’45d 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
B
bluegreymouseβ€’45d ago
so switching the hook to createToken and keeping everything else the same seems to get the functionality i'm looking for.
B
bluegreymouseβ€’45d ago
Thanks for your help!
Want results from more Discord servers?
Add your server
More Posts
empty packSo I'm trying to access a pack in my module and it does exist but it's empty. My thoughts are 1. TError on package installError message ``` Error: Cannot read properties of null (reading 'protected') ``` I've started up Absolutely new to all of this, need help with a silly query.Hello! I'm extremely new to all of this, and i'm having issues trying to understand how to code a waHi, friends! I'd like to build a moduleHi, friends! I'd like to build a module and I could use a shove in the right part of the docs to go Well now I'm downloading lancerWell now I'm downloading lancerNew Entertainer - Fantasy if not Table Top ---- yetMy grandson is 11, and he has started making video shorts. This is his first full length YouTube stBoilerplate.css fileHi I started a new system from the Boilerplate system, and am having an issue seeing updates from myNewbie question on scopeHey there, total newbie here. I'm a complete amateur to programming. I've completed the todo tutoriaHaving trouble to get scripts loadedTrying to make my first module and I have troubles getting anything loaded in Foundry. Here is what Help Needed! Dynamic Table for Reputation TrackingHello, everyone. I am still encountering an issue with my Reputation Tracking system. In the actor Newbie questions on testing.Hi all, ive put together a simple module for PF2E that allows a user to favorite a spell, feat, itemTrouble with Synthetic Actors (unlinked tokens)When I create a token from an actor and try to update something like its hp, I get the following errAvoid Sheet Re-render When Editing Actor's Item**TL;DR** How do I avoid a re-render of a character sheet with actor.items displayed, that happens wHow to Create & Save Custom Field Values from a Dropdown on Actor sheet?I am building a custom system for a Hogwarts setting using the Wands & Wizards 5e supplement. I am cFinding target when making an attackI’m developing a game system, and can’t figure it out, when there is combat active and a player is tTrying to register a new systemHey everyone, I am new to Foundry system development. I am working with the boilerplate system and hMinimise Document using PrototypeQuick question, I've got no idea what I'm doing so please excuse my ineptitude. Say I have a documenNew to System Development but have done minor module developmentI am trying to see if there is a complete working model of a system that I could use a base to undercalculating range/distanceI am trying to calculate the distance between two tokens to make sure they are in range of one anothPartial RerollI am looking for a way to partially "reroll" a roll. Quick example: Player rolls 2d6 (a 2 and a 5) a