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

Is it posible to search if an attribute contains a certain word?

So, I am trying to make a sheet for my system, and I have something call "Talents" which you can chose from a list, and it gives you a bonus to a stat. I haven't seen any sheet that searchs for a specific word so I don't even know if it's posible. This is what I have tried: <div class="stat-block flex-center"></div> <label>Talents</label> <select name="attr_talselect"> <option value="dexterity">Try 1</option> <option value="strenght">Try 2</option> <option value="faith">Try 3</option> </select> <span name="attr_talselect"></span> </div> <div class="stat-block flex-center"> <label>Bonus</label> <span name="attr_bonus"></span> </div> <script type="text/worker"> //Try on("sheet:opened change:talselect", function() { getAttrs(["talselect"], function(values) { var output3 = {value["talselect"].includes("dexterity")} setAttrs({ "bonus": output3 }); }); }); </script> If someone can point me what's wrong they will have my eternal gratitude (?
1614376999
GiGs
Pro
Sheet Author
API Scripter
The syntax for this line is wrong: var output3 = {value["talselect"].includes("dexterity")} what are you trying to do with this sheet worker? You probably want something like var output3 = value["talselect"].includes("dexterity") ? "whatever it should be if includes dexterity" : "whatever it should be if no dexterity"; Though the values after the ? and : need changing.
1614377346
GiGs
Pro
Sheet Author
API Scripter
If you are trying to get the dex or strength (note: your select has a typo there) or faith value, you need to supply those values in the sheet worker. Something like: on ( 'sheet:opened change:talselect change:strength change:dexterity change:faith' ,  function () {      getAttrs ([ 'talselect' ,  'dexterity' ,  'strength' ,  'faith' ],  function ( values ) {          var   which  =  values . talselect ;          var   bonus  =  parseInt ( values [ which ]) ||  0 ;          setAttrs ({              bonus :   bonus         });     }); }); You supply all the stats needed in getAttrs, so their scores can be accessed with values[statname] You then grab the stat from the select ( which , above), and use the follwoing line to grab that stat value. For this to work, your attributes must exist, and be named exactly as they are defined in the select options.
Thank you for the fast responde. I try copying exaclty what you send me and I still don't get what I want. The thing that I want to do is to set the value of "Bonus" to either "True or False" if the correct word is chosen from "attr_talselect". Sorry if the explanation is a bit confusing, english isn't my main language. This is the edited code: <div class="stat-block flex-center"></div> <label>Talents</label> <select name="attr_talselect"> <option value="dexterity">Try 1</option> <option value="strength">Try 2</option> <option value="faith">Try 3</option> </select> <span name="attr_talselect"></span> </div> <div class="stat-block flex-center"> <label>Bonus</label> <span name="attr_bonus"></span> </div> <script type="text/worker"> //Try on("sheet:opened change:talselect", function() { getAttrs(["talselect"], function(values) { var output3 = value["talselect"].includes("dexterity") ? "true" : "false"; setAttrs({ "bonus": output3 }); }); }); </script>
1614384517

Edited 1614386513
Caden
Forum Champion
Sheet Author
API Scripter
Compendium Curator
If I understand what you're trying to do its this simple.  on(`change:talselect`, eventinfo => updateBonus( eventinfo )) const updateBonus = ({ newValue }) => {         setAttrs({           "bonus": newValue === 'dexterity' ? 'true' : 'false'         }); } Did you need to search if the string contains a word? Based on your example a simple === will suffice. Do you need to do it on sheet open? If you're just trying to set a default you're be better doing: <select name="attr_talselect" value='dexterity'> <option value="dexterity" selected>Try 1</option> <option value="strength">Try 2</option> <option value="faith">Try 3</option> </select>
Thank you, that's kinda what I wanted, but I explained it too shortly in my previous post, what I want to do is to this: Let's say this are my stats: Strenght= 1 Dexterity= 2 Faith= 3 And this are my Talents: Ta1 (gives you +1 strenght) Ta2 (gives you +1 faith) Ta3 (gives you +2 strenght) A player can choose their talents, they can either choose Ta1, Ta2 or Ta3 or any combination of them. How do I make it so the script knows the bonus that the talent gives? Again, sorry, if I am not being clear enough I can explained it further!
1614402821

Edited 1614412710
GiGs
Pro
Sheet Author
API Scripter
If you want players to be able to choose multiple talents, it's best to put them in a repeating section. So the essential elements of the section would look something like this: < label > Talents </ label > < fieldset   class = "repeating_talents" >      < div   class = "talents" >          < select   name = "attr_talent_stat" >              < option   value = "dexterity" > DEX </ option >              < option   value = "strength" > STR </ option >              < option   value = "faith" > FAI </ option >          </ select >          < input   type = "number"   name = "attr_talent_bonus"   value = "1" >          < input   type = "checkbox"   name = "attr_talent_active"   value = "1" checked >      </ div > </ fieldset > You have a select which lets you choose which stat it applies to, a talent bonus, which lets you set the size of the bonus, and a checkbox which lets you click it on and off. Then for each of your base attributes, you have three inputs, a base score, a talent bonus, and a total, something like this < input   type = "number"   name = "attr_dexterity_base"   value = "10"   > < input   type = "number"   name = "attr_dexterity_talent"   value = "0"  readonly > < input   type = "number"   name = "attr_dexterity"   value = "0"   title = "@{dexterity}" readonly > So players enter their score in the base stat, a sheet worker enters the bonus from talents in the _talent box, and another sheet worker adds the base and telent together to give a total. This is simply called dexterity, or faith, or whatever, so players can use @{faith} in macros, without having to use (say) @{faith_total}. It's the total score they always want to use, so might as well name that easily for them. Then add these sheet workers. At the very top is an array to hold the name of your attributes. If you have more attributes, just add their names here - that's the only change you need to make this work. What these workers do: get the repeating section above to work properly. When you select an attribute, set a bonus size, and check the talent, its bonus is applied to the relevant stat_talent attribute, and the appropriate talent is updated. If you change a base attribute, the total is updated automatically. // set up an array to hold the attribute names - add any extra attributes you have here const   stats  = [ 'strength' ,  'dexterity' ,  'faith' ]; // a loop to create a sheet worker for each stat, that adds the base and talent modifier to get the total stats . forEach ( stat   =>  {      on ( `change: ${ stat } _base change: ${ stat } _talent sheet:opened` , ()  =>  {          getAttrs ([ ` ${ stat } _base` , ` ${ stat } _talent` ],  values   =>  {              const   base  = + values [ ` ${ stat } _base` ] ||  0 ;              const   talent  = + values [ ` ${ stat } _talent` ] ||  0 ;              const   total  =  base  +  talent ;              setAttrs ({                  [ stat ]:   total             });         });     }); }); // a worker to check the repeating section and make sure the attributes are correct const   talentStats  =  stats . map ( stat   =>   `change: ${ stat } ` ). join ( ' ' ); on ( `change:repeating_talents:talent_stat change:repeating_talents:talent_bonus change:repeating_talents:talent_active  remove:repeating_talents ${ talentStats }  sheet:opened` , ()  =>  {      // need to check every row of the repeating section      getSectionIDs ( 'repeating_talents' ,  idarray   =>  {          const   fieldnames  = [];          idarray . forEach ( id   =>   fieldnames . push (              `repeating_talents_ ${ id } _talent_stat` ,              `repeating_talents_ ${ id } _talent_bonus` ,              `repeating_talents_ ${ id } _talent_active`         ));          getAttrs ([... fieldnames , ... stats ],  values   =>  {              const   output  = {};              // set the base for each talwnt bonus to 0              stats . forEach ( stat   =>   output [ ` ${ stat } _talent` ] =  0 );                       // now loop through each row and see which stat needs increaseing              idarray . forEach ( id   =>  {                  const   which  =  values [ `repeating_talents_ ${ id } _talent_stat` ];                  const   bonus  = + values [ `repeating_talents_ ${ id } _talent_bonus` ] ||  0 ;                  const   active  = + values [ `repeating_talents_ ${ id } _talent_active` ] ||  0 ;                  if ( active  &&  stats . includes ( which )) {                      output [ ` ${ which } _talent` ] =  output [ ` ${ which } _talent` ] +  bonus ;                 }             });              setAttrs ( output );         });     }); }); You might want to add labels and other details to the stats and repeating sections, but as long as at least the three talent attributes (_stat, _bonus, and _active) and each of the core stats has a _base and _talent attribute, this will work.
Wow.... thank you so much. My mind is completly blown, I don't even understand how you manage to do that so fast, so thank you, you are a lifesaver. The code works perfectly, and I can keep moving foward!! Thank you all for your rapid response and help!
1614413975
GiGs
Pro
Sheet Author
API Scripter
You're very welcome :)
Well as I said it worked perfectly. But I have a question, is it posible to also add stats from items to the question? Let's say I have a hammer that gives +1 STR, is there a way to add it from a different table? GiGs said: You're very welcome :)
1614634420
GiGs
Pro
Sheet Author
API Scripter
If these items are in a different repeating section, it's best to just replicate the method, and create an extra base attribute: you currently have (say) strength_base, strength_talent, and strength (the total). Just as an extra attribute here, strength_item, and have a copy of the above repeating section which applies an items bonus to that attribute. Then you just need to change the worker that adds _base and _talent to also add _item when calculating the total. You can do this as many times as needed, and you could make the _talent and _item attributes hidden if you dont want to display them, and have a separate _modifier attribute in their place to show total values if desired, for display purposes. Would you like me to tweak the above code to add the option to handle extra repeating sections (that have the same structure, containing _stat, _bonus, and _active attributes)?
1614642299

