User Tools

Site Tools


hpl2:amnesia:script_language_reference_and_guide:constants_and_enumerations

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
hpl2:amnesia:script_language_reference_and_guide:constants_and_enumerations [2013/01/13 23:25]
thegreatcthulhu [Enumerations]
hpl2:amnesia:script_language_reference_and_guide:constants_and_enumerations [2015/10/06 21:06]
thegreatcthulhu [At a Glance]
Line 9: Line 9:
  
 **Constants** **Constants**
-<code c++> +<code c++>// Some math constants:
-// Some math constants:+
 const float PI = 3.1415926f; const float PI = 3.1415926f;
 const float E = 2.7182818f; const float E = 2.7182818f;
  
- +// Integer constants:
-// Integer constants: ​+
 // The constants in this example define when should collision events take place; // The constants in this example define when should collision events take place;
 // intended to be used with the AddEntityCollideCallback() engine function // intended to be used with the AddEntityCollideCallback() engine function
Line 28: Line 26:
  
 **Enumerated Constants (Enumerations)** **Enumerated Constants (Enumerations)**
-<code c++> +<code c++>enum Color     // Note: Enums are based on the int type.
-enum Color     // Note: Enums are based on the int type.+
 { {
     Red,        // has the default value of: 0     Red,        // has the default value of: 0
Line 35: Line 32:
     Blue        // value: (previous + 1) = 2, etc, if more added...     Blue        // value: (previous + 1) = 2, etc, if more added...
 } }
- 
  
 // Usage: // Usage:
Line 44: Line 40:
  
 // Assigning an integer value is not possible without an explicit conversion: // Assigning an integer value is not possible without an explicit conversion:
-Color col = 2;    // Causes compilation error! ​+Color col = 2;    // Causes compilation error!
  
-// Converting from integers - should generally be avoided: ​+// Converting from integers - should generally be avoided:
 Color col = Color(2); ​  // Assigns Blue to col, since 2 corresponds to Color::Blue Color col = Color(2); ​  // Assigns Blue to col, since 2 corresponds to Color::Blue
  
Line 54: Line 50:
 // This is allowed: // This is allowed:
 int colValue = col;   // so, enums can be passed to functions expecting ints --> see example below int colValue = col;   // so, enums can be passed to functions expecting ints --> see example below
- 
- 
  
 // Enumerations - choosing your own values // Enumerations - choosing your own values
Line 68: Line 62:
 AddEntityCollideCallback("​Player",​ "​Area_Example",​ "​ExampleCallback",​ false, CollisionState::​Both);​ AddEntityCollideCallback("​Player",​ "​Area_Example",​ "​ExampleCallback",​ false, CollisionState::​Both);​
  
- +// You can define all or some of the values; those left undefined will be
-// You can define all or some of the values; those left undefined will be +
 // assigned the value of previous_constant + 1 // assigned the value of previous_constant + 1
 enum Ending enum Ending
-   +{
     Good = 1,           // =   1     Good = 1,           // =   1
     ReallyGood, ​        // =   ​2 ​  ​(previous + 1)     ReallyGood, ​        // =   ​2 ​  ​(previous + 1)
Line 281: Line 274:
 // Later on: // Later on:
 HealingPotion vial = HealingPotion::​Small;​ HealingPotion vial = HealingPotion::​Small;​
-HealingPotion leatherArmor = BodyArmor::Small;+HealingPotion leatherArmor = BodyArmor::Weak;
  
  
Line 388: Line 381:
   * 0 - the lock is left unlocked   * 0 - the lock is left unlocked
  
-All of that information is encoded in //a single byte//. This is one of the reasons to use flags - using them saves memory (a single byte vs several state variables). The other reason is that flags can be (and usually are) passed as parameters to functions, whereby several indicators of simple binary ​state are passed to the function simultaneously,​ through one variable. Flags often encode "​settings"​-like data, but should ​not be overused, as they can affect code readability.+All of that information is encoded in //a single byte//. This is one of the reasons to use flags - using them saves memory (a single byte vs several state variables). The other reason is that flags can be (and usually are) passed as parameters to functions, whereby several indicators of simple binary ​states ​are passed to the function simultaneously,​ through one variable. Flags often encode "​settings"​-like data, but should be used with care, as they can affect code readability ​and clarity.
  
-How to manipulate individual bits then? Well, by assigning integer numbers to variables, and by using binary logical operators, described below. However, manipulating bits while working with numbers in the decimal system is not very convenient. This is why programmers often use //​hexadecimal//​ (HEX) numbers for such tasks. The reason is: there'​s a direct correspondence between HEX and binary numbers, as the table below shows, which makes it easy to deal with binary representations.+//How to manipulate individual bits then?// Well, by assigning integer numbers to variables, and by using binary logical operators, described below. However, manipulating bits while working with numbers in the decimal system is not very convenient. This is why programmers often use //​hexadecimal//​ (HEX) numbers for such tasks. The reason is: there'​s a direct correspondence between HEX and binary numbers, as the table below shows, which makes it easy to deal with binary representations.
  
 {{ http://​farm9.staticflickr.com/​8365/​8371351734_f2f2795a54.jpg }} {{ http://​farm9.staticflickr.com/​8365/​8371351734_f2f2795a54.jpg }}
Line 396: Line 389:
 As you can see, unlike the decimal number system, which represents all numbers using 10 different symbols (0-9), the HEX number system represents those same numbers using 16 different symbols (0-F). It just so happens that each of the HEX digits perfectly corresponds to one of all possible 4-bit combinations. Any one byte can thus be represented by 2 HEX digits - all you need to do is to refer to the table above, pick two hexadecimal digits, and write them together. To get the corresponding binary number, just replace the HEX digit with its binary equivalent from the table. As you can see, unlike the decimal number system, which represents all numbers using 10 different symbols (0-9), the HEX number system represents those same numbers using 16 different symbols (0-F). It just so happens that each of the HEX digits perfectly corresponds to one of all possible 4-bit combinations. Any one byte can thus be represented by 2 HEX digits - all you need to do is to refer to the table above, pick two hexadecimal digits, and write them together. To get the corresponding binary number, just replace the HEX digit with its binary equivalent from the table.
  
-Representing bytes using HEX system - some examples: 
 <​code>​ <​code>​
 +Representing bytes using HEX system - some examples:
 +
 +----------------------
 HEX          binary HEX          binary
 ---------------------- ----------------------
Line 406: Line 401:
  ​3A ​        0011 1010  ​3A ​        0011 1010
  ​FC ​        1111 1100  ​FC ​        1111 1100
 +----------------------
 </​code>​ </​code>​
 +
 +Note that to be able to do this, you //​don'​t have to// understand the internal workings of the decimal, binary and hexadecimal number systems (although I encourage you to learn more on the web), nor do you have to know how to convert binary and HEX representations to and from decimal; all you need to know is which HEX digit corresponds to which binary sequence (and you can get that from the table). ​
  
 Since HEX digits include both numerals and characters, the script language needs a way to distinguish HEX numerical literals from other numbers and variable names. So, the language provides the ''​0x''​ prefix - when you want to express a number in the HEX format, start by typing ''​0x''​ and then immediately (with no spaces in between) follow with your hex digits: Since HEX digits include both numerals and characters, the script language needs a way to distinguish HEX numerical literals from other numbers and variable names. So, the language provides the ''​0x''​ prefix - when you want to express a number in the HEX format, start by typing ''​0x''​ and then immediately (with no spaces in between) follow with your hex digits:
hpl2/amnesia/script_language_reference_and_guide/constants_and_enumerations.txt · Last modified: 2015/10/06 21:06 by thegreatcthulhu