0.2.0-dev Download ZIP
408 lines ·13.9 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="int" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | A built-in type for integers. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | 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. |
| 8 | [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. |
| 9 | Likewise, [float]s can be automatically converted into [int]s. This will truncate the [float], discarding anything after the floating-point. |
| 10 | [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. |
| 11 | [codeblocks] |
| 12 | [gdscript] |
| 13 | var x: int = 1 # x is 1 |
| 14 | x = 4.2 # x is 4, because 4.2 gets truncated |
| 15 | var max_int = 9223372036854775807 # Biggest value an int can store |
| 16 | max_int += 1 # max_int is -9223372036854775808, because it wrapped around |
| 17 | [/gdscript] |
| 18 | [csharp] |
| 19 | int x = 1; // x is 1 |
| 20 | x = (int)4.2; // x is 4, because 4.2 gets truncated |
| 21 | // We use long below, because GDScript's int is 64-bit while C#'s int is 32-bit. |
| 22 | long maxLong = 9223372036854775807; // Biggest value a long can store |
| 23 | maxLong++; // maxLong is now -9223372036854775808, because it wrapped around. |
| 24 | |
| 25 | // Alternatively with C#'s 32-bit int type, which has a smaller maximum value. |
| 26 | int maxInt = 2147483647; // Biggest value an int can store |
| 27 | maxInt++; // maxInt is now -2147483648, because it wrapped around |
| 28 | [/csharp] |
| 29 | [/codeblocks] |
| 30 | 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. |
| 31 | [codeblocks] |
| 32 | [gdscript] |
| 33 | var x = 0b1001 # x is 9 |
| 34 | var y = 0xF5 # y is 245 |
| 35 | var z = 10_000_000 # z is 10000000 |
| 36 | [/gdscript] |
| 37 | [csharp] |
| 38 | int x = 0b1001; // x is 9 |
| 39 | int y = 0xF5; // y is 245 |
| 40 | int z = 10_000_000; // z is 10000000 |
| 41 | [/csharp] |
| 42 | [/codeblocks] |
| 43 | </description> |
| 44 | <tutorials> |
| 45 | </tutorials> |
| 46 | <constructors> |
| 47 | <constructor name="int"> |
| 48 | <return type="int" /> |
| 49 | <description> |
| 50 | Constructs an [int] set to [code]0[/code]. |
| 51 | </description> |
| 52 | </constructor> |
| 53 | <constructor name="int"> |
| 54 | <return type="int" /> |
| 55 | <param index="0" name="from" type="int" /> |
| 56 | <description> |
| 57 | Constructs an [int] as a copy of the given [int]. |
| 58 | </description> |
| 59 | </constructor> |
| 60 | <constructor name="int"> |
| 61 | <return type="int" /> |
| 62 | <param index="0" name="from" type="String" /> |
| 63 | <description> |
| 64 | Constructs a new [int] from a [String], following the same rules as [method String.to_int]. |
| 65 | </description> |
| 66 | </constructor> |
| 67 | <constructor name="int"> |
| 68 | <return type="int" /> |
| 69 | <param index="0" name="from" type="bool" /> |
| 70 | <description> |
| 71 | 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]. |
| 72 | </description> |
| 73 | </constructor> |
| 74 | <constructor name="int"> |
| 75 | <return type="int" /> |
| 76 | <param index="0" name="from" type="float" /> |
| 77 | <description> |
| 78 | Constructs a new [int] from a [float]. This will truncate the [float], discarding anything after the floating point. |
| 79 | </description> |
| 80 | </constructor> |
| 81 | </constructors> |
| 82 | <operators> |
| 83 | <operator name="operator !="> |
| 84 | <return type="bool" /> |
| 85 | <param index="0" name="right" type="float" /> |
| 86 | <description> |
| 87 | Returns [code]true[/code] if the [int] is not equivalent to the [float]. |
| 88 | </description> |
| 89 | </operator> |
| 90 | <operator name="operator !="> |
| 91 | <return type="bool" /> |
| 92 | <param index="0" name="right" type="int" /> |
| 93 | <description> |
| 94 | Returns [code]true[/code] if the [int]s are not equal. |
| 95 | </description> |
| 96 | </operator> |
| 97 | <operator name="operator %"> |
| 98 | <return type="int" /> |
| 99 | <param index="0" name="right" type="int" /> |
| 100 | <description> |
| 101 | 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]. |
| 102 | [codeblock] |
| 103 | print(6 % 2) # Prints 0 |
| 104 | print(11 % 4) # Prints 3 |
| 105 | print(-5 % 3) # Prints -2 |
| 106 | [/codeblock] |
| 107 | </description> |
| 108 | </operator> |
| 109 | <operator name="operator &"> |
| 110 | <return type="int" /> |
| 111 | <param index="0" name="right" type="int" /> |
| 112 | <description> |
| 113 | Performs the bitwise [code]AND[/code] operation. |
| 114 | [codeblock] |
| 115 | print(0b1100 & 0b1010) # Prints 8 (binary 1000) |
| 116 | [/codeblock] |
| 117 | This is useful for retrieving binary flags from a variable. |
| 118 | [codeblock] |
| 119 | var flags = 0b101 |
| 120 | # Check if the first or second bit are enabled. |
| 121 | if flags & 0b011: |
| 122 | do_stuff() # This line will run. |
| 123 | [/codeblock] |
| 124 | </description> |
| 125 | </operator> |
| 126 | <operator name="operator *"> |
| 127 | <return type="Color" /> |
| 128 | <param index="0" name="right" type="Color" /> |
| 129 | <description> |
| 130 | Multiplies each component of the [Color] by the [int]. |
| 131 | </description> |
| 132 | </operator> |
| 133 | <operator name="operator *"> |
| 134 | <return type="Quaternion" /> |
| 135 | <param index="0" name="right" type="Quaternion" /> |
| 136 | <description> |
| 137 | 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. |
| 138 | </description> |
| 139 | </operator> |
| 140 | <operator name="operator *"> |
| 141 | <return type="Vector2" /> |
| 142 | <param index="0" name="right" type="Vector2" /> |
| 143 | <description> |
| 144 | Multiplies each component of the [Vector2] by the [int]. |
| 145 | [codeblock] |
| 146 | print(2 * Vector2(1, 4)) # Prints (2, 8) |
| 147 | [/codeblock] |
| 148 | </description> |
| 149 | </operator> |
| 150 | <operator name="operator *"> |
| 151 | <return type="Vector2i" /> |
| 152 | <param index="0" name="right" type="Vector2i" /> |
| 153 | <description> |
| 154 | Multiplies each component of the [Vector2i] by the [int]. |
| 155 | </description> |
| 156 | </operator> |
| 157 | <operator name="operator *"> |
| 158 | <return type="Vector3" /> |
| 159 | <param index="0" name="right" type="Vector3" /> |
| 160 | <description> |
| 161 | Multiplies each component of the [Vector3] by the [int]. |
| 162 | </description> |
| 163 | </operator> |
| 164 | <operator name="operator *"> |
| 165 | <return type="Vector3i" /> |
| 166 | <param index="0" name="right" type="Vector3i" /> |
| 167 | <description> |
| 168 | Multiplies each component of the [Vector3i] by the [int]. |
| 169 | </description> |
| 170 | </operator> |
| 171 | <operator name="operator *"> |
| 172 | <return type="Vector4" /> |
| 173 | <param index="0" name="right" type="Vector4" /> |
| 174 | <description> |
| 175 | Multiplies each component of the [Vector4] by the [int]. |
| 176 | </description> |
| 177 | </operator> |
| 178 | <operator name="operator *"> |
| 179 | <return type="Vector4i" /> |
| 180 | <param index="0" name="right" type="Vector4i" /> |
| 181 | <description> |
| 182 | Multiplies each component of the [Vector4i] by the [int]. |
| 183 | </description> |
| 184 | </operator> |
| 185 | <operator name="operator *"> |
| 186 | <return type="float" /> |
| 187 | <param index="0" name="right" type="float" /> |
| 188 | <description> |
| 189 | Multiplies the [float] by the [int]. The result is a [float]. |
| 190 | </description> |
| 191 | </operator> |
| 192 | <operator name="operator *"> |
| 193 | <return type="int" /> |
| 194 | <param index="0" name="right" type="int" /> |
| 195 | <description> |
| 196 | Multiplies the two [int]s. |
| 197 | </description> |
| 198 | </operator> |
| 199 | <operator name="operator **"> |
| 200 | <return type="float" /> |
| 201 | <param index="0" name="right" type="float" /> |
| 202 | <description> |
| 203 | Raises an [int] to a power of a [float]. The result is a [float]. |
| 204 | [codeblock] |
| 205 | print(2 ** 0.5) # Prints 1.4142135623731 |
| 206 | [/codeblock] |
| 207 | </description> |
| 208 | </operator> |
| 209 | <operator name="operator **"> |
| 210 | <return type="int" /> |
| 211 | <param index="0" name="right" type="int" /> |
| 212 | <description> |
| 213 | Raises the left [int] to a power of the right [int]. |
| 214 | [codeblock] |
| 215 | print(3 ** 4) # Prints 81 |
| 216 | [/codeblock] |
| 217 | </description> |
| 218 | </operator> |
| 219 | <operator name="operator +"> |
| 220 | <return type="float" /> |
| 221 | <param index="0" name="right" type="float" /> |
| 222 | <description> |
| 223 | Adds the [int] and the [float]. The result is a [float]. |
| 224 | </description> |
| 225 | </operator> |
| 226 | <operator name="operator +"> |
| 227 | <return type="int" /> |
| 228 | <param index="0" name="right" type="int" /> |
| 229 | <description> |
| 230 | Adds the two [int]s. |
| 231 | </description> |
| 232 | </operator> |
| 233 | <operator name="operator -"> |
| 234 | <return type="float" /> |
| 235 | <param index="0" name="right" type="float" /> |
| 236 | <description> |
| 237 | Subtracts the [float] from the [int]. The result is a [float]. |
| 238 | </description> |
| 239 | </operator> |
| 240 | <operator name="operator -"> |
| 241 | <return type="int" /> |
| 242 | <param index="0" name="right" type="int" /> |
| 243 | <description> |
| 244 | Subtracts the two [int]s. |
| 245 | </description> |
| 246 | </operator> |
| 247 | <operator name="operator /"> |
| 248 | <return type="float" /> |
| 249 | <param index="0" name="right" type="float" /> |
| 250 | <description> |
| 251 | Divides the [int] by the [float]. The result is a [float]. |
| 252 | [codeblock] |
| 253 | print(10 / 3.0) # Prints 3.33333333333333 |
| 254 | [/codeblock] |
| 255 | </description> |
| 256 | </operator> |
| 257 | <operator name="operator /"> |
| 258 | <return type="int" /> |
| 259 | <param index="0" name="right" type="int" /> |
| 260 | <description> |
| 261 | Divides the two [int]s. The result is an [int]. This will truncate the [float], discarding anything after the floating point. |
| 262 | [codeblock] |
| 263 | print(6 / 2) # Prints 3 |
| 264 | print(5 / 3) # Prints 1 |
| 265 | [/codeblock] |
| 266 | </description> |
| 267 | </operator> |
| 268 | <operator name="operator <"> |
| 269 | <return type="bool" /> |
| 270 | <param index="0" name="right" type="float" /> |
| 271 | <description> |
| 272 | Returns [code]true[/code] if the [int] is less than the [float]. |
| 273 | </description> |
| 274 | </operator> |
| 275 | <operator name="operator <"> |
| 276 | <return type="bool" /> |
| 277 | <param index="0" name="right" type="int" /> |
| 278 | <description> |
| 279 | Returns [code]true[/code] if the left [int] is less than the right [int]. |
| 280 | </description> |
| 281 | </operator> |
| 282 | <operator name="operator <<"> |
| 283 | <return type="int" /> |
| 284 | <param index="0" name="right" type="int" /> |
| 285 | <description> |
| 286 | Performs the bitwise shift left operation. Effectively the same as multiplying by a power of 2. |
| 287 | [codeblock] |
| 288 | print(0b1010 << 1) # Prints 20 (binary 10100) |
| 289 | print(0b1010 << 3) # Prints 80 (binary 1010000) |
| 290 | [/codeblock] |
| 291 | </description> |
| 292 | </operator> |
| 293 | <operator name="operator <="> |
| 294 | <return type="bool" /> |
| 295 | <param index="0" name="right" type="float" /> |
| 296 | <description> |
| 297 | Returns [code]true[/code] if the [int] is less than or equal to the [float]. |
| 298 | </description> |
| 299 | </operator> |
| 300 | <operator name="operator <="> |
| 301 | <return type="bool" /> |
| 302 | <param index="0" name="right" type="int" /> |
| 303 | <description> |
| 304 | Returns [code]true[/code] if the left [int] is less than or equal to the right [int]. |
| 305 | </description> |
| 306 | </operator> |
| 307 | <operator name="operator =="> |
| 308 | <return type="bool" /> |
| 309 | <param index="0" name="right" type="float" /> |
| 310 | <description> |
| 311 | Returns [code]true[/code] if the [int] is equal to the [float]. |
| 312 | </description> |
| 313 | </operator> |
| 314 | <operator name="operator =="> |
| 315 | <return type="bool" /> |
| 316 | <param index="0" name="right" type="int" /> |
| 317 | <description> |
| 318 | Returns [code]true[/code] if the two [int]s are equal. |
| 319 | </description> |
| 320 | </operator> |
| 321 | <operator name="operator >"> |
| 322 | <return type="bool" /> |
| 323 | <param index="0" name="right" type="float" /> |
| 324 | <description> |
| 325 | Returns [code]true[/code] if the [int] is greater than the [float]. |
| 326 | </description> |
| 327 | </operator> |
| 328 | <operator name="operator >"> |
| 329 | <return type="bool" /> |
| 330 | <param index="0" name="right" type="int" /> |
| 331 | <description> |
| 332 | Returns [code]true[/code] if the left [int] is greater than the right [int]. |
| 333 | </description> |
| 334 | </operator> |
| 335 | <operator name="operator >="> |
| 336 | <return type="bool" /> |
| 337 | <param index="0" name="right" type="float" /> |
| 338 | <description> |
| 339 | Returns [code]true[/code] if the [int] is greater than or equal to the [float]. |
| 340 | </description> |
| 341 | </operator> |
| 342 | <operator name="operator >="> |
| 343 | <return type="bool" /> |
| 344 | <param index="0" name="right" type="int" /> |
| 345 | <description> |
| 346 | Returns [code]true[/code] if the left [int] is greater than or equal to the right [int]. |
| 347 | </description> |
| 348 | </operator> |
| 349 | <operator name="operator >>"> |
| 350 | <return type="int" /> |
| 351 | <param index="0" name="right" type="int" /> |
| 352 | <description> |
| 353 | Performs the bitwise shift right operation. Effectively the same as dividing by a power of 2. |
| 354 | [codeblock] |
| 355 | print(0b1010 >> 1) # Prints 5 (binary 101) |
| 356 | print(0b1010 >> 2) # Prints 2 (binary 10) |
| 357 | [/codeblock] |
| 358 | </description> |
| 359 | </operator> |
| 360 | <operator name="operator ^"> |
| 361 | <return type="int" /> |
| 362 | <param index="0" name="right" type="int" /> |
| 363 | <description> |
| 364 | Performs the bitwise [code]XOR[/code] operation. |
| 365 | [codeblock] |
| 366 | print(0b1100 ^ 0b1010) # Prints 6 (binary 110) |
| 367 | [/codeblock] |
| 368 | </description> |
| 369 | </operator> |
| 370 | <operator name="operator unary+"> |
| 371 | <return type="int" /> |
| 372 | <description> |
| 373 | 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. |
| 374 | </description> |
| 375 | </operator> |
| 376 | <operator name="operator unary-"> |
| 377 | <return type="int" /> |
| 378 | <description> |
| 379 | Returns the negated value of the [int]. If positive, turns the number negative. If negative, turns the number positive. If zero, does nothing. |
| 380 | </description> |
| 381 | </operator> |
| 382 | <operator name="operator |"> |
| 383 | <return type="int" /> |
| 384 | <param index="0" name="right" type="int" /> |
| 385 | <description> |
| 386 | Performs the bitwise [code]OR[/code] operation. |
| 387 | [codeblock] |
| 388 | print(0b1100 | 0b1010) # Prints 14 (binary 1110) |
| 389 | [/codeblock] |
| 390 | This is useful for storing binary flags in a variable. |
| 391 | [codeblock] |
| 392 | var flags = 0 |
| 393 | flags |= 0b101 # Turn the first and third bits on. |
| 394 | [/codeblock] |
| 395 | </description> |
| 396 | </operator> |
| 397 | <operator name="operator ~"> |
| 398 | <return type="int" /> |
| 399 | <description> |
| 400 | 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]. |
| 401 | [codeblock] |
| 402 | print(~4) # Prints -5 |
| 403 | print(~(-7)) # Prints 6 |
| 404 | [/codeblock] |
| 405 | </description> |
| 406 | </operator> |
| 407 | </operators> |
| 408 | </class> |