virtualx-engine/doc/classes/int.xml

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

402 lines
14 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="UTF-8" ?>
<class name="int" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
A built-in type for integers.
</brief_description>
<description>
2022-12-14 07:40:50 +01:00
Signed 64-bit integer type. This means that it can take values from [code]-2^63[/code] to [code]2^63 - 1[/code], i.e. from [code]-9223372036854775808[/code] to [code]9223372036854775807[/code]. When it exceeds these bounds, it will wrap around.
[int]s can be automatically converted to [float]s when necessary, for example when passing them as arguments in functions. The [float] will be as close to the original integer as possible.
Likewise, [float]s can be automatically converted into [int]s. This will truncate the [float], discarding anything after the floating point.
[b]Note:[/b] In a boolean context, an [int] will evaluate to [code]false[/code] if it equals [code]0[/code], and to [code]true[/code] otherwise.
[codeblocks]
[gdscript]
2022-12-14 07:40:50 +01:00
var x: int = 1 # x is 1
x = 4.2 # x is 4, because 4.2 gets truncated
var max_int = 9223372036854775807 # Biggest value an int can store
max_int += 1 # max_int is -9223372036854775808, because it wrapped around
[/gdscript]
[csharp]
2022-12-14 07:40:50 +01:00
int x = 1; // x is 1
x = 4.2; // x is 4, because 4.2 gets truncated
// We use long below, because GDScript's int is 64-bit while C#'s int is 32-bit.
long maxLong = 9223372036854775807; // Biggest value a long can store
maxLong++; // maxLong is now -9223372036854775808, because it wrapped around.
2022-12-14 07:40:50 +01:00
// Alternatively with C#'s 32-bit int type, which has a smaller maximum value.
int maxInt = 2147483647; // Biggest value an int can store
maxInt++; // maxInt is now -2147483648, because it wrapped around
[/csharp]
[/codeblocks]
2022-12-14 07:40:50 +01:00
In GDScript, you can use the [code]0b[/code] literal for binary representation, the [code]0x[/code] literal for hexadecimal representation, and the [code]_[/code] symbol to separate long numbers and improve readability.
[codeblock]
var x = 0b1001 # x is 9
var y = 0xF5 # y is 245
var z = 10_000_000 # z is 10000000
[/codeblock]
</description>
<tutorials>
</tutorials>
<constructors>
<constructor name="int">
<return type="int" />
<description>
2022-12-14 07:40:50 +01:00
Constructs an [int] set to [code]0[/code].
</description>
</constructor>
<constructor name="int">
<return type="int" />
<param index="0" name="from" type="int" />
<description>
Constructs an [int] as a copy of the given [int].
</description>
</constructor>
<constructor name="int">
<return type="int" />
<param index="0" name="from" type="String" />
<description>
2022-12-14 07:40:50 +01:00
Constructs a new [int] from a [String], following the same rules as [method String.to_int].
</description>
</constructor>
<constructor name="int">
<return type="int" />
<param index="0" name="from" type="bool" />
<description>
2022-12-14 07:40:50 +01:00
Constructs a new [int] from a [bool]. [code]true[/code] is converted to [code]1[/code] and [code]false[/code] is converted to [code]0[/code].
</description>
</constructor>
<constructor name="int">
<return type="int" />
<param index="0" name="from" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Constructs a new [int] from a [float]. This will truncate the [float], discarding anything after the floating point.
</description>
</constructor>
</constructors>
<operators>
<operator name="operator !=">
<return type="bool" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the [int] is not equivalent to the [float].
</description>
</operator>
<operator name="operator !=">
<return type="bool" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the [int]s are not equal.
</description>
</operator>
<operator name="operator %">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Returns the remainder after dividing two [int]s. Uses truncated division, which returns a negative number if the dividend is negative. If this is not desired, consider using [method @GlobalScope.posmod].
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(6 % 2) # Prints 0
print(11 % 4) # Prints 3
print(-5 % 3) # Prints -2
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator &amp;">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Performs the bitwise [code]AND[/code] operation.
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(0b1100 &amp; 0b1010) # Prints 8 (binary 1000)
2021-01-06 01:13:11 +01:00
[/codeblock]
2022-12-14 07:40:50 +01:00
This is useful for retrieving binary flags from a variable.
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
var flags = 0b101
# Check if the first or second bit are enabled.
if flags &amp; 0b011:
do_stuff() # This line will run.
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator *">
<return type="Color" />
<param index="0" name="right" type="Color" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies each component of the [Color] by the [int].
</description>
</operator>
<operator name="operator *">
<return type="Quaternion" />
<param index="0" name="right" type="Quaternion" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies each component of the [Quaternion] by the [int]. This operation is not meaningful on its own, but it can be used as a part of a larger expression.
</description>
</operator>
<operator name="operator *">
<return type="Vector2" />
<param index="0" name="right" type="Vector2" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies each component of the [Vector2] by the [int].
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(2 * Vector2(1, 4)) # Prints (2, 8)
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator *">
<return type="Vector2i" />
<param index="0" name="right" type="Vector2i" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies each component of the [Vector2i] by the [int].
</description>
</operator>
<operator name="operator *">
<return type="Vector3" />
<param index="0" name="right" type="Vector3" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies each component of the [Vector3] by the [int].
</description>
</operator>
<operator name="operator *">
<return type="Vector3i" />
<param index="0" name="right" type="Vector3i" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies each component of the [Vector3i] by the [int].
</description>
</operator>
<operator name="operator *">
<return type="Vector4" />
<param index="0" name="right" type="Vector4" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies each component of the [Vector4] by the [int].
</description>
</operator>
<operator name="operator *">
<return type="Vector4i" />
<param index="0" name="right" type="Vector4i" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies each component of the [Vector4i] by the [int].
</description>
</operator>
<operator name="operator *">
<return type="float" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies the [float] by the [int]. The result is a [float].
</description>
</operator>
<operator name="operator *">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Multiplies the two [int]s.
</description>
</operator>
<operator name="operator **">
<return type="float" />
<param index="0" name="right" type="float" />
<description>
Raises an [int] to a power of a [float]. The result is a [float].
[codeblock]
2022-12-14 07:40:50 +01:00
print(2 ** 0.5) # Prints 1.4142135623731
[/codeblock]
</description>
</operator>
<operator name="operator **">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Raises the left [int] to a power of the right [int].
[codeblock]
2022-12-14 07:40:50 +01:00
print(3 ** 4) # Prints 81
[/codeblock]
</description>
</operator>
<operator name="operator +">
<return type="float" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Adds the [int] and the [float]. The result is a [float].
</description>
</operator>
<operator name="operator +">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Adds the two [int]s.
</description>
</operator>
<operator name="operator -">
<return type="float" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Subtracts the [float] from the [int]. The result is a [float].
</description>
</operator>
<operator name="operator -">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Subtracts the two [int]s.
</description>
</operator>
<operator name="operator /">
<return type="float" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Divides the [int] by the [float]. The result is a [float].
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(10 / 3.0) # Prints 3.33333333333333
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator /">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Divides the two [int]s. The result is an [int]. This will truncate the [float], discarding anything after the floating point.
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(6 / 2) # Prints 3
print(5 / 3) # Prints 1
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator &lt;">
<return type="bool" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the [int] is less than the [float].
</description>
</operator>
<operator name="operator &lt;">
<return type="bool" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the left [int] is less than the right [int].
</description>
</operator>
<operator name="operator &lt;&lt;">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Performs the bitwise shift left operation. Effectively the same as multiplying by a power of 2.
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(0b1010 &lt;&lt; 1) # Prints 20 (binary 10100)
print(0b1010 &lt;&lt; 3) # Prints 80 (binary 1010000)
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator &lt;=">
<return type="bool" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the [int] is less than or equal to the [float].
</description>
</operator>
<operator name="operator &lt;=">
<return type="bool" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the left [int] is less than or equal to the right [int].
</description>
</operator>
<operator name="operator ==">
<return type="bool" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the [int] is equal to the [float].
</description>
</operator>
<operator name="operator ==">
<return type="bool" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the two [int]s are equal.
</description>
</operator>
<operator name="operator &gt;">
<return type="bool" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the [int] is greater than the [float].
</description>
</operator>
<operator name="operator &gt;">
<return type="bool" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the left [int] is greater than the right [int].
</description>
</operator>
<operator name="operator &gt;=">
<return type="bool" />
<param index="0" name="right" type="float" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the [int] is greater than or equal to the [float].
</description>
</operator>
<operator name="operator &gt;=">
<return type="bool" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Returns [code]true[/code] if the left [int] is greater than or equal to the right [int].
</description>
</operator>
<operator name="operator &gt;&gt;">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Performs the bitwise shift right operation. Effectively the same as dividing by a power of 2.
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(0b1010 &gt;&gt; 1) # Prints 5 (binary 101)
print(0b1010 &gt;&gt; 2) # Prints 2 (binary 10)
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator ^">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Performs the bitwise [code]XOR[/code] operation.
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(0b1100 ^ 0b1010) # Prints 6 (binary 110)
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator unary+">
<return type="int" />
<description>
2021-08-18 20:04:10 +02:00
Returns the same value as if the [code]+[/code] was not there. Unary [code]+[/code] does nothing, but sometimes it can make your code more readable.
</description>
</operator>
<operator name="operator unary-">
<return type="int" />
<description>
2021-08-18 20:04:10 +02:00
Returns the negated value of the [int]. If positive, turns the number negative. If negative, turns the number positive. If zero, does nothing.
</description>
</operator>
<operator name="operator |">
<return type="int" />
<param index="0" name="right" type="int" />
<description>
2022-12-14 07:40:50 +01:00
Performs the bitwise [code]OR[/code] operation.
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(0b1100 | 0b1010) # Prints 14 (binary 1110)
2021-01-06 01:13:11 +01:00
[/codeblock]
2022-12-14 07:40:50 +01:00
This is useful for storing binary flags in a variable.
2021-01-06 01:13:11 +01:00
[codeblock]
var flags = 0
2022-12-14 07:40:50 +01:00
flags |= 0b101 # Turn the first and third bits on.
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
<operator name="operator ~">
<return type="int" />
<description>
2022-12-14 07:40:50 +01:00
Performs the bitwise [code]NOT[/code] operation on the [int]. Due to [url=https://en.wikipedia.org/wiki/Two%27s_complement/]2's complement[/url], it's effectively equal to [code]-(int + 1)[/code].
2021-01-06 01:13:11 +01:00
[codeblock]
2022-12-14 07:40:50 +01:00
print(~4) # Prints -5
print(~(-7)) # Prints 6
2021-01-06 01:13:11 +01:00
[/codeblock]
</description>
</operator>
</operators>
</class>