int.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="int" version="4.0">
  3. <brief_description>
  4. Integer built-in type.
  5. </brief_description>
  6. <description>
  7. Signed 64-bit integer type.
  8. It can take values in the interval [code][-2^63, 2^63 - 1][/code], i.e. [code][-9223372036854775808, 9223372036854775807][/code]. Exceeding those bounds will wrap around.
  9. [int] is a [Variant] type, and will thus be used when assigning an integer value to a [Variant]. It can also be enforced with the [code]: int[/code] type hint.
  10. [codeblocks]
  11. [gdscript]
  12. var my_variant = 0 # int, value 0.
  13. my_variant += 4.2 # float, value 4.2.
  14. var my_int: int = 1 # int, value 1.
  15. my_int = 4.2 # int, value 4, the right value is implicitly cast to int.
  16. my_int = int("6.7") # int, value 6, the String is explicitly cast with int.
  17. var max_int = 9223372036854775807
  18. print(max_int) # 9223372036854775807, OK.
  19. max_int += 1
  20. print(max_int) # -9223372036854775808, we overflowed and wrapped around.
  21. [/gdscript]
  22. [csharp]
  23. int myInt = (int)"6.7".ToFloat(); // int, value 6, the String is explicitly cast with int.
  24. // We have to use `long` here, because GDSript's `int`
  25. // is 64 bits long while C#'s `int` is only 32 bits.
  26. long maxInt = 9223372036854775807;
  27. GD.Print(maxInt); // 9223372036854775807, OK.
  28. maxInt++;
  29. GD.Print(maxInt); // -9223372036854775808, we overflowed and wrapped around.
  30. // Alternatively, if we used C#'s 32-bit `int` type, the maximum value is much smaller:
  31. int halfInt = 2147483647;
  32. GD.Print(halfInt); // 2147483647, OK.
  33. halfInt++;
  34. GD.Print(halfInt); // -2147483648, we overflowed and wrapped around.
  35. [/csharp]
  36. [/codeblocks]
  37. </description>
  38. <tutorials>
  39. </tutorials>
  40. <constructors>
  41. <constructor name="int">
  42. <return type="int" />
  43. <description>
  44. Constructs a default-initialized [int] set to [code]0[/code].
  45. </description>
  46. </constructor>
  47. <constructor name="int">
  48. <return type="int" />
  49. <argument index="0" name="from" type="int" />
  50. <description>
  51. Constructs an [int] as a copy of the given [int].
  52. </description>
  53. </constructor>
  54. <constructor name="int">
  55. <return type="int" />
  56. <argument index="0" name="from" type="bool" />
  57. <description>
  58. Cast a [bool] value to an integer value, [code]int(true)[/code] will be equals to 1 and [code]int(false)[/code] will be equals to 0.
  59. </description>
  60. </constructor>
  61. <constructor name="int">
  62. <return type="int" />
  63. <argument index="0" name="from" type="float" />
  64. <description>
  65. Cast a float value to an integer value, this method simply removes the number fractions (i.e. rounds [code]from[/code] towards zero), so for example [code]int(2.7)[/code] will be equals to 2, [code]int(0.1)[/code] will be equals to 0 and [code]int(-2.7)[/code] will be equals to -2. This operation is also called truncation.
  66. </description>
  67. </constructor>
  68. </constructors>
  69. <operators>
  70. <operator name="operator !=">
  71. <return type="bool" />
  72. <description>
  73. </description>
  74. </operator>
  75. <operator name="operator !=">
  76. <return type="bool" />
  77. <argument index="0" name="right" type="float" />
  78. <description>
  79. Returns [code]true[/code] if operands are different from each other.
  80. </description>
  81. </operator>
  82. <operator name="operator !=">
  83. <return type="bool" />
  84. <argument index="0" name="right" type="int" />
  85. <description>
  86. Returns [code]true[/code] if operands are different from each other.
  87. </description>
  88. </operator>
  89. <operator name="operator %">
  90. <return type="int" />
  91. <argument index="0" name="right" type="int" />
  92. <description>
  93. Returns the result of the modulo operator for two integers, i.e. the remainder after dividing both numbers.
  94. [codeblock]
  95. print(5 % 2) # 1
  96. print(12 % 4) # 0
  97. print(12 % 2) # 2
  98. [/codeblock]
  99. </description>
  100. </operator>
  101. <operator name="operator &amp;">
  102. <return type="int" />
  103. <argument index="0" name="right" type="int" />
  104. <description>
  105. Returns the result of bitwise [code]AND[/code] operation for two integers.
  106. [codeblock]
  107. print(3 &amp; 1) # 1
  108. print(11 &amp; 3) # 3
  109. [/codeblock]
  110. It's useful to retrieve binary flags from a variable.
  111. [codeblock]
  112. var flags = 5
  113. # Do something if the first bit is enabled.
  114. if flags &amp; 1:
  115. do_stuff()
  116. [/codeblock]
  117. </description>
  118. </operator>
  119. <operator name="operator *">
  120. <return type="Color" />
  121. <argument index="0" name="right" type="Color" />
  122. <description>
  123. </description>
  124. </operator>
  125. <operator name="operator *">
  126. <return type="Quaternion" />
  127. <argument index="0" name="right" type="Quaternion" />
  128. <description>
  129. </description>
  130. </operator>
  131. <operator name="operator *">
  132. <return type="int" />
  133. <argument index="0" name="right" type="int" />
  134. <description>
  135. Multiplies two [int]s.
  136. </description>
  137. </operator>
  138. <operator name="operator *">
  139. <return type="float" />
  140. <argument index="0" name="right" type="float" />
  141. <description>
  142. Multiplies an [int] and a [float]. The result is a [float].
  143. </description>
  144. </operator>
  145. <operator name="operator *">
  146. <return type="Vector2" />
  147. <argument index="0" name="right" type="Vector2" />
  148. <description>
  149. Multiplies each component of the [Vector2] by the given [int].
  150. [codeblock]
  151. print(2 * Vector2(1, 1)) # Vector2(2, 2)
  152. [/codeblock]
  153. </description>
  154. </operator>
  155. <operator name="operator *">
  156. <return type="Vector2i" />
  157. <argument index="0" name="right" type="Vector2i" />
  158. <description>
  159. Multiplies each component of the [Vector2i] by the given [int].
  160. </description>
  161. </operator>
  162. <operator name="operator *">
  163. <return type="Vector3" />
  164. <argument index="0" name="right" type="Vector3" />
  165. <description>
  166. Multiplies each component of the [Vector3] by the given [int].
  167. </description>
  168. </operator>
  169. <operator name="operator *">
  170. <return type="Vector3i" />
  171. <argument index="0" name="right" type="Vector3i" />
  172. <description>
  173. Multiplies each component of the [Vector3i] by the given [int].
  174. </description>
  175. </operator>
  176. <operator name="operator +">
  177. <return type="float" />
  178. <argument index="0" name="right" type="float" />
  179. <description>
  180. Adds an [int] and a [float]. The result is a [float].
  181. </description>
  182. </operator>
  183. <operator name="operator +">
  184. <return type="int" />
  185. <argument index="0" name="right" type="int" />
  186. <description>
  187. Adds two integers.
  188. </description>
  189. </operator>
  190. <operator name="operator -">
  191. <return type="float" />
  192. <argument index="0" name="right" type="float" />
  193. <description>
  194. Subtracts a [float] from an [int]. The result is a [float].
  195. </description>
  196. </operator>
  197. <operator name="operator -">
  198. <return type="int" />
  199. <argument index="0" name="right" type="int" />
  200. <description>
  201. Subtracts two integers.
  202. </description>
  203. </operator>
  204. <operator name="operator /">
  205. <return type="float" />
  206. <argument index="0" name="right" type="float" />
  207. <description>
  208. Divides an [int] by a [float]. The result is a [float].
  209. [codeblock]
  210. print(10 / 3.0) # 3.333...
  211. [/codeblock]
  212. </description>
  213. </operator>
  214. <operator name="operator /">
  215. <return type="int" />
  216. <argument index="0" name="right" type="int" />
  217. <description>
  218. Divides two integers. The decimal part of the result is discarded (truncated).
  219. [codeblock]
  220. print(10 / 2) # 5
  221. print(10 / 3) # 3
  222. [/codeblock]
  223. </description>
  224. </operator>
  225. <operator name="operator &lt;">
  226. <return type="bool" />
  227. <argument index="0" name="right" type="float" />
  228. <description>
  229. Returns [code]true[/code] if this [int] is less than the given [float].
  230. </description>
  231. </operator>
  232. <operator name="operator &lt;">
  233. <return type="bool" />
  234. <argument index="0" name="right" type="int" />
  235. <description>
  236. Returns [code]true[/code] the left integer is less than the right one.
  237. </description>
  238. </operator>
  239. <operator name="operator &lt;&lt;">
  240. <return type="int" />
  241. <argument index="0" name="right" type="int" />
  242. <description>
  243. Performs bitwise shift left operation on the integer. Effectively the same as multiplying by a power of 2.
  244. [codeblock]
  245. print(10 &lt;&lt; 1) # 20
  246. print(10 &lt;&lt; 4) # 160
  247. [/codeblock]
  248. </description>
  249. </operator>
  250. <operator name="operator &lt;=">
  251. <return type="bool" />
  252. <argument index="0" name="right" type="float" />
  253. <description>
  254. Returns [code]true[/code] if this [int] is less than or equal to the given [float].
  255. </description>
  256. </operator>
  257. <operator name="operator &lt;=">
  258. <return type="bool" />
  259. <argument index="0" name="right" type="int" />
  260. <description>
  261. Returns [code]true[/code] the left integer is less than or equal to the right one.
  262. </description>
  263. </operator>
  264. <operator name="operator ==">
  265. <return type="bool" />
  266. <description>
  267. </description>
  268. </operator>
  269. <operator name="operator ==">
  270. <return type="bool" />
  271. <argument index="0" name="right" type="float" />
  272. <description>
  273. Returns [code]true[/code] if the integer is equal to the given [float].
  274. </description>
  275. </operator>
  276. <operator name="operator ==">
  277. <return type="bool" />
  278. <argument index="0" name="right" type="int" />
  279. <description>
  280. Returns [code]true[/code] if both integers are equal.
  281. </description>
  282. </operator>
  283. <operator name="operator &gt;">
  284. <return type="bool" />
  285. <argument index="0" name="right" type="float" />
  286. <description>
  287. Returns [code]true[/code] if this [int] is greater than the given [float].
  288. </description>
  289. </operator>
  290. <operator name="operator &gt;">
  291. <return type="bool" />
  292. <argument index="0" name="right" type="int" />
  293. <description>
  294. Returns [code]true[/code] the left integer is greater than the right one.
  295. </description>
  296. </operator>
  297. <operator name="operator &gt;=">
  298. <return type="bool" />
  299. <argument index="0" name="right" type="float" />
  300. <description>
  301. Returns [code]true[/code] if this [int] is greater than or equal to the given [float].
  302. </description>
  303. </operator>
  304. <operator name="operator &gt;=">
  305. <return type="bool" />
  306. <argument index="0" name="right" type="int" />
  307. <description>
  308. Returns [code]true[/code] the left integer is greater than or equal to the right one.
  309. </description>
  310. </operator>
  311. <operator name="operator &gt;&gt;">
  312. <return type="int" />
  313. <argument index="0" name="right" type="int" />
  314. <description>
  315. Performs bitwise shift right operation on the integer. Effectively the same as dividing by a power of 2.
  316. [codeblock]
  317. print(10 &gt;&gt; 1) # 5
  318. print(10 &gt;&gt; 2) # 2
  319. [/codeblock]
  320. </description>
  321. </operator>
  322. <operator name="operator ^">
  323. <return type="int" />
  324. <argument index="0" name="right" type="int" />
  325. <description>
  326. Returns the result of bitwise [code]XOR[/code] operation for two integers.
  327. [codeblock]
  328. print(5 ^ 1) # 4
  329. print(4 ^ 7) # 3
  330. [/codeblock]
  331. </description>
  332. </operator>
  333. <operator name="operator unary+">
  334. <return type="int" />
  335. <description>
  336. 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.
  337. </description>
  338. </operator>
  339. <operator name="operator unary-">
  340. <return type="int" />
  341. <description>
  342. Returns the negated value of the [int]. If positive, turns the number negative. If negative, turns the number positive. If zero, does nothing.
  343. </description>
  344. </operator>
  345. <operator name="operator |">
  346. <return type="int" />
  347. <argument index="0" name="right" type="int" />
  348. <description>
  349. Returns the result of bitwise [code]OR[/code] operation for two integers.
  350. [codeblock]
  351. print(2 | 4) # 6
  352. print(1 | 3) # 3
  353. [/codeblock]
  354. It's useful to store binary flags in a variable.
  355. [codeblock]
  356. var flags = 0
  357. # Turn first and third bit on.
  358. flags |= 1
  359. flags |= 4
  360. [/codeblock]
  361. </description>
  362. </operator>
  363. <operator name="operator ~">
  364. <return type="int" />
  365. <description>
  366. Returns the result of bitwise [code]NOT[/code] operation for the integer. It's effectively equal to [code]-int + 1[/code].
  367. [codeblock]
  368. print(~4) # -3
  369. print(~7) # -6
  370. [/codeblock]
  371. </description>
  372. </operator>
  373. </operators>
  374. </class>