Skip to content

3.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.

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

Script-interpreted types

Type Example
boolean $ x = true
integer $ x = 1
float $ x = 1.0
string $ x = "Hello"

For Booleans, you can type true, True, false, or False.

Variables can technically contain any object. A variable can come to contain such a value if it the variable's value was the result of a bound method or if it was set directly from C#.

Undefined and Null

In Rumor, undefined is the same as null. For example, consider a script that references a variable that has not been assigned a value yet.

$ say foobar

In this example, foobar will return null even though it has not been assigned anything yet.

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

say 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 returns null when we try to print it to the stage.

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>

If a binding does not return a value, it will return null in Rumor.

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. The default bindings can be loaded by calling the method rumor.SetupDefaultBindings(). The default bindings are briefly described below:

  • _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
  • _set_default_speaker(object) Sets the default speaker

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

When using math operators with variables of different types, Rumor implicitly decides the type that should result from the operation. The following charts for each operation show what types should be returned based on the input types. Any cell with the contents "ERROR" means that the operation is invalid and will cause an exception to be thrown at runtime.

Addition (+)

null bool int float string object
null null ERROR int float string ERROR
bool ERROR ERROR ERROR ERROR string ERROR
int int ERROR int float string int
float float ERROR float float string float
string string string string string string string
object ERROR ERROR int float string ERROR

Subtraction (-), Multiplication (*), and Division (/)

null bool int float string object
null null ERROR int float ERROR ERROR
bool ERROR ERROR ERROR ERROR ERROR ERROR
int int ERROR int float ERROR int
float float ERROR float float ERROR float
string ERROR ERROR ERROR ERROR ERROR ERROR
object ERROR ERROR int float ERROR ERROR

Assignment Operators

In addition to =, there are also four more assignment operators:

  • += Add and assign
  • -= Subtract and assign
  • += Multiply and assign
  • /= Divide and assign

All of these operators do the job of =, but instead of assigning the value on the right to the variable, it also performs the mathematical operation. For example...

$ x = x + 1

...is the same as:

$ x += 1

Boolean Operators

There are three boolean operators:

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

Rumor implicitly converts non-boolean types into booleans if they are about to be used with a boolean operator. The following chart determines how non-boolean types are converted:

Result int float string object
True != 0 != 0 == "" != null
False == 0 == 0 != "" == null

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. They only work with the types int and float.

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

Logic Operators

There is one logic operator, the . operator. It can be used to execute public methods and access public member variables on objects. For example:

C#

class Foo
{
    public float bar;

    public Foo(float f) { bar = f; }

    public void Print(string message)
    {
        Debug.Log(message)
    }
}

C# Binding

rumor.Bind<float>("new_foo", (f) => { return new Foo(f); });

Rumor script

$ a = new_foo(5)
$ a.Print("Hello world!")
$ b = a.bar

In the above script, the variable a is assigned a value of type Foo. Then, we call the print method on Foo. Lastly, we assign the value of the bar, which is 5, to the variable b.

Operator Precedence

The precedence of operators from highest to lowest priority is:

  • .
  • !
  • *
  • /
  • +
  • -
  • and
  • xor
  • or
  • ==
  • !=
  • *=
  • /=
  • +=
  • -=
  • =