Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Trying to automatically create a character with a default token on script install

1636655035
David M.
Pro
API Scripter
To make things easier for users of my new SmartAoE script, I'd like to check for the existence of a default character ("AoEControlToken") and if it doesn't exist I want to create it and automatically set the default token with the desired properties (like setting vision to bypass the z-order issue for sightless tokens). I'd like to eventually add some default abilities to the character sheet as well, but that is outside the scope of this question. The following creates the character if it doesn't exist, but fails to set a default token.&nbsp; Is this a fools errand, or is there something simple that I am missing?&nbsp; Relevant snippet: let charName = 'AoEControlToken' let URL = '<a href="https://s3.amazonaws.com/files.d20.io/images/219939338/Q-RiO5B4NhyaPGwc1YMz9w/max.png?1620037815" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/219939338/Q-RiO5B4NhyaPGwc1YMz9w/max.png?1620037815</a>' //reticle image let controlTokenChars = findObjs({ _type: 'character', name: charName }, {caseInsensitive: true}) || []; if (controlTokenChars.length === 0) { log(`====&gt; From ${scriptName}: The default ${charName} character does not exist! Creating now.`); let charObj = { avatar: URL, name: charName, inplayerjournals: 'all', controlledby: 'all' } let newChar = createObj('character', charObj); if (newChar) { let charID = newChar.get('_id'); let tokObj = { imgsrc: getCleanImgsrc(URL), represents: charID, name: charName, has_bright_light_vision: true, //UDL light_hassight: true, //LDL width: 70, height: 70 } newChar.set('_defaulttoken', JSON.stringify(tokObj)); log('new char created = ' + charID) } }&nbsp;
1636657414

Edited 1636657539
Kurt J.
Pro
API Scripter
There is probably some additional unnecessary stuff in this code, but it *does* work (I think... I don't use dynamic lighting so I can't be sure the right properties are set) :) on('ready',function() { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let charName = 'AoEControlToken' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let scriptName = "TokenTesting" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let URL = '<a href="https://s3.amazonaws.com/files.d20.io/images/219939338/Q-RiO5B4NhyaPGwc1YMz9w/max.png?1620037815" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/219939338/Q-RiO5B4NhyaPGwc1YMz9w/max.png?1620037815</a>'&nbsp; &nbsp; //reticle image &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let URL2 = '<a href="https://s3.amazonaws.com/files.d20.io/images/219939338/Q-RiO5B4NhyaPGwc1YMz9w/thumb.png?1620037815" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/219939338/Q-RiO5B4NhyaPGwc1YMz9w/thumb.png?1620037815</a>'&nbsp; &nbsp; //reticle image &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let controlTokenChars = findObjs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _type: 'character', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: charName &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, {caseInsensitive: true}) || []; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (controlTokenChars.length === 0) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log(`====&gt; From ${scriptName}: The default ${charName} character does not exist! Creating now.`); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log(`Token object : ${tokObj}`); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var charObj = { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; avatar: URL, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: charName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inplayerjournals: 'all', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controlledby: 'all', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log(`Char object: ${charObj}`) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newChar = createObj('character', charObj); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var tokObj = { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgsrc: URL2, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: charName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; has_bright_light_vision: true,&nbsp; //UDL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; light_hassight: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//LDL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 70, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 70, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left: 100, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top: 100, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; layer: "objects", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageid:Campaign().get("playerpageid"), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; represents:newChar.get('_id') &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var theToken = createObj('graphic', tokObj) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (newChar) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let charID = newChar.get('_id'); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setDefaultTokenForCharacter(newChar,theToken); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log('new char created = ' + charID) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theToken.remove(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; }) The _defaulttoken property of a character is read only. There is a function (setDefaultTokenForCharacter) that can be used to set it, but it requires a graphic object. You need a "thumb" image for this, hence the two different URLs. In order to create the graphic, you also need to specify a page and a layer, so this creates it on the player page and deletes it after setting the default token. (for ease of testing, I put it in a wrapper as well) :)
1636660467
David M.
Pro
API Scripter
Thanks Kurt! I had tried the create-then-delete method with playerpageid/objects layer, but looks like the trick was the&nbsp;setDefaultTokenForCharacter function that I didn't realize existed. Re: image - The getCleanImgSrc function in my snippet assigns the thumb (something I stole from theAaron), but I forgot to include the function definition in my post ;) Appreciate it!
1636666521
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Too late to the party, but Door Knocker does exactly this if you needed any code to swipe.
1636668528

Edited 1636668675
David M.
Pro
API Scripter
Thanks, Keith! I got it working using Kurt's example as a guide, but I'll check out DoorKnocker as well.&nbsp; EDIT - looks like it uses the same approach!