Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Difference between revisions of "Order of Operations"

From Roll20 Wiki

Jump to: navigation, search
(create page, move info from dice reference)
 
(confirm order of Step 10 & 11)
 
(8 intermediate revisions by one user not shown)
Line 1: Line 1:
<noinclude>{{NavMacroDocs}}{{stub}}{{main|Complete_Guide_to_Macros_%26_Rolls}}
+
<noinclude>{{revdate}}{{stub}}{{main|Complete Guide to Macros & Rolls}}
  
'''Order of Operations''' here refers to the order in which Roll20's dice engine processes the different dice syntax and [[macros]], which is useful to know when creating more complex macros. Normal [https://www.basic-mathematics.com/order-of-operations.html Math Order of Operations] is processed towards the end of the process.
+
{{NavMacroDocs}}'''Order of Operations''' here refers to the order in which Roll20's dice engine processes the different dice syntax and [[macros]], which is useful to know when creating more complex macros. Normal [https://www.basic-mathematics.com/order-of-operations.html Math Order of Operations] is processed towards the end of the process.
  
</noinclude>While the Roll20 dice engine does support basic math and [[Dice_Reference#Math_Operators_and_Functions|functions]] such as <code>floor()</code> and <code>ceil()</code>, it is first and foremost a dice engine, and so it has its own order of operations. This means that putting parentheses inside of your dice formula will not always affect the outcome of the roll (for example, you can't force a variable to be interpreted before a macro). Here is the Roll20 Order of Operations:
+
</noinclude>While the Roll20 dice engine does support basic math and [[Dice_Reference#Math_Operators_and_Functions|math functions]] such as <code>floor()</code> and <code>ceil()</code>, it is first and foremost a dice engine, and so it has its own order of operations. This means that putting parentheses inside of your dice formula will not always affect the outcome of the roll (for example, you can't force a variable to be interpreted before a macro). Here is the Roll20 Order of Operations:
  
# '''[[Journal#Abilities|Abilities]]''' are expanded (meaning the definition of the ability is placed in the formula anywhere that ability appears; e.g. <code>%{character name|ability_name}</code> becomes <code>/r 1d4</code>).
+
:1. '''[[Character#Abilities|Abilities]]''' are expanded (meaning the definition of the ability is placed in the formula anywhere that ability appears; e.g. <code>%{character name|ability_name}</code> becomes <code>/r 1d4</code>).
# '''[[Macros]]''' are expanded (e.g. <code>#macro-name</code> becomes <code>/r 1d4</code>).
+
:2. '''[[Macros]]''' are expanded (e.g. <code>#macro-name</code> becomes <code>/r 1d4</code>).
# '''[[Complete_Guide_to_Macros_%26_Rolls#Character_Attributes|Attribute calls]]''' are resolved. (e.g. <code>@{attribute_name}</code> becomes <code>4</code>)
+
:3. '''[[Complete_Guide_to_Macros_%26_Rolls#Character_Attributes|Attribute calls]]''' are resolved. (e.g. <code>@{attribute_name}</code> becomes <code>4</code>)
# '''Steps 1-3''' are repeated up to 99 levels deep,''' or there are no longer any expansions required.
+
:4. '''Steps 1-3''' are repeated up to 99 levels deep,''' or there are no longer any expansions required.
# '''[[#Roll_Queries|Roll queries]]''' are executed up to 99 levels deep (the player making the roll is asked to provide a value for each query, and that value is substituted in where the roll query appears in the formula). [[Complete_Guide_to_Macros_%26_Rolls#HTML_replacement|Html entities]] within roll queries are parsed once after each roll query (e.g. <code>&#125;</code> becomes <code>}</code>, but <code>&amp;#125;</code> becomes <code>&#125;</code>)
+
:5. '''[[Roll Query|Roll queries]]''' are executed up to 99 levels deep (the player making the roll is asked to provide a value for each query, and that value is substituted in where the roll query appears in the formula).
# '''[[Inline rolls]]''' are executed, starting with the most deeply nested inline roll working upward. The overall result of the inline roll is substituted in place where it appeared in the formula.
+
::5b. [[HTML Entities]] within roll queries are parsed once after each roll query (e.g. <code>&amp;#125;</code> becomes <code>}</code>, but <code>&amp;amp;#125;</code> becomes <code>&amp;#125;</code>)
# '''The remaining roll is executed''': first, dice are rolled for any dice (e.g. <code>2d6</code> is rolled; including any special dice such as dropped or exploding), then the result of that roll is substituted into the formula.
+
:6. '''[[Inline rolls]]''' are executed, starting with the most deeply nested inline roll working upward. The overall result of the inline roll is substituted in place where it appeared in the formula.<br>
# '''[[Dice_Reference#Math_Operators_and_Functions|Mathematical functions]]''' like <code>floor()</code> and <code>ceil()</code> are executed.
+
:7. '''The remaining roll is executed''': first, dice are rolled for any dice (e.g. <code>2d6</code> is rolled; including any special dice such as dropped or exploding), then the result of that roll is substituted into the formula.<br>
# The '''entire remaining formula is evaluated''', including observing proper [https://www.basic-mathematics.com/order-of-operations.html Math Order of Operations] (parentheses first, then multiplication/division, then addition/subtraction).
+
:8. '''[[Dice_Reference#Math_Operators_and_Functions|Mathematical functions]]''' like <code>floor()</code> and <code>ceil()</code> are executed.<br>
# '''[[Complete_Guide_to_Macros_%26_Rolls#HTML_replacement|Html entities/replacements]]''' are processed once more
+
:9. The '''entire remaining formula is evaluated''', including observing proper [https://www.basic-mathematics.com/order-of-operations.html Math Order of Operations] (parentheses first, then multiplication/division, then addition/subtraction).<br>
# The '''message is sent to {{Text Chat}}''' as well as the [[API:Sandbox Model|API sandbox]].
+
:10. [[Custom Roll Parsing]] happens.<br>
 +
:11. '''[[HTML Entities]]''' are processed once.<br>
 +
:12. The '''message is sent to {{Text Chat}}''' as well as the [[API:Sandbox Model|API sandbox]].
  
  
''Thank you to [[Scott C.]] for additional testing in this matter!''
+
''Thanks goes to [[Scott C.]] & ''Lockbox313'' for additional testing in this matter!''
 
<noinclude>
 
<noinclude>
 +
{| role="presentation" class="wikitable mw-collapsible mw-collapsed"
 +
| Test confirming order of CRP and last HTML Entity processing (Step 10 & 11)
 +
|-
 +
| Have the input be {{c|1d6&#38;#38;}}, and run the following sheet. Output having two ! means CRP happens before the last processing of HTML Entities.
 +
<pre>
 +
<input type="text" name="attr_test2">
 +
<button type="action" name="act_test">test</button>
 +
<rolltemplate class="sheet-rolltemplate-test">
 +
    <div class="sheet-template-container">
 +
        <h1>{{name}}</h1>
 +
        <div class="sheet-results">
 +
            <h4>Custom Result = {{computed::roll1}}</h4>
 +
        </div>
 +
    </div>
 +
</rolltemplate>
 +
<script type="text/worker">
 +
  on(`clicked:test`, function() {
 +
    console.log("button pressed");
 +
          getAttrs(["test2"], function(values) {
 +
              console.log(`values ${JSON.stringify(values)}`);   
 +
              let test_str = values.test2;
 +
            startRoll("&{template:test} {{name=Test}} {{roll1=[[0]]}}", (results) => {
 +
              test_str  =  test_str.replace(/&#33;/g,"!!");
 +
              console.log("test_str: "+test_str);
 +
                finishRoll(
 +
                    results.rollId,
 +
                    {
 +
                        roll1: test_str ,
 +
                    }
 +
                );
 +
            })                   
 +
          });
 +
    });
 +
</script>
 +
<pre>|}
 +
 
==Examples==
 
==Examples==
 
(todo)
 
(todo)
<br>
+
 
<br>
+
* [https://app.roll20.net/forum/permalink/10915703/ Different order of operation in character sheet's ability.] explanation by RainbowEncoder
 +
 
 
[[Category:Macros]]
 
[[Category:Macros]]
 +
[[Category:Character Sheet Creation]]
 
</noinclude>
 
</noinclude>

Latest revision as of 14:01, 24 June 2022

Main Page: Complete Guide to Macros & Rolls

Order of Operations here refers to the order in which Roll20's dice engine processes the different dice syntax and macros, which is useful to know when creating more complex macros. Normal Math Order of Operations is processed towards the end of the process.

While the Roll20 dice engine does support basic math and math functions such as floor() and ceil(), it is first and foremost a dice engine, and so it has its own order of operations. This means that putting parentheses inside of your dice formula will not always affect the outcome of the roll (for example, you can't force a variable to be interpreted before a macro). Here is the Roll20 Order of Operations:

1. Abilities are expanded (meaning the definition of the ability is placed in the formula anywhere that ability appears; e.g. %{character name|ability_name} becomes /r 1d4).
2. Macros are expanded (e.g. #macro-name becomes /r 1d4).
3. Attribute calls are resolved. (e.g. @{attribute_name} becomes 4)
4. Steps 1-3 are repeated up to 99 levels deep, or there are no longer any expansions required.
5. Roll queries are executed up to 99 levels deep (the player making the roll is asked to provide a value for each query, and that value is substituted in where the roll query appears in the formula).
5b. HTML Entities within roll queries are parsed once after each roll query (e.g. &#125; becomes }, but &amp;#125; becomes &#125;)
6. Inline rolls are executed, starting with the most deeply nested inline roll working upward. The overall result of the inline roll is substituted in place where it appeared in the formula.
7. The remaining roll is executed: first, dice are rolled for any dice (e.g. 2d6 is rolled; including any special dice such as dropped or exploding), then the result of that roll is substituted into the formula.
8. Mathematical functions like floor() and ceil() are executed.
9. The entire remaining formula is evaluated, including observing proper Math Order of Operations (parentheses first, then multiplication/division, then addition/subtraction).
10. Custom Roll Parsing happens.
11. HTML Entities are processed once.
12. The message is sent to q Text Chat as well as the API sandbox.


Thanks goes to Scott C. & Lockbox313 for additional testing in this matter!

Test confirming order of CRP and last HTML Entity processing (Step 10 & 11)
Have the input be 1d6&#38;, and run the following sheet. Output having two ! means CRP happens before the last processing of HTML Entities.
<input type="text" name="attr_test2">
<button type="action" name="act_test">test</button>
<rolltemplate class="sheet-rolltemplate-test">
    <div class="sheet-template-container">
        <h1>{{name}}</h1>
        <div class="sheet-results">
            <h4>Custom Result = {{computed::roll1}}</h4>
        </div>
    </div>
</rolltemplate>
<script type="text/worker">
   on(`clicked:test`, function() {
    console.log("button pressed");
           getAttrs(["test2"], function(values) {
               console.log(`values ${JSON.stringify(values)}`);    
               let test_str = values.test2;
            startRoll("&{template:test} {{name=Test}} {{roll1=[[0]]}}", (results) => {
               test_str  =  test_str.replace(/!/g,"!!");
               console.log("test_str: "+test_str);
                finishRoll(
                    results.rollId,
                    {
                        roll1: test_str ,
                    }
                );
            })                     
           });
    });
</script>
<pre>|}

==Examples==
(todo)

* [https://app.roll20.net/forum/permalink/10915703/ Different order of operation in character sheet's ability.] explanation by RainbowEncoder

[[Category:Macros]]
[[Category:Character Sheet Creation]]
</noinclude>