What are types?

Types provide a way to group data into a container. This is very useful as it allows you to keep your data organised. Let's consider a game where you want to keep track of a player's score, how many lives they have left and how many bullets they have remaining. This could be handled by declaring three separate variables.

score as integer
lives as integer
bullets as integer

An alternative approach would be to group this data together using a type. Here's how it would look.

type playerType
	score as integer
	lives as integer
	bullets as integer
endtype

The keyword type is used followed by a name, which is an identifier for the type. In this instance playerType has been used. The lines that follow declare variables that will be contained within this type. To finish the type declaration the keyword endtype is used.

With the type declaration in place the next step is to declare a variable that uses this type. This is achieved by using the as keyword and works in the same way as declaring other variables, except we use the identifier name for the type instead of the typical float, integer or string.

player as playerType

The variable declaration for player tells the program that it's going to be of type playerType, that has been declared earlier.

To access the variables contained within the type the dot operator is used. The next few lines show values being assigned to the variables within the type.

player.score = 100
player.lives = 3
player.bullets = 5

Here's a complete program that defines a type, declares a variable that uses the type, assigns values to the variable and then displays its values on screen.

type playerType
	score as integer
	lives as integer
	bullets as integer
endtype

player as playerType
player.score = 100 player.lives = 3 player.bullets = 5
do print ( player.score ) print ( player.lives ) print ( player.bullets )
sync ( ) loop