Mod:Creation Kit/Cast Reference

::= 'is'

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:

Examples
x = 1 as bool x = "" as bool x = new int[5] as bool
 * x gets true
 * x gets false
 * x gets true

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 = 10.5 as int x = "5 little dwarves!" as int
 * x gets 10
 * x gets 5

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 = 15 as float x = "10.34" as float
 * x gets 15.0
 * x gets 10.34

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:

Examples
x = 15 as string x = new int[3] as string
 * x gets "15"
 * x gets "[0, 0, 0]"

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 = ChildVariable as ParentObject x = ParentVariable as ChildObject MyAlias.GetReference as Actor Quest property MyQuest auto MyQuest as MyQuestScript GetOwningQuest as MyQuestScript
 * x gets the parent object
 * x gets the child object if it is one, otherwise None
 * If the alias is an actual actor, you can cast the alias to actor to with functions like Mod:Creation Kit/StartCombat. (Functions that expect actors to be called on them.)
 * When needing to cast a Quest to its quest script, first make a property.
 * Then use this line:
 * Same can be done with GetOwningQuest for objects directly attached to the Quest.
 * 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 = myArray as Form[]
 * x gets a copy of myArray cast as an array of forms - elements that don't cast are converted to None

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 = myInt as var
 * x gets the value of myInt, and will return that value when cast back to an int