Difference between revisions of "Cast Reference"

1,854 bytes added ,  14:45, 31 March 2018
m
→‎Examples: Cleaned up syntax a bit
imported>Jlundin
 
imported>Rasikko
m (→‎Examples: Cleaned up syntax a bit)
 
(5 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 130: Line 155:


Nothing can be cast to an array, even other arrays if the base types would normally cast just fine.
Nothing can be cast to an array, even other arrays if the base types would normally cast just fine.
== 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.
=== Examples ===
<source lang="papyrus">
Weapon Property MyUberWeapon auto
Event OnTriggerEnter(ObjectReference type)
Weapon triggeredWeapon = (type as form) as weapon  ; Cast the triggered object first to a form and then to a weapon.
if(triggeredWeapon == MyUberWeapon)
  ; if triggeredWeapon is equal to MyUberWeapon, do something.
endif
EndEvent
</source>
But if you only want to determine the type of the object, you could also use a keyword:
<source lang="papyrus">
Keyword Property IsAnArrow auto
Event OnTriggerEnter(ObjectReference Type)
if(type.haskeyword(IsAnArrow))
  ; if the object type has our keyword, do something.
endif
EndEvent
</source>


[[Category:Scripting]]
[[Category:Scripting]]
[[Category:Papyrus]]
[[Category:Papyrus]]
[[Category:Papyrus Language Reference]]
[[Category:Papyrus Language Reference]]
Anonymous user