Edited 1614642936
GiGs
Pro
Sheet Author
API Scripter
I went ahead and did it anyway. The code below allows you have any number of repeating sections (say, items, talents, spells, even injuries) that modify any number of base stats, as long they are have specific attributes named in certain ways: Setting Up Stats (the HTML part): Each stat needs a base stat, a total stat, and one stat for each repeating section, like so: < input   type = "number"   name = "attr_dexterity_base"   value = "10"   > < input   type = "number"   name = "attr_dexterity_talent"   value = "0"   readonly > < input   type = "number"   name = "attr_dexterity_item"   value = "0"   readonly > < input   type = "number"   name = "attr_dexterity"   value = "0"   title = "@{dexterity}"   readonly > Note the attributes linked to a repeating section must be named in singular terms (stat_talent, not stat_talents). You can optionally include a modifier stat that totals up the repeating section, and hide the others, like so < input   type = "number"   name = "attr_dexterity_base"   value = "10"   > < input   type = "hidden"   name = "attr_dexterity_talent"   value = "0" > < input   type = "hidden"   name = "attr_dexterity_item"   value = "0" > < input   type = "number"   name = "attr_dexterity_modifier"   value = "0"   readonly > < input   type = "number"   name = "attr_dexterity"   value = "0"   title = "@{dexterity}"   readonly > That way you keep down the stat bloat. If you want to show those hidden stats somewhere on the sheet you can simply move them. Setting up the Repeating Sections Each repeating section needs a name, and it must be plural, like repeating_ talent s, repeating_ item s, etc. Notice the part in bold, that is the sections identifying key.  Each section needs three attributes within it. A select, with options equal to the stat names exactly ('dexterity', 'strength', etc), and the select itself needs to be named key _stat, so talent_stat, or item_stat, etc. A number input, named key _bonus, where the user enters the size of the modifier. This could be a select too, if you wish to limit the range of modifiers. A checkbox, named key _active, where the user can set whether the modifier is active or not. You can have more stats in the section, but those three must be there. The repeating talents section from earlier will work with no modifications. And here's one for items-  notice it is almost identical. < label > Items </ label > < fieldset   class = "repeating_items" >      < div   class = "items" >          < select   name = "attr_item_stat" >              < option   value = "-"   selected > (Choose) </ option >              < option   value = "dexterity" > DEX </ option >              < option   value = "strength" > STR </ option >              < option   value = "faith" > FAI </ option >          </ select >          < input   type = "number"   name = "attr_item_bonus"   value = "1" >          < input   type = "checkbox"   name = "attr_item_active"   value = "1"   checked >      </ div > </ fieldset > Setting up the Sheet Workers The following code will handle any number of stats and any number of repeating sections. The very first three lines are the only ones you need to change. Details below. // user defined functions, set up name of stats, and repeating sections. const   stats  = [ 'strength' ,  'dexterity' ,  'faith' ]; const   sections  = [ 'talent' ,  'item' ]; const   useModifier  =  0 ;      // utility functions and variables const   sectionFieldName  = ( section ,  field ,  id  =  '' )  =>   `repeating_ ${ section } s ${ id  ?  `_ ${ id } _`  :  ':' }${ section } _ ${ field } ` ; const   sectionFields  = [ 'stat' ,  'bonus' ,  'active' ]; const   buildSectionChanges  = ( section ,  fields )  =>   fields . reduce (( a ,  field )  =>   ` ${ a }  change: ${ sectionFieldName ( section , field ) } ` ,  `remove:repeating_ ${ section } s sheet:opened` );           // a loop to create a sheet worker for each stat, that adds the subordinate stats to get the final total for each stat stats . forEach ( stat   =>  {      on ( `change: ${ stat } _base  ${ sections . reduce (( a ,  section )  =>   ` ${ a }  change: ${ stat } _ ${ section } ` ,  '' ) }  sheet:opened` , ()  =>  {          getAttrs ([ ` ${ stat } _base` , ... sections . map ( section   =>   ` ${ stat } _ ${ section } ` )],  values   =>  {              const   stat_values  =  Object . values ( values );              const   output  = {};              output [ stat ] =  stat_values . reduce (( a ,  b )  =>  (+ a  ||  0 ) + (+ b  ||  0 ),  0 );              if ( useModifier ) {                  output [ ` ${ stat } _modifier` ] =  sections . reduce (( a ,  section_name )  =>   a  + (+ values [ ` ${ stat } _ ${ section_name } ` ] ||  0 ),  0 );             }                               setAttrs ( output );         });     }); });      // a worker to check repeating section designed to apply stat modifiers  sections . forEach ( section   =>  {      on ( buildSectionChanges ( section ,  sectionFields ), ()  =>  {          // need to check every row of the repeating section          getSectionIDs ( `repeating_ ${ section } s` ,  idarray   =>  {              const   fieldnames  = [];              idarray . forEach ( id   =>   sectionFields . forEach ( field   =>   fieldnames . push ( sectionFieldName ( section ,  field ,  id ))));                   getAttrs ([... fieldnames ],  values   =>  {                  const   output  = {};                  // set the base for each talwnt bonus to 0                  stats . forEach ( stat   =>   output [ ` ${ stat } _ ${ section } ` ] =  0 );                                   // now loop through each row and see which stat needs increaseing                  idarray . forEach ( id   =>  {                      const   which  =  values [ sectionFieldName ( section ,  'stat' ,  id )];                      const   bonus  = + values [ sectionFieldName ( section ,  'bonus' ,  id )] ||  0 ;                      const   active  = + values [ sectionFieldName ( section ,  'active' ,  id )] ||  0 ;                      if ( active  &&  stats . includes ( which )) {                          output [ ` ${ which } _ ${ section } ` ] =  output [ ` ${ which } _ ${ section } ` ] +  bonus ;                     }                 });                  setAttrs ( output );             });         });     }); }); So the bits you need to change are these lines: const   stats  = [ 'strength' ,  'dexterity' ,  'faith' ]; const   sections  = [ 'talent' ,  'item' ]; const   useModifier  =  0 ; Change the stats array to exactly what your stat names are. Change the sections array to an array of the repeating section keys (notice they are singular not plural). Set useModifier to 1, if you are using a stat_modifier stat, to show the total of all modifiers from repeating sections. And that's it - give it a whirl.
Wow, this is amazing. I'll implement all of it and tell you how I am doing. But again, thank you. You are a lifesaver for a true newbie in sheet making.
Sorry to be of bother again. I was wondering, is there a way to add instead of a "flar number" add the value of another stat. Let's say for example I found a weapon that adds my Faith to the final Strenght score. How could I do that?
1614824378

