Cast Reference

From the CreationKit Wiki
Jump to navigation Jump to search
<cast> ::= <expression> 'as' <type>

Casting an expression from one type to another allows you to perform additional manipulations on the object, or hand it off to a function or property that expects a different type. The various types of casts are listed below:

Cast to Boolean[edit | edit source]

Compiler auto-cast from: Anything

Anything in Papyrus can be cast to a boolean. The result that you get depends on the type of object that you are casting from, according to the following table:

Expression type Resulting boolean
Int True if the integer is non-zero
Float True if the float is non-zero (accounting for a small epsilon)
String True if the string isn't empty
Objects True if the object isn't None
Arrays True if the array is 1 element or longer in size

Notice that the compiler will auto-cast integers and floats to booleans, but not the other way around. This means that arithmetic involving booleans may not work as expected:

Bool b = True

; f gets 1.0, not 0.9, because b won't auto-cast to 1; instead, (9.0 / 10.0) auto-casts to True, and True * b = True, and True as Float = 1.0
Float f = 9.0 / 10.0 * b

; f gets 0.9 here because b is explicitly cast to an Int (1), which can then be auto-cast to a Float during the multiplication step
Float f = 9.0 / 10.0 * (b as Int)

Examples[edit | edit source]

; b gets true
Bool b = 1 as bool


; b gets false
Bool b = "" as bool


; b gets true
Bool b = new int[5] as bool

Cast to Int[edit | edit source]

Compiler auto-cast from: Nothing

Booleans, floats and strings can be cast to integers. True and False convert to 1 and 0 respectively, floating point values will be truncated, and strings will convert to their integer representation, if it has one (otherwise 0)

Examples[edit | edit source]

; x gets 1
x = True as int


; x gets 10
x = 10.5 as int


; x gets -1
x = -1.9 as int


; x gets 5
x = "5 little dwarves!" as int

Cast to Float[edit | edit source]

Compiler auto-cast from: Int

Booleans, integers and strings can be cast to floats. True and False convert to 1.0 and 0.0 respectively, integers will simply be their floating point equivalents, and strings will convert to their float representation, if it has one (otherwise 0.0)

Examples[edit | edit source]

; x gets 1.0
x = True as float


; x gets 15.0
x = 15 as float


; x gets 10.34
x = "10.34" as float

Cast to String[edit | edit source]

Compiler auto-cast from: Anything

Anything can be cast to strings. The result that you get depends on the type of object that you are casting from, according to the following table:

Expression type Resulting string
Bool "True" or "False" depending on the value
Int The string version of the integer
Float The string version of the float
Objects A string representing the object in the format: "[ScriptName <EditorID (FormID)>]"
Arrays A list of elements in the array separated by commas, formatted as above, and possibly truncated with a "..." if too long for the internal string buffer.

Examples[edit | edit source]

; x gets "15"
x = 15 as string


; x gets "[0, 0, 0]"
x = new int[3] as string

Cast to Object[edit | edit source]

Compiler auto-cast from: Child object

Only other objects can be cast to objects, and only if that object is a direct child or parent of the one being cast. If you are casting from the parent object to the child one, the cast may fail if it isn't actually an instance of the child object, in which case the result will be None.

Examples[edit | edit source]

; x gets the parent object
x = ChildVariable as ParentObject


; x gets the child object if it is one, otherwise None
x = ParentVariable as ChildObject

Cast to Array[edit | edit source]

Compiler auto-cast from: Nothing

Nothing can be cast to an array, even other arrays if the base types would normally cast just fine.

Cast to different types[edit | edit source]

Casting types to different ones might be useful i.e. if you want to know the type of the object triggered with your triggerzone. But, for example, you can't cast an ObjectReference to a Weapon or compare them. In this case you can work with the "Form"-type.

Examples[edit | edit source]

Weapon Property MyUberWeapon auto

Event OnTriggerEnter(ObjectReference type)

Weapon triggeredWeapon = (type as form) as weapon   ; Cast the triggered object first to a form and then to a weapon.
if(triggeredWeapon == MyUberWeapon)
  ; if triggeredWeapon is equal to MyUberWeapon, do something.
endif

EndEvent


But if you only want to determine the type of the object, you could also use a keyword:


Keyword Property IsAnArrow auto

Event OnTriggerEnter(ObjectReference Type)

if(type.haskeyword(IsAnArrow))
  ; if the object type has our keyword, do something.
endif

EndEvent