Difference between revisions of "Cast Reference"
m
→Examples: Cleaned up syntax a bit
imported>Taleden (notes and examples about casting between booleans and numeric types) |
imported>Rasikko m (→Examples: Cleaned up syntax a bit) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 28: | Line 28: | ||
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: | 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"> | <source lang="papyrus"> | ||
b = True | 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> | </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 65: | Line 67: | ||
; 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 157: | 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 174: | 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)) |