Difference between revisions of "Cast Reference"

117 bytes added ,  14:45, 31 March 2018
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
; x 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
 
x = 9.0 / 10.0 * b
; 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
; x gets 0.9 here because b is explicitly cast to an Int, which can then be auto-cast to a Float during the multiplication step
Float f = 9.0 / 10.0 * b
x = 9.0 / 10.0 * (b as Int)
 
; 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">
; x gets true
; b gets true
x = 1 as bool
Bool b = 1 as bool
</source>
</source>
<br>
<br>
<source lang="papyrus">
<source lang="papyrus">
; x gets false
; b gets false
x = "" as bool
Bool b = "" as bool
</source>
</source>
<br>
<br>
<source lang="papyrus">
<source lang="papyrus">
; x gets true
; b gets true
x = new int[5] as bool
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 Type)
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))
Anonymous user