Difference between revisions of "Cast Reference"
m
→Examples: Cleaned up syntax a bit
imported>Kahmul m |
imported>Rasikko m (→Examples: Cleaned up syntax a bit) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 26: | Line 26: | ||
| True if the array is 1 element or longer in size | | 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: | |||
<source lang="papyrus"> | |||
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) | |||
</source> | |||
=== Examples === | === Examples === | ||
<source lang="papyrus"> | <source lang="papyrus"> | ||
; | ; b gets true | ||
Bool b = 1 as bool | |||
</source> | </source> | ||
<br> | <br> | ||
<source lang="papyrus"> | <source lang="papyrus"> | ||
; | ; b gets false | ||
Bool b = "" as bool | |||
</source> | </source> | ||
<br> | <br> | ||
<source lang="papyrus"> | <source lang="papyrus"> | ||
; | ; b gets true | ||
Bool b = new int[5] as bool | |||
</source> | </source> | ||
Line 46: | Line 56: | ||
'''Compiler auto-cast from''': Nothing | '''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 === | === Examples === | ||
<source lang="papyrus"> | |||
; x gets 1 | |||
x = True as int | |||
</source> | |||
<br> | |||
<source lang="papyrus"> | <source lang="papyrus"> | ||
; x gets 10 | ; x gets 10 | ||
x = 10.5 as int | x = 10.5 as int | ||
</source> | |||
<br> | |||
<source lang="papyrus"> | |||
; x gets -1 | |||
x = -1.9 as int | |||
</source> | </source> | ||
<br> | <br> | ||
Line 62: | Line 82: | ||
'''Compiler auto-cast from''': Int | '''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 === | === Examples === | ||
<source lang="papyrus"> | |||
; x gets 1.0 | |||
x = True as float | |||
</source> | |||
<br> | |||
<source lang="papyrus"> | <source lang="papyrus"> | ||
; x gets 15.0 | ; x gets 15.0 | ||
Line 139: | Line 164: | ||
Weapon Property MyUberWeapon auto | Weapon Property MyUberWeapon auto | ||
OnTriggerEnter(ObjectReference | Event OnTriggerEnter(ObjectReference type) | ||
Weapon triggeredWeapon = (type as form) as weapon ; Cast the triggered object first to a form and then to a weapon. | Weapon triggeredWeapon = (type as form) as weapon ; Cast the triggered object first to a form and then to a weapon. | ||
Line 156: | Line 181: | ||
Keyword Property IsAnArrow auto | Keyword Property IsAnArrow auto | ||
OnTriggerEnter(ObjectReference Type) | Event OnTriggerEnter(ObjectReference Type) | ||
if(type.haskeyword(IsAnArrow)) | if(type.haskeyword(IsAnArrow)) |