Typecasting

From ZCWiki
Jump to: navigation, search

Typecasting is the act of converting one data type to another, for example, converting an integer variable (type int) to a floating point (type float). The need for typecasting stems from the fact that many programming languages store different data types differently, so a program or script needs to know how to interpret the data. ZScript is slightly different. ZScript will automatically convert some types of data for you. ZScript does not include any functions or methods to manually convert data from one type to another.

Primitive Data Types

int and float

The int and float types are interchangable; there is no need to convert one to the other in order to perform operations on them. In some sense, you can think of all numbers as floats. For instance, the following statements are perfectly valid, even though the int and float data types are mixed:

int W = 10;
float X = 10.5;

// Do some math and dump the results to a log file
W = W + X;
Trace(W); // Prints "20.5000" to the allegro.log file.

In this case, the integer variable "W" is automatically converted to a floating point value, even though it was declared as an int.

While the types int and float are essentially the same, you may still declare some variables as one type or the other depending on how you expect to use them. For example, you would declare a variable named "Links_X_Position" as an int because a floating-point value for Link's X position makes no sense (his position is computed in whole pixels). This is a good programming practice, even if the ZScript compiler doesn't really care.

bool and float

ZScript will consider any non-zero number to have a boolean value of true. Thus, the following if statement is legal, even though it doesn't make much sense:

if(42){
  // Statements here will always get executed.
  // ...
}

In this case, since 42 is non-zero, the statements in the if statement will always get executed.

The above example isn't very useful, but this feature's usefulness is apparent when you consider functions that return the number of items or enemies on the screen. For instance, if you need to do something when at least one non-playing character is on the screen, you could use:

if(Screen->NumNPCs()){
  // Statements here will get executed if
  // At least one enemy or guy is on the screen.
  // ...
}

Note that ZScript will not automatically convert from a bool to a float (or int). To do this, you will have to write your own function, or use the one below as applicable:

// Converts a boolean to a float or int.
float CFloat(bool A_Boolean){
  if (A_Boolean) {
    return 1.0000;
  }
  else {
    return 0.0000;
  }
}

Other data types

There is no built-in way to convert more complex data types (such as npc or ffc) to other types. While the usefulness of such an operation might be questionable, it would be possible to write a custom function that copied some of the information from an ffc type to a npc type, for instance.

See Also