Difference between revisions of "Cast Reference"

909 bytes added ,  14:45, 31 March 2018
m
→‎Examples: Cleaned up syntax a bit
imported>Kahmul
imported>Rasikko
m (→‎Examples: Cleaned up syntax a bit)
 
(4 intermediate revisions by 3 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">
; 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 46: Line 56:
'''Compiler auto-cast from''': Nothing
'''Compiler auto-cast from''': Nothing


Floats and strings 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)
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


Integers and strings 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)
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 133: Line 158:
== Cast to different types ==
== Cast to different types ==


Casting types to different ones might be useful i.e. if you want to know the type of the object triggered with your triggerzone. But, for example, you can't cast an ObjectReference to a Weapon or compare them. In this case you can work with the "Form"-Type.
Casting types to different ones might be useful i.e. if you want to know the type of the object triggered with your triggerzone. But, for example, you can't cast an ObjectReference to a Weapon or compare them. In this case you can work with the "Form"-type.


=== Examples ===
=== Examples ===
Line 139: 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 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))
   ;if the object type has our keyword, do something.
   ; if the object type has our keyword, do something.
endif
endif


Anonymous user