This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
hpl2:amnesia:script_language_reference_and_guide:types [2012/12/26 21:05] thegreatcthulhu |
hpl2:amnesia:script_language_reference_and_guide:types [2013/01/03 14:18] (current) thegreatcthulhu Deleted a sentence... Apparently, it was not true! :D Good! |
||
---|---|---|---|
Line 5: | Line 5: | ||
- | <code =c++>// Commonly Used | + | <code=c++>// Commonly Used |
int a = 0; // integer | int a = 0; // integer | ||
float b = 3.14f; // floating point | float b = 3.14f; // floating point | ||
- | + | ||
bool c = true; // Boolean (true/false) | bool c = true; // Boolean (true/false) | ||
bool d = false; | bool d = false; | ||
- | + | ||
string e = "Amnesia"; // string | string e = "Amnesia"; // string | ||
- | + | ||
///////////////////////////////////////////////////////////// | ///////////////////////////////////////////////////////////// | ||
// Signed Integer Types | // Signed Integer Types | ||
- | int8 f = -2; // | + | int8 f = -2; // -128 to 127 |
- | -128 to 127 | + | int16 g = 5; // -32,768 to 32,767 |
- | int16 g = 5; // | + | int h = 100; // -2,147,483,648 to 2,147,483,647 |
- | -32,768 to 32,767 | + | int64 i = 8; // -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
- | int h = 100; // | + | |
- | -2,147,483,648 to 2,147,483,647 | + | |
- | int64 i = 8; // | + | |
- | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | + | |
- | + | ||
// Unsigned Integer Types | // Unsigned Integer Types | ||
uint8 j = 255; // 0 to 255 | uint8 j = 255; // 0 to 255 | ||
Line 30: | Line 26: | ||
uint l = 1234; // 0 to 4,294,967,295 | uint l = 1234; // 0 to 4,294,967,295 | ||
uint64 m = 56; // 0 to 18,446,744,073,709,551,615 | uint64 m = 56; // 0 to 18,446,744,073,709,551,615 | ||
- | + | ||
// Script engine is optimized for 32-bit data types | // Script engine is optimized for 32-bit data types | ||
// int is alias for int32 | // int is alias for int32 | ||
// uint is alias for uint32 | // uint is alias for uint32 | ||
- | + | ||
- | + | ||
// Floating Point Types | // Floating Point Types | ||
float n = 1.234f; | float n = 1.234f; | ||
double o = 5.67; | double o = 5.67; | ||
- | + | ||
float p = 10.0e-5f; // exponent notation | float p = 10.0e-5f; // exponent notation | ||
double q = 6.022141e+23; // exponent notation | double q = 6.022141e+23; // exponent notation | ||
- | + | ||
- | + | ||
// Classes | // Classes | ||
MyClass obj1; // instantiates the obj object to default value | MyClass obj1; // instantiates the obj object to default value | ||
MyClass obj2(); // likewise, explicitly invoking the constructor | MyClass obj2(); // likewise, explicitly invoking the constructor | ||
MyClass obj3(5); // constructor with a parameter | MyClass obj3(5); // constructor with a parameter | ||
- | + | ||
// Classes - Using Constructors As Functions | // Classes - Using Constructors As Functions | ||
// NOTE: Not allowed in global scope! | // NOTE: Not allowed in global scope! | ||
MyClass obj4 = MyClass(); // default constructor | MyClass obj4 = MyClass(); // default constructor | ||
MyClass obj5 = MyClass(10); // constructor with a parameter | MyClass obj5 = MyClass(10); // constructor with a parameter | ||
- | + | ||
MyClass@ ptrObj1; // A null handle (pointer) to an object of type MyClass | MyClass@ ptrObj1; // A null handle (pointer) to an object of type MyClass | ||
MyClass@ ptrObj2 = @obj2; // ptrObj2 holds a reference to obj2 | MyClass@ ptrObj2 = @obj2; // ptrObj2 holds a reference to obj2 | ||
- | + | ||
// Arrays | // Arrays | ||
int[] arrayA(5); // A 5-element integer array (uninitialized) | int[] arrayA(5); // A 5-element integer array (uninitialized) | ||
Line 62: | Line 58: | ||
uint[] arrayC = { 1, 1, 2, 3, 5, 8 }; // explicit initialization | uint[] arrayC = { 1, 1, 2, 3, 5, 8 }; // explicit initialization | ||
uint[] arrayD = { , , 5, 5, , }; // 6-element array, with some elements initialized | uint[] arrayD = { , , 5, 5, , }; // 6-element array, with some elements initialized | ||
- | + | ||
- | + | ||
MyClass[] objArray(10); // An array of objects | MyClass[] objArray(10); // An array of objects | ||
MyClass@[] ptrArray(10); // An array of object handles (pointers) | MyClass@[] ptrArray(10); // An array of object handles (pointers) | ||
- | + | ||
// Function Handles (Function Pointers) | // Function Handles (Function Pointers) | ||
MyFuncPtr@ funcPtr = @MyFunc; | MyFuncPtr@ funcPtr = @MyFunc; | ||
</code> | </code> | ||
+ | |||
Line 106: | Line 103: | ||
- | * //Range://''+/- 3.402823466e+38'' (or approximately: +/- 3.<//38_zeros_here//>) | + | * //Range://''+/- 3.402823466e+38'' (or approximately: +/- 3<//38_zeros_here//>.0) |
- | * //Smallest positive value://''1.175494351e-38'' ( or: <//38_zeros_here//>.1175494351 ) | + | * //Smallest positive value://''1.175494351e-38'' ( or: 0.<//37_zeros_here//>1175494351 ) |
* //Precision://''6-7 digits'' | * //Precision://''6-7 digits'' | ||
The ''double'' type is also a floating point type, but it takes up 64 bits (8 bytes) of memory, double the amount the ''float'' type uses (thus the name). It can represent a wider range of values, and with a greater precision. | The ''double'' type is also a floating point type, but it takes up 64 bits (8 bytes) of memory, double the amount the ''float'' type uses (thus the name). It can represent a wider range of values, and with a greater precision. | ||
- | * //Range://''+/- 1.7976931348623158e+308'' (or approximately: +/- 1.<//308_zeros_here//>) | + | * //Range://''+/- 1.7976931348623158e+308'' (or approximately: +/- 1<//308_zeros_here//>.0) |
- | * //Smallest positive value://''2.2250738585072014e-308'' ( or: <//308_zeros_here//>.22250738585072014 ) | + | * //Smallest positive value://''2.2250738585072014e-308'' ( or: 0.<//307_zeros_here//>22250738585072014 ) |
* //Precision://''15-16 digits'' | * //Precision://''15-16 digits'' | ||
- | Real number literals are considered to be of type ''double'' by default. When assigning values to floating-point variables, you can append the 'f' suffix to numerical literals to indicate that they should be treated as ''float''-s instead, and avoid unnecessary conversion, like this: \\ ''float number = 9.81f;'' | + | Real number literals are considered to be of type ''double'' by default. When assigning values to floating-point variables, you can append the 'f' suffix to numerical literals to indicate that they should be treated as ''float''-s instead, and avoid unnecessary conversion, like this: \\ ''float number = 9.81f;'' |
Line 122: | Line 119: | ||
The ''bool'' type is used to represent the values ''true'' and ''false''. Variables of ''bool'' type are useful //as indicators//. You can use them to represent binary states (for example, on/off switches), or indicate if some event happened or not. You can use them as return values of functions, to indicate the success or failure of an operation. \\ The ''bool'' type is named after George Boole, an English mathematician, philosopher and logician, who first defined an algebraic system of logic in the mid 19th century. | The ''bool'' type is used to represent the values ''true'' and ''false''. Variables of ''bool'' type are useful //as indicators//. You can use them to represent binary states (for example, on/off switches), or indicate if some event happened or not. You can use them as return values of functions, to indicate the success or failure of an operation. \\ The ''bool'' type is named after George Boole, an English mathematician, philosopher and logician, who first defined an algebraic system of logic in the mid 19th century. | ||
+ | |||
+ | For more info about the operations available for the ''bool'' type, see [[hpl2:amnesia:script_language_reference_and_guide:control_flow_-_conditional_statements#comparison_operators|Comparison Operators]] and [[hpl2:amnesia:script_language_reference_and_guide:control_flow_-_conditional_statements#using_logical_operators|Using Logical Operators]]. | ||
Line 133: | Line 132: | ||
string aString = "" + anInteger; // "10" is stored in aString variable | string aString = "" + anInteger; // "10" is stored in aString variable | ||
</code> | </code> | ||
- | |||
- | |||
- | //Note:// Due to implementation details, when strings are passed as parameters to functions, they must always be passed by reference; otherwise, the game might crash, or the script will simply stop executing. | ||
Line 147: | Line 143: | ||
- | While you can think of variables of other data types as of containing their value directly, object handles (or objects pointers) are a special kind of data type which stores an indirect reference, or a pointer, to the memory location which holds the actual value. This enables you to have two or more object handle variables pointing to the same value; using and making changes through any one of them will be reflected on all the others. This is useful in some non-basic scenarios, so it is explained in another place (to do…). | + | While you can think of variables of other data types as of containing their value directly, object handles (or objects pointers) are a special kind of data type which stores an indirect reference, or a pointer, to the memory location which holds the actual value (object). This enables you to have two or more object handle variables pointing to the same value; using and making changes through any one of them will be reflected on all the others. This is useful in some non-basic scenarios, so it is explained in another place (to do…). |