Auto converting data types

(Redirected from Autoconvert)
Jump to: navigation, search


The HSL compiler will automatically convert many data types to other data types, based on context.


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:

Valid options

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.


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.

Conversion functions

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:

As a final note, the script strutils, on both client and server sides, has the functions:

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))

See also

Personal tools