User Tools

Site Tools


elderscript_flow_control

Flow Control

Being able to do simple expressions, we’ve already hinted that we can alter the flow of the program based on calculated values. This is done via an if then else end construction, similar to what is found in most programming languages. What is interesting is that this is also an expression (i.e., there is no difference between an expression and a statement - everything creates a value). So while something like JavaScript would have code like this:

  1. if (a > 10) {
  2. b = 5
  3. } else {
  4. b = 6
  5. }

We can do this:

  1. @B(if @A > 10 then
  2. 5
  3. else
  4. 6
  5. end)

Namely, the value of the if expression is used to assign to the variable. Granted, this is a simplistic example, but common enough that languages such as JavaScript have special operators such as ?: which become special if then else expressions (e.g., b = a > 10 ? 5 : 6).

ELDARScript is specially designed to support having dice rolled, with special features to make it be aware that dice take “time” to come up with a result. This is true with expression, especially with our conditional expressions. So

  1. if d20 > 10 then
  2. “Hit for+ d8
  3. else
  4. “Missed”
  5. end

will roll a d20, wait to see what the result is, and then roll the d8 (but only if the result was more than 10)

Chained conditionals

Instead of writing:

  1. if @A > 10 then
  2. “A Big”
  3. else
  4. if @B > 10 then
  5. “B Big”
  6. else
  7. “Small”
  8. end
  9. end

we can combine lines 3 & 4 together and omit line 8:

  1. if @A > 10 then
  2. “A Big”
  3. elif @B > 10 then
  4. “B Big”
  5. else
  6. “Small”
  7. end

These two expressions are otherwise identical, the second is just easier to read

Iteration

ELDARScript as a special flow control operator to do things multiple times named for. It looks like a classic iterator found in most programming languages. It has a special local variable that is created during the loop, as well as list to iterate through. However, since everything is an expression, you may wonder what sort of expression it returns? It generates a list of all the expressions created by the body of the loop (this makes it work like a map function found in many languages). So:

  1. for @i in 15 do
  2. @i x 3
  3. end

will create a list (3, 6, 9, 12, 15). (If you thought “it would be easier to do (1 … 5) x 3 to make this”, you are exactly correct).

NB: Do not try to roll dice while inside a for loop - it will not work (and even if it did, it probably wouldn’t work like you expect).

You can ignore the result of the for loop and it works much more like a “traditional” loop:

  1. @x0.
  2. for @i in 15 do
  3. @x@x + @i
  4. end

This will take the sum of numbers 1 through 5 and store the result into the local variable @x. In case you are curious, the for loop will create a list (1, 4, 9, 14, 19), since it will take the value of the assignment operator in each pass of the list.

Comments

Once you start making complex formulas, you’ll probably want to add comments to the code to help you document how it works. You can always throw in a string followed by a period (since the period will drop the previous value - the string - and use the next value), but ELDARScript provides for a specific comment capability:

  1. ❝Start by setting our total to zero❞@X ← 0.
  2. ❝Iterate through the values❞for @i in 15 do
  3. ❝Keep a running total❞ @X@X + @I
  4. end
  5. And at this point we've got that total❞

The use of “heavy double comma quotation mark ornament” indicates a comment. This can also be typed by an exclamation point and double quote

  1. !”Start by setting our total to zero”!
  2. @X0.
  3. “!Iterate through the values!”
  4. for @i in 15 do
  5. !“Keep a running total”!
  6. @X@X + @I
  7. end
  8. !”And at this point we've got that total”!

The editor will convert those to the ❝❞. Unlike most programming languages, however, ELDARScript is somewhat picky about where they go - they are actually part of the language itself, rather than some lexical whitespace that can be ignored. More specifically, they can go before a statement, after a period, or after the last statement. They do not, however, have a value, nor can they, for example, go in the middle of some parameters (i.e., foo(1 count, 2, “a” name) is illegal. These “heavy” appearance is designed to emphasize that they go between lines (as the editor would format them).

elderscript_flow_control.txt · Last modified: 2019/07/12 15:25 by dicenomiwiki