Auto converting data types
root as NodeRef = GetRootNode() rootID as ID = root MsgPlayer(me, "", "The ID of the area's root node is: " + rootID + ".")
When the above code compiles, three auto-conversions happen:
- The NodeRef root is auto-converted to an ID;
- The NodeRef me is auto-converted to an ID; and
- The ID rootID is auto-converted to a String.
The following diagram shows the valid options for auto-conversion in HSL:
Note that many of the arrows are two-way. So for example, it is valid to auto-convert a Vector3 into a String, and it is also valid to auto-convert a String into a Vector3.
- Boolean <-autoconverts-> String
- Enum <-autoconverts-> String
- Float <-autoconverts-> String
- Float <-autoconverts-> Integer
- ID <-autoconverts-> String
- ID <-autoconverts-> NodeRef
- Integer <-autoconverts-> ID *see note
- Integer <-autoconverts-> String
- ScriptRef <-autoconverts-> String
- Vector3 <-autoconverts-> String (note: the string representation of a Vector3 follows the pattern: "(1,2,3)")
Note: Auto-converting between ID and Integer should be avoided whenever possible. Integers are 64-bit signed variables, while IDs are 64-bit unsigned variable, which is a fancy way of saying that Integers have both positive and negative values, but IDs only have positive values. This results in IDs being capable of representing values that are greater in size than what is possible with Integers.
Because of auto-conversion, many conversion functions in HSL can be ignored. In fact, those functions should be ignored, because using auto-conversion helps with code readability, as demonstrated by this:
raceMod as Integer = 5 classMod as Integer = 10 totalMod as Integer = raceMod + classMod MsgPlayer(me, "", "The modifiers are " + raceMod + " from race, plus " + classMod + " from class, for a total of " + totalMod + ".")
is easier to read than this:
raceMod as Integer = 5 classMod as Integer = 10 MsgPlayer(me, "", "The modifiers are " + itos(raceMod) + " from race, plus " + itos(classMod) + " from class, for a total of " + itos(totalMod) + ".")
The conversion functions in question are:
- itos(itosValue as Integer) as String;
- stoi(stoiString as String) as Integer;
- ftos(ftosValue as Float) as String;
- stof(stofString as String) as Float; and
- btos(btosValue as Boolean) as String.
As a final note, the script strutils, on both client and server sides, has the functions:
- idtos(i as ID) as String; and
- stoid(s as String) as ID.
which allow a script to convert a NodeRef to a String, and vice versa, by taking advantage of auto-conversion.
pc as NodeRef of Class character_class = playerutils:GetMyChar(me) MsgPlayer(me, "", "Your character node ID is: " + strutils:idtos(pc))