Edited 1614825425
GiGs
Pro
Sheet Author
API Scripter
It could be done - it's not too hard, but the code would need rewriting a bit.  Is the stat bonus instead of the numeric bonus, or can you get both a stat bonus and a numeric bonus? If you can be both, just add a select with the stats to pick, and the sheet worker can be modifier to get its value too (default would be zero). If it's one or the other, you'd also need some method for the player to choose between them - a checkbox which hides the stat select and shows the number input, and vice versa. The sheet worker code could be basically the same for both approaches. I'll have a look at that tomorrow, once I know which you want to do. Important Questions Also do you add the full value of the stat? What are the stat ranges? Also when adding the stat, do you add the modified total or the base total? Let's say a character has a talent that lets them add Faith. But their Faith has a base score of 3, and they have an item that adds 2 to Faith. So when you pick Faith as an extra bonus, do you add 3 or 5?
Also do you add the full value of the stat? What are the stat ranges? Yes, I want to be able to add the full (total) value of the stat, basically be able to add: for example, add "@{strength}" to the Faith Value. Also when adding the stat, do you add the modified total or the base total? The system doesnt use modifiers so I just want to add the stat total. Let's say a character has a talent that lets them add Faith. But their Faith has a base score of 3, and they have an item that adds 2 to Faith. So when you pick Faith as an extra bonus, do you add 3 or 5? In this case you add 5, you always add the total not the base value.
1614832091

Edited 1614832257
GiGs
Pro
Sheet Author
API Scripter
MaRiTg R. said: The system doesnt use modifiers so I just want to add the stat total. Let's say a character has a talent that lets them add Faith. But their Faith has a base score of 3, and they have an item that adds 2 to Faith. So when you pick Faith as an extra bonus, do you add 3 or 5? In this case you add 5, you always add the total not the base value. I just realised there's a problem here. Lets say you have the following stats: STR: 5 DEX: 3 FAI: 7 You then get a strength item that adds you faith bonus. So you now have: STR: 5, item +FAI(7) = 12. DEX: 3 FAI: 7 You then get a faith item, that adds STR, so you now have STR: 5, item +FAI(7) = 12. DEX: 3 FAI: 7, item +STR(12) = 19 But wait, now your faith is higher so your strength increases STR: 5, item +FAI(19) = 24. DEX: 3 FAI: 7, item +STR(12) = 19 But now you strength is higher STR: 5, item +FAI(19) = 24. DEX: 3 FAI: 7, item +STR(24) = 31 and now your FAI is higher - and so on for ever, until those two attributes pass beyond the limits of your game. How does your game handle this?
Ok it's my fault, I realize that as soon as I said to add a stat value to another stat it would make things confusing. I was a using them just as an example, in reality stats like Strenght or Faith get added to HP, Evasion or staff like that, so you never have a chance to make an infinite loop. GiGs said: MaRiTg R. said: The system doesnt use modifiers so I just want to add the stat total. Let's say a character has a talent that lets them add Faith. But their Faith has a base score of 3, and they have an item that adds 2 to Faith. So when you pick Faith as an extra bonus, do you add 3 or 5? In this case you add 5, you always add the total not the base value. I just realised there's a problem here. Lets say you have the following stats: STR: 5 DEX: 3 FAI: 7 You then get a strength item that adds you faith bonus. So you now have: STR: 5, item +FAI(7) = 12. DEX: 3 FAI: 7 You then get a faith item, that adds STR, so you now have STR: 5, item +FAI(7) = 12. DEX: 3 FAI: 7, item +STR(12) = 19 But wait, now your faith is higher so your strength increases STR: 5, item +FAI(19) = 24. DEX: 3 FAI: 7, item +STR(12) = 19 But now you strength is higher STR: 5, item +FAI(19) = 24. DEX: 3 FAI: 7, item +STR(24) = 31 and now your FAI is higher - and so on for ever, until those two attributes pass beyond the limits of your game. How does your game handle this?
1614834067
GiGs
Pro
Sheet Author
API Scripter
It is possible to do that, but I cant write the code without knowing what combinations are possible - which attributes can receive bonuses, and which can give bonuses.
Don't worry about it. I decided to go with a simpler option of adding a Text Box to the fieldset so if my players need to add a value from (let's say Faith to HP), they can simply type faith and update the number. I hope this is the last time I need ask for your help!! Thank you!
Sorry I lied, one last question I swear. Is it posible to make a dropdown list with buttons?
1614840809
GiGs
Pro
Sheet Author
API Scripter
MaRiTg R. said: Sorry I lied, one last question I swear. Is it posible to make a dropdown list with buttons? Ask as many questions as you need. But I dont know what you mean here. What do you mean by dropdown list with buttons?
Let's say I have something like this: <select id="armas" onchange="armase(this);"> <option value="bow"><button name="roll_bow" type="roll" value="&{template:default} {{name=Bow}} {{Resultado=[[(@{bow}d10!!-5)]]}}">Bow</button></option> <option value="sword"><button name="roll_sword" type="roll" value="&{template:default} {{name=Sword}} {{Resultado=[[(@{sword}d10!!-5)]]}}">Sword</button></option> <option value="spear"><button name="roll_spear" type="roll" value="&{template:default} {{name=Spear}} {{Resultado=[[(@{spear}d10!!-5)]]}}">Spear</button></option> </select> It's a select list in which you can choose a weapon and you get shown a weapon button with which you can use to roll the attack