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

First word from attribute

Is there a way to return the first word in a text attribute? Specifically I want my roll template to show just the first name from the 'Name' field on the character sheet.
1600214917
Andreas J.
Forum Champion
Sheet Author
Translator
Create a sheetworker to parse the attribute.
1600243333
GiGs
Pro
Sheet Author
API Scripter
As Andreas says, you'd need to use a sheet worker. Normal macros cant to this. The technique would be to have the sheet worker do the parsing, and create a hidden "firstname" attribute where it gets saved. Then you can send that to you rolltemplate.
Thanks guys. I've now tried this but can't figure out why it isn't working. Can you please help? Here's the sheet worker: <script type="text/worker">     on(sheet:opened", function() {     getAttrs(["character_name"], function(values) {     let fulln = parseInt(values.character_name);     let rolln = fulln.substr(0, codeLine.indexOf(" "));     setAttrs({                                   "roller": rolln     });   }); }); </script> Here's the roll template: <rolltemplate class="sheet-rolltemplate-wod">     <table>         <tr><th>{{roller}}</th></tr>         <tr><th style="font-size:1.5em;">rolls</th></tr>         <tr><th style="color:#ff0000; background-color:#ffffff; opacity:50%;">{{#roll_name}} {{roll_name}}{{/roll_name}}</th></tr>         <td class="result" style="font-size:225%;"><br />{{result}} Successes<br /><br />{{fails}} Failures</td>     </table> </rolltemplate> Everything except roller  is working in the roll template. Where have I gone wrong? Is it that parseInt  is looking for a number rather than text? What would be the text equivalent?
1600256550

Edited 1600259527
GiGs
Pro
Sheet Author
API Scripter
There are a few issues with your sheet worker. Youre missing an open quote at the start of sheet:opened.   The character_name attribute is a string - its text. parseInt is only for numbers (specifically integers) - you use it to extra numbers from strings. Since attribute values are stored as text, you dont need an equivalent to parseInt.  Your substr function references a codeLine variable, but there isnt one. Here's a fixed version of the sheet worker, that also handles if a character sheet name is changed while the sheet is open. It uses a different version to calculate rolln, but substr is fine. I just prefer this way. Also, you might want a function to strip the rolln of special characters like quotes, to avoid weirdness and errors.     on('change:character_name sheet:opened', () => {         getAttrs(['character_name'], v => {             let fulln = v.character_name;             let rolln = fulln.split(' ')[0];                  setAttrs({                 roller: rolln             });         });     });
Thanks, but still no good. I have another sheet worker already in place. Presumably they need to be within separate <script type="text/worker">  sections? When I tried them both in the same one neither worked. Is any CSS required? Where else might the issue be if not in the sheet worker or the roll template? The attr input is simply <input type="text" name="attr_character_name"/>
1600259620

Edited 1600259721
GiGs
Pro
Sheet Author
API Scripter
All sheet workers should be in the same script block. Try giving the character_name input a default value: <input type="text" name="attr_character_name" value=""/> <input type="hidden" name="attr_roller" value="" /> Post your other sheet worker. If it has a syntax error it can break all workers.
The other worker works with the new one now - guess it was the new one's faulty syntax that broke the first one. The default value hasn't helped. Does it matter where the hidden one goes? I just stuck it straight after the character_name one.
Just in case there is a syntax issue, here are both workers together: <script type="text/worker">     const buttonlist = ["personal","relationships","stats","special","gear"];     buttonlist.forEach(button => {         on(`clicked:${button}`, function() {             setAttrs({                 sheetTab: button             });         });     });              on('change:character_name sheet:opened', () => {         getAttrs(['character_name'], v => {             let fulln = v.character_name;             let rolln = fulln.split(' ')[0];                  setAttrs({                 roller: rolln             });         });     }); </script>
1600264240
GiGs
Pro
Sheet Author
API Scripter
Those workers both look fine. Change the hidden input to a visible one temporarily <input type="text" name="attr_roller" value="" /> and see if its displaying the correct value.
It is.
1600267564
GiGs
Pro
Sheet Author
API Scripter
If it's showing the correct value, the problem must be in your rolltemplate or the way you are calling it. What is the text of a typical roll to this template?  Also, check the CSS is set up properly.
Aaaaaand I didn't update the roll button to call on the new attribute. <Many, many face-palms> Sigh, all working now. Thanks again for all your help (and patience!).
1600277341
GiGs
Pro
Sheet Author
API Scripter
hehe, that kind of mistake is one we've all made. Yay!