Module 0359: Values, types and expressions

Tak Auyeung

1 Setting up for this module

This module depends on the module that discusses event driven coding. Particularly, we need a way to trigger code execution and a way to pause the execution of a program.

2 Values and their types

GDScript, like Python, recognizes a few native types of values, such as the following:

There are other GDScript native types, but let us start with the ones mentioned.

3 How to experiment with values and types?

At this point, we have not introduced any method to experiment. Since the debugger of GoDot currently lacks the ability to evaluate arbitrary expressions at a breakpoint, we need to resort to the print function.

Download and import the project from the previous module and open it for editing. In the middle section of the top toolbar, click “Script”. Then click “File | Open” in the Script panel as show in the following screenshot.

Open a script for editing

Double-click DoSomething.gd to open the GDScript file. Now we use the function _on_DoSomething_pressed as the entry point to sprinkle print function calls to experiment with values. Let us start with some simple ones to verify that this method works.

“Comment out” the pass statement. To “comment out” means to turn some statement into comments and render it not a part of the code anymore. Commenting out is, sometimes, better than just deleting code because there are occasions where we may need to see what the code was before change.

Type some additional statements as follows:

func _on_DoSomething_pressed():
    # pass # Replace with function body.
    print(123)
    print(1.23)
    print(true)
    print(false)
    print('hey, this is pretty cool!')

Now run the app and click the button labeled “Do something!”. Confirm that the Output panel shows the values. Note how true is spelled with a lower-case ‘T’ as a value, but when printed, the first letter is capitalized. The same applies to the value of false.

Well, this is hardly fun because everything is, well, as expected. However, we have just learned how to use the print function and the Output pane for logging purposes. Logging by itself is not a very productive method to debug a program. However, when it is combined with the ability to use breakpoints, logging can provide insights of why an app is not doing what it is supposed to.

Now stop the app (click the stop button in the toolbar).

4 Expressions

An expression is a nested construct to compute a new value from some given value(s). For example 23+52 has a value of 75. The + symbol is sometimes called an “operator”, and the values 23 and 52 are called “operands”.

Refer to the official GoDot document for a complete treatment of operators. Some of the operators in the official document are more advanced than the intended level of this module.

It is important to understand the purpose of the “type” of a value and how it relates to operators. The type of a value, in essence, specifies “what can be done with” a value. Let us use an example:

'twenty-three is '+23

The GoDot editor checks the validity of code constantly. The problem of this expression is reflected as follows:

A problematic expression

This expression is problematic because 'twenty-three is ' is a string, whereas 23 is an integer. The addition (+) operator cannot handle the case when one side is a string and the other side is an integer. If the intention of the + operator is to “concatenate”, then both sides of the operator (both operands, that is) must be strings.

The integral value of 23 can be converted to a string '23' using the String() function (technically, it is a constructor). As a result, the following statement works fine:

    print('twenty-three is '+String(23))

In fact, this method using String(...) can convert just about any value to a string value.

#auto-typecast

Automatic type casting in other scripting languages

If you are familiar with some other scripting languages, you may realize that the problematic expression in this example works just fine. This is because some scripting languages supports automatic type casting.

Automatic type casting reads and expression from left to right, and uses the type of the left operand and the operator to infer the “proper type” of the right operand. If there is a known method to “cast” (convert) the original type of the right operand to what is expected, the casting is performed automatically.

In this sense, GDScript and Python are more strongly-typed (strict about types) than many other scripting languages.

#string-to-numerical

Converting from a string to a numerical value

When a user inputs a value, it is often in the form of a string. However, multiplication * requires operands to be numerical (integer or floating point).

Just as the String(...) constructor can be used to convert (just about anything) to a string, int(...) and float(...) convert a properly formatted number to an integer or a floating point value, respectively.

But what is int(...) is given a string that is not a proper number? For example, what is the value of int('whatever')?

The answer to this question is described by the documentation that is previously linked (specially the part that converts from a string)! In addition to figuring out the answer using the documentation, you are also encouraged to verify your answer by including a print in the sample program.

4.1 Relational/comparison operators

A particular type of operators is the “relational” operators. These are more commonly known as “comparison” operators. Note that GDScript and Python follow the C language operators. This means the “equals to” operator is == (double equal), and the “does not equal to” operator is != (exclamation point equal).

A relational expression has a Boolean value. “Boolean”, as a type, means that the possible values are true or false. Any expression that has a Boolean value is also called a “condition”. Conditions have a special place in programming as we will explore in another module.

Note that string comparison works differently from numerical comparison. You are encouraged to expand the sample program to print the result of the following expressions.

4.2 Boolean/logic operators

There is (yet) another type of operators called Boolean operators (aka logical operators). These operators require operands to be of Boolean type:

4.3 Ternary(-if) expression

This is an “odd ball” operator that may seem useless. However, when used correctly, a ternary(-if) expression can help simplify program code.

Let us example the following example:

23 if 12 < 13 else 45

There are three parts to this example. 23 is the first part, it is the “then-value”, 12 < 13 is the second part, it has to be a condition (boolean expression), and 45 is the third part, it is the “else-value”.

The way a ternary(-if) expression works is that the condition is evaluated first. In our example, 12 < 13 is evaluated. In this case, the condition is true.

If and only if the condition is true, the first value, in this example it is 23, becomes the overall value of the entire ternary(-if) expression. Because in this example the condition 12 < 13 is, indeed true, the value of the entire ternary(-if) expression is 23.

Let us consider another example:

'Tom' if 23 < 12 else 'Jerry'

23 < 12 is first evaluated. In this case, the relational/comparison expression has a value of false. As a result, the “else-value” of 'Jerry' becomes the value of the entire ternary(-if) expression.

5 Suggested exercise

Using the project from a previous module, experiment with all the mentioned operators to confirm your understanding of how the operators work.