Skip to main content
Participating Frequently
January 16, 2025
Answered

Help Creating a Script that assigns text based on a total number.

  • January 16, 2025
  • 1 reply
  • 369 views

I created a text field named [unit_section_totaldef] that sums up the values of other text boxes.

I also have 3 fields named:

 

  • [unit_section_classification]
  • [unit_section_baseAP]
  • [unit_section_standard.move]

I would like to fill in each of the 3 fields with different text based on the value of the [unit_section_totaldef]:

 

  • 0 - 75 = Class 1 | 1d8+2 | 5
  • 76 - 150 = Class 2 | 1d8+2 | 4 
  • 151 - 225 = Class 3 | 1d8+3 | 3
  • 226 - 300 = Class 4 | 1d8+4 | 2 
  • 300+ = Class 5 | 1d8+4 | 1

 

I believe it would be something with the collections fields, but the best I could do was if/elseif statements and thats not efficient. 

 

Can someone assist me with this?

Correct answer Nesa Nurani

Use this as validate (or calculate) script in "unit_section_totaldef" field:

var classification = "";
var baseAP = "";
var move = "";
var totalDef = Number(event.value);

if (!isNaN(event.value) && event.value !== "") {
 if (totalDef >= 0 && totalDef <= 75) {
  classification = "Class 1";
  baseAP = "1d8+2";
  move = 5;} 
 else if (totalDef >= 76 && totalDef <= 150) {
  classification = "Class 2";
  baseAP = "1d8+2";
  move = "4";} 
 else if (totalDef >= 151 && totalDef <= 225) {
  classification = "Class 3";
  baseAP = "1d8+3";
  move = "3";} 
 else if (totalDef >= 226 && totalDef <= 300) {
  classification = "Class 4";
  baseAP = "1d8+4";
  move = "2";} 
 else if (totalDef > 300) {
  classification = "Class 5";
  baseAP = "1d8+4";
  move = "1";}}

this.getField("unit_section_classification").value = classification;
this.getField("unit_section_baseAP").value = baseAP;
this.getField("unit_section_standard.move").value = move;

 

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
January 16, 2025

Use this as validate (or calculate) script in "unit_section_totaldef" field:

var classification = "";
var baseAP = "";
var move = "";
var totalDef = Number(event.value);

if (!isNaN(event.value) && event.value !== "") {
 if (totalDef >= 0 && totalDef <= 75) {
  classification = "Class 1";
  baseAP = "1d8+2";
  move = 5;} 
 else if (totalDef >= 76 && totalDef <= 150) {
  classification = "Class 2";
  baseAP = "1d8+2";
  move = "4";} 
 else if (totalDef >= 151 && totalDef <= 225) {
  classification = "Class 3";
  baseAP = "1d8+3";
  move = "3";} 
 else if (totalDef >= 226 && totalDef <= 300) {
  classification = "Class 4";
  baseAP = "1d8+4";
  move = "2";} 
 else if (totalDef > 300) {
  classification = "Class 5";
  baseAP = "1d8+4";
  move = "1";}}

this.getField("unit_section_classification").value = classification;
this.getField("unit_section_baseAP").value = baseAP;
this.getField("unit_section_standard.move").value = move;

 

Participating Frequently
January 16, 2025

That didnt work. 

 Should I use this as well as the calculate formula?

Participating Frequently
January 16, 2025

My bad. I messed up. It works. 

 

Thank you sor much!!!!