Types

In our first t program we wrote a simple print statement with a string containing exactly what should be printed to the output. For more advanced applications however, we may need to store values that we can call upon at a later time. For that purpose, we need variables.

Variables is an umbrella term used to refer to types of values that can be stored and altered, or recalled in code. You may, for instance, store a message identifier as an integer, a whole number type. There are four types predefined in t with support of arrays of any type though more complex types can be created using the ones available. These types are:

int  – A 32-bit signed integer.
float – A 32-bit floating point number.
char – An 8-bit character (signed integer)
byte – An 8-bit unsigned integer.

Using a Variable

Like in many other languages, a variable must be declared before it can be used. For example, let’s alter our current program to use a variable. We’ll use an int and change our text to read, “Hello World! version 2” But rather than simply writing the number ‘2’, we want to practice using an integer.

on start {
     int version = 2;
      printf(“Hello World version %d!\n”, version);
}

We’ve declared an integer named version and initialized it at the same time by assigning it a value. The next change we made was rather than writing the number, we’re able to print the integer in our string using a format specifier; in this case, %d.

Strings

Let’s try the same thing, but this time replace our text with a string. There is no string type in t, however by declaring an array of type char, we’re able to make our own string. Let’s take a look at that now.

on start {
     int version = 2;
     char hello[22] = “Hello World! Version ”;
      printf(“%s %d!\n”, hello, version);
}

By placing brackets next to the char variable name as we declared it, we are declaring a char array rather than a single variable of type char. In this example we’re also initializing our char array by assigning a value during declaration. For our value, we need the total number of characters to be stored in the array, +1 for an implicit \0 at the end of the string. Finally, in the printf() function, the format specifier %s is used to inform the function it should be anticipating a string.

Static Variables

A static variable is a variable that retains its value upon exiting and re-entering the block whose scope within it resides. So regardless of how many times the block containing the static variable is entered, it will only be initialized the first time the block is entered and the variable’s value will be retained after exiting the block. One last modification of our hello world example below shows a simple example of a static variable being used to count how many times the hook event triggers. So when the key, ‘h’ is pressed, the event hook will trigger, the message will be printed to the console with the static variable being updated to reflect the count.

on key ‘h’ {
     static int timesPressed = 1;
     char hello[22] = “Hello World! Pressed ”;

      printf(“%s %d!\n”, hello, timesPressed);
     timesPressed++;
}
Lesson tags: t script, TRX

Types Quiz

Please sign up for the course before taking this quiz.
  1. 1. What is wrong with the statement: char word = “integer”;

  2. 2. t has four predefined types. These are: int, float, char, and ____.

Back to: <em>t</em> Scripting > Basics of t