Cast Reference

From the Fallout4 CreationKit Wiki
Revision as of 08:33, 25 August 2016 by imported>Goggle (→‎Examples: Casting a quest to its script.)
Jump to navigation Jump to search
<cast> ::= <expression> 'as' <type>
<type check> ::= <expression> 'is' <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 "is" operator checks to see if the expression to the left is the type requested. The check is strict for base types (bool, int, etc) but loose for object types (Form, Alias, etc). The various types of casts are listed below:

Cast to Boolean

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
Structs True if the struct isn't None
Var Whatever the contents of the variable would cast to

Examples

; x gets true
x = 1 as bool


; x gets false
x = "" as bool


; x gets true
x = new int[5] as bool

Cast to Int

Compiler auto-cast from: Nothing

Floats, strings, and vars can be cast to integers. Floating point values will be truncated, and strings will convert to their integer representation, if it has one (otherwise 0). Vars will convert to whatever their contents would convert to, or 0 if their contents cannot be cast.

Examples

; x gets 10
x = 10.5 as int


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

Cast to Float

Compiler auto-cast from: Int

Integers, strings and vars can be cast to floats. Integers will simply be their floating point equivalents, and strings will convert to their float representation, if it has one (otherwise 0.0). Vars will cast to whatever their contents would have cast to, or 0.0 if their contents won't cast to floats.

Examples

; x gets 15.0
x = 15 as float


; x gets 10.34
x = "10.34" as float

Cast to String

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.
Structs A list of the values in the struct separated by commas, and possibly truncated with a "..." if too long for the internal string buffer.
Var Whatever the contents of the var would cast to

Examples

; x gets "15"
x = 15 as string


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

Cast to Object

Compiler auto-cast from: Child object

Only other objects or vars 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. If you attempt to cast a non-object var to an object, the result will also be None.

Examples

; x gets the parent object
x = ChildVariable as ParentObject


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


; If the alias is an actual actor, you can cast the alias to actor to with functions like [[StartCombat()]]. (Functions that expect actors to be called on them.)
MyAlias.GetReference() as Actor


; When needing to cast a Quest to its quest script, first make a property.
Quest property MyQuest auto
; Then use this line:
MyQuest as MyQuestScript
; Same can be done with GetOwningQuest() for objects directly attached to the Quest.
GetOwningQuest() as MyQuestScript
; When calling them, they must be placed in parenthesis.

Cast to Array

Compiler auto-cast from: Nothing

Arrays can cast to other arrays, but only explicitly, and only if their elements could be cast. Casting arrays makes a copy of the array, and elements that fail to cast will end up as None, 0, or equivalent.

Examples

; x gets a copy of myArray cast as an array of forms - elements that don't cast are converted to None
x = myArray as Form[]

Cast to Struct

Compiler auto-cast from: Nothing

Nothing can be cast to a struct.

Cast to Var

Compiler auto-cast from: Everything but arrays

Everything except arrays can be cast to the var type. The variable will then internally hold that value and remember what type it is.

Examples

; x gets the value of myInt, and will return that value when cast back to an int
x = myInt as var