Skip to content

4.X Expressions

Ava Pek edited this page Feb 27, 2021 · 1 revision

Expressions are used to perform arithmetic and logic. This document contains examples to show you what can be done with expressions and how you can expect them to behave.

Variables

Variables names in Rumor can only contain alphanumeric characters (a-z, A-Z, 0-9) and the _ character. It must also start with a letter.

You can assign new values by using the = operator. For example, variables can be assigned the following types in Rumor scripts:

Script-interpreted types

Type Example
boolean x = {true}
double x = {1.0}
string x = {"Hello"}

For booleans, you can type true or false

Undefined

Rumor permits referencing variables before they have been defined, but it will always result in a no-op. For example, consider this substitution which references an undefined variable:

: { some_function(foobar) }

In this example, the substitution will return an empty string since foobar has not been assigned anything yet.

Additionally, any if/elif/else expression which references an undefined variable will always fail. For example:

if { foobar > 0 or foobar <= 0 }
  : This is never printed.

Finally, any attempt to assign a variable while using an undefined variable will always result in the variable being undefined. For example:

baz = { some_function(foobar) }

If foobar is not defined, neither will baz.

Variable Scope

Unlike some programming languages, variables do not fall out of scope in Rumor. For example:

choice "A":
  x = { 4 }
choice "B":
  # Do nothing
choose

: { x }

If the user selects choice A, the variable x is set and the number 4 is printed to the stage. If the user selects choice B, the variable x is never defined, so it prints an empty string to the stage instead.

Bindings

You can bind methods to a Rumor. All C# method types are supported, including Action, Action<T>, Action<T1,T2>, Action<T1,T2,T3>, Action<T1,T2,T3,T4>, Func<T>, Func<T1,T2>, Func<T1,T2,T3>, Func<T1,T2,T3,T4>, as long as they only use the types bool, double, and string.

You can call bindings in a script by typing the name of the binding, followed by parentheses that contain the arguments (or nothing, if the binding doesn't take any parameters). For example, if the following binding is defined in C#...

rumor.Bind<string>("print", (str) => { Debug.Log(str); });

...then you can call the binding in a script like so:

print("Hello world!")

When choosing a name for a binding, it is recommended to avoid any names that are prefixed with _, as those names are reserved for default bindings.

Default Bindings

Rumor has several default bindings available.

  • _auto_advance(float) Sets the amount of time in seconds before the script should attempt to automatically advance
  • _cancel_count() Returns the number of times the rumor has been cancelled
  • _finish_count() Returns the number of times the rumor has been finished
  • _choice() Returns the contents of the last chosen choice

Operators

Almost all operators in Rumor take an expression on the left and right and return a new value.

Math Operators

There are four math operators:

  • + Performs addition
  • - Performs subtraction
  • * Performs multiplication
  • / Performs division

Rumor is strongly typed and there is only one number type. This means any use of the math operators will always result in another double except in the case of using an undefined variable.

Boolean Operators

There are three boolean operators:

  • not or ! Returns true if the right hand expression is false or false if the right hand expression is true
  • and or && Returns true if both left hand and right hand expressions are true
  • or or || Returns true if the left hand or the right hand expression is true
  • xor or ^ Returns true if one expression is true and the other is false

Note that the ! operator does not use an expression on both the left and right hand sides like most operators; it only operates on a right-hand expression.

Comparison Operators

Comparison operators allow you to compare two number values.

There are four comparison operators:

  • < Returns true if the right hand expression is less than the right hand expression
  • <= Returns true if the right hand expression is less than or equal to the right hand expression
  • > Returns true if the right hand expression is greater than the right hand expression
  • >= Returns true if the right hand expression is greater than or equal to the right hand expression

Operator Precedence

The precedence of operators from highest to lowest priority for arithmetic is:

  • *
  • /
  • +
  • -

The precedence of operators from highest to lowest priority for logic is:

  • ==
  • !=
  • !
  • xor
  • and
  • or