Work with HSL operators
Operators enable you to manipulate data with commands. An operator is a symbol that represents an action to be taken by the command. The operator acts on an operand, the object of the action. In Shogun Post scripting, an operand can be a variable or a constant value.
Operators require operands to be of the same type. If there is any difference between the left and right variable, one may be converted to the value of the other.
Operators are written as part of an expression, with the operator between the operands in the following format:
operand_1 operator operand_2 |
For example:
int $frame = 1;
where:
int $frame | is the left side operand, in this case a variable. |
= | is the operator, in this case an assignment operator |
1; | is the right side operator, in this case the value assigned to the variable |
The following sections show you how to work with operators:
Understand operator types
Shogun Post scripting recognizes the following types of operators:
Arithmetic operators
Arithmetic operators are symbols that represent mathematic functions.
An expression with an arithmetic operator, also called an arithmetic statement, the arithmetic operator is written with the first operand followed by a mathematic symbol, followed by the second operand:
operand_1 mathematizations operand_2 |
This table shows the symbols that Shogun Post recognizes as arithmetic operators, defines their function, and indicates the data types that can be used as operands.
Symbol | Function | Valid data types |
---|---|---|
+ | addition | float, int, string, vector, rotation |
- | subtraction, negation | float, int, string, vector, rotation |
* | multiplication (int, float) or dot product (vector) | float, int, vector |
/ | division | float, int, vector |
% | modulus (remainder of division) | float, int |
^ | power (int, float) or cross product (vector) | float, int, vector |
Arithmetic operator shortcuts
Shortcut operations enable you to assign a variable the value of itself operated on by some other value. You can write shortcut expressions for any arithmetic operators.
For example, instead of writing the full expression in the format:
variable = variable operator value
you can write the following shortcut format:
variable operator = value
The following table shows all of the shortcut operators of this form, their expanded syntax, and their values.
Shortcut syntax | Expanded syntax | Value |
---|---|---|
variable += value; | variable = variable + value; | variable + value; |
variable -= value; | variable = variable - value; | variable - value; |
variable *= value; | variable = variable value; | variable value; |
variable /= value; | variable = variable / value; | variable / value; |
variable %= value; | variable = variable % value; | variable % value; |
variable ^= value; | variable = variable ^ value; | variable ^ value |
You can also write shortcut expressions to add and assign int, float, and vector data types at the same time.
For example, instead of writing the full expression:
$hammer = $hammer + 3.5;
You can write the following shortcut:
$hammer += 3.5;
Both statements accomplish the same results.
Arithmetic operator examples
These examples show you how to write expressions using arithmetic operators for the specified data types.
- Integers and floats
Basic math rules apply for integer, floating-point attributes, and variables. The modulus (%) operator calculates the remainder of division. For example,int $plop = 11 % 3; //Value: 2
- Strings
Strings can be concatenated, or appended to other strings, with the plus sign + operator. For example:// Starts out empty
string $words = "Thanks for all" + " the fish";
string $ myStr;
// Value: " from the market"
$ myStr += " from the market";
// Value "Thanks for all the fish from the market"
string $fullStr = $words + $myStr;
- Vectors
The power (^) operator calculates the cross product of vectors, and the multiplication * operator calculates the dot product of vectors. For all other operators, each component of one vector is operated on by its counterpart component in the other vector.//Value: -4.000000, 2.000000, 0.000000
vector $test = <<1, 2, 3>> ^ <<2, 4, 4>>;
print ($test);
Operator shortcut examples
These examples show how to use shortcut operators rather than writing out full expressions:
// Declare variable int $inside = 1; // Shortcut for expression $inside = $inside + 9 $inside += 9; // Shortcut for expression $inside = $inside –2 $inside -= 2; // Shortcut for expression $inside = $inside 10 $inside *= 10; print $inside; //declare variable vector $outside = <<2,2,2>>; // Shortcut for expression $outside += $outside + <<<<1,2,3>> $outside += <<1,2,3>>; print $outside;
Comparison operators
Comparison operators are used to compare two values and evaluate whether a statement is true based on the relation of the value on the left to the value on the right and then execute the statement accordingly.
Comparison operators are valid with int, float, and vector data types. The truth of the statement depends on the relation of the value on the left to the value on the right of the operator.
An expression with an comparison operator, also called a comparison statement, is written with the first operand followed by a relational symbol, followed by the second operand:
operand_1 relational_symbol operand_2 |
This table shows the symbols that Shogun Post recognizes as relational operators and indicates the condition that would result in the statement being evaluated as true.
Symbol | True only if the left-hand side is |
---|---|
< | less than the right-hand side |
> | greater than the right-hand side |
== | equal to the right-hand side |
!= | not equal to the right-hand side |
>= | greater than or equal to the right-hand side |
<= | less than or equal to the right-hand side |
Note that comparison operators and arithmetic operators can be used in the same statement.
// Prints "I love you" float $threshold = 10.0; if( $threshold < 25.0 ) print( "I love you" );
You can write a hook statement with a comparison operator using three operands.
Examples of comparison expressions
//true if (3.5 < 6) print ("true"); //false if (10 < 10) print ("true"); //false if (56 == 57) print ("true"); //false if (-3 != 3) print ("true"); //true if (-3 >= -3) print ("true"); //false if (0 <= -3) print ("true");
Hook statements
A hook statement is written with three operands. The first operand is a test condition that represents true or false. This operand is followed by a question mark ?, then the second and third operands are separated by a colon ( : ).
operand_1 ? operand_2 : operand_3 |
If the test condition is true, the operand after the question mark ?, is used. Otherwise the operand after the colon ( : ) is used. For example:
test condition ? operation1 : operation2 int $markers = 5 < 9 ? 40 : 50; // Value: 40
Logical operators
Logical operators are used to compare two values and evaluate whether a statement is true or false and then execute the statement accordingly. Logical operators create boolean results, that is, values of either TRUE or FALSE.
You can use logical operators with int, float, and vector data types. The values of each type correspond to being either true or false:
- An int or float value is considered false when its value, converted to type int, is 0.
- A vector value is considered to be false when its magnitude, converted to type int, is 0.
- All other converted values are considered to be true.
An expression with a logical operator, also called a logical statement, is written with the first operand followed by a logic symbol, followed by the second operand:
operand_1 logic_symbol operand_2
This table shows the symbols that Shogun Post recognizes as logical operators, defines their function, and indicates how the statement is evaluated as true.
Symbol | Logic | True only if |
---|---|---|
|| | OR | either left-hand or right-hand side is true |
&& | AND | both left-hand and right-hand sides are true |
! | NOT | right-hand side is false (not supported yet) |
Examples of logical operators
//true if ( true || false) print ("true"); //false if ( true && false) print ("true"); //false if (no || no) print ("true"); //false if (off || off) print ("true");
Determine operator precedence
If you use multiple operators on the same row, they have equal precedence. If a statement has two or more operators with the same precedence, the left-most operator is evaluated first. Unary and assignment operators (=) are right associative; all others are left associative.
The following sections list operator precedence from highest to lowest and show you how to influence precedence by grouping operators.
Highest precedence
( ) [ ] */ % ^ + - < <= > >= == != && || ?: = += -= *= /=
Lowest precedence
Examples of when precedence effects the value of a statement:
//$front = (5 +(2*3)) int $front = 5 + 2 3 //$side = $side ((8 % 5) / 3) int $back = 2 - 3 int (1 < 3); //$side = $side ((8 % 5) / 3) int $side = 2; $side = 8 % 5 / 3;
Grouping operations for precedence
The order of operation can be manually dictated by placing parentheses around operators. This is because parentheses have highest operator precedence. Examples of dictating precedence:
//$side = $side (8 % (5 / 3)) int $side = 2; $side *= 8 % (5 / 3); int $top = (5 ( int($side >4)));