gdscript.lyx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  1. #LyX 2.0 created this file. For more info see http://www.lyx.org/
  2. \lyxformat 413
  3. \begin_document
  4. \begin_header
  5. \textclass article
  6. \use_default_options true
  7. \maintain_unincluded_children false
  8. \language english
  9. \language_package default
  10. \inputencoding auto
  11. \fontencoding global
  12. \font_roman default
  13. \font_sans default
  14. \font_typewriter default
  15. \font_default_family default
  16. \use_non_tex_fonts false
  17. \font_sc false
  18. \font_osf false
  19. \font_sf_scale 100
  20. \font_tt_scale 100
  21. \graphics default
  22. \default_output_format default
  23. \output_sync 0
  24. \bibtex_command default
  25. \index_command default
  26. \paperfontsize default
  27. \use_hyperref false
  28. \papersize default
  29. \use_geometry false
  30. \use_amsmath 1
  31. \use_esint 1
  32. \use_mhchem 1
  33. \use_mathdots 1
  34. \cite_engine basic
  35. \use_bibtopic false
  36. \use_indices false
  37. \paperorientation portrait
  38. \suppress_date false
  39. \use_refstyle 0
  40. \index Index
  41. \shortcut idx
  42. \color #008000
  43. \end_index
  44. \secnumdepth 3
  45. \tocdepth 3
  46. \paragraph_separation indent
  47. \paragraph_indentation default
  48. \quotes_language english
  49. \papercolumns 1
  50. \papersides 1
  51. \paperpagestyle default
  52. \tracking_changes false
  53. \output_changes false
  54. \html_math_output 0
  55. \html_css_as_file 0
  56. \html_be_strict false
  57. \end_header
  58. \begin_body
  59. \begin_layout Title
  60. GD Scripting Language (GDScript)
  61. \end_layout
  62. \begin_layout Section
  63. Introduction
  64. \end_layout
  65. \begin_layout Standard
  66. GDScript is a high level, dynamically typed programming language used to
  67. create content.
  68. It uses a syntax that is very similar to the Python language (blocks are
  69. indent-based) and it's goal is to be very optimal and tigthly integrated
  70. with the engine, allowing great flexibility for content creation and integratio
  71. n.
  72. \end_layout
  73. \begin_layout Section
  74. Example
  75. \end_layout
  76. \begin_layout Standard
  77. Some people can learn better by just taking a look at the syntax, so here's
  78. a simple example of how it looks.
  79. \end_layout
  80. \begin_layout Standard
  81. \begin_inset listings
  82. inline false
  83. status open
  84. \begin_layout Plain Layout
  85. #a file is a class!
  86. \end_layout
  87. \begin_layout Plain Layout
  88. \end_layout
  89. \begin_layout Plain Layout
  90. #inheritance
  91. \end_layout
  92. \begin_layout Plain Layout
  93. extends BaseClass
  94. \end_layout
  95. \begin_layout Plain Layout
  96. \end_layout
  97. \begin_layout Plain Layout
  98. #member variables
  99. \end_layout
  100. \begin_layout Plain Layout
  101. \end_layout
  102. \begin_layout Plain Layout
  103. var a=5
  104. \end_layout
  105. \begin_layout Plain Layout
  106. var s="Hello"
  107. \end_layout
  108. \begin_layout Plain Layout
  109. var arr=[1,2,3]
  110. \end_layout
  111. \begin_layout Plain Layout
  112. var dict={"key":"value", 2:3}
  113. \end_layout
  114. \begin_layout Plain Layout
  115. \end_layout
  116. \begin_layout Plain Layout
  117. #constants
  118. \end_layout
  119. \begin_layout Plain Layout
  120. \end_layout
  121. \begin_layout Plain Layout
  122. const answer=42
  123. \end_layout
  124. \begin_layout Plain Layout
  125. const thename="Charly"
  126. \end_layout
  127. \begin_layout Plain Layout
  128. \end_layout
  129. \begin_layout Plain Layout
  130. #built-in vector types
  131. \end_layout
  132. \begin_layout Plain Layout
  133. \end_layout
  134. \begin_layout Plain Layout
  135. var v2 = Vector2(1,2)
  136. \end_layout
  137. \begin_layout Plain Layout
  138. var v3 = Vector3(1,2,3)
  139. \end_layout
  140. \begin_layout Plain Layout
  141. \end_layout
  142. \begin_layout Plain Layout
  143. #function
  144. \end_layout
  145. \begin_layout Plain Layout
  146. \end_layout
  147. \begin_layout Plain Layout
  148. func some_function(param1,param2):
  149. \end_layout
  150. \begin_layout Plain Layout
  151. var local_var=5
  152. \end_layout
  153. \begin_layout Plain Layout
  154. \end_layout
  155. \begin_layout Plain Layout
  156. if param1 < local_var:
  157. \end_layout
  158. \begin_layout Plain Layout
  159. print(param1)
  160. \end_layout
  161. \begin_layout Plain Layout
  162. elif param2 > 5:
  163. \end_layout
  164. \begin_layout Plain Layout
  165. print(param2)
  166. \end_layout
  167. \begin_layout Plain Layout
  168. else:
  169. \end_layout
  170. \begin_layout Plain Layout
  171. print("fail!")
  172. \end_layout
  173. \begin_layout Plain Layout
  174. \end_layout
  175. \begin_layout Plain Layout
  176. for i in range(20):
  177. \end_layout
  178. \begin_layout Plain Layout
  179. print(i)
  180. \end_layout
  181. \begin_layout Plain Layout
  182. \end_layout
  183. \begin_layout Plain Layout
  184. while(param2!=0):
  185. \end_layout
  186. \begin_layout Plain Layout
  187. param2-=1
  188. \end_layout
  189. \begin_layout Plain Layout
  190. \end_layout
  191. \begin_layout Plain Layout
  192. var local_var2 = param1+3
  193. \end_layout
  194. \begin_layout Plain Layout
  195. return local_var2
  196. \end_layout
  197. \begin_layout Plain Layout
  198. \end_layout
  199. \begin_layout Plain Layout
  200. \end_layout
  201. \begin_layout Plain Layout
  202. #subclass
  203. \end_layout
  204. \begin_layout Plain Layout
  205. \end_layout
  206. \begin_layout Plain Layout
  207. class Something:
  208. \end_layout
  209. \begin_layout Plain Layout
  210. var a=10
  211. \end_layout
  212. \begin_layout Plain Layout
  213. \end_layout
  214. \begin_layout Plain Layout
  215. #constructor
  216. \end_layout
  217. \begin_layout Plain Layout
  218. \end_layout
  219. \begin_layout Plain Layout
  220. func _init():
  221. \end_layout
  222. \begin_layout Plain Layout
  223. print("constructed!")
  224. \end_layout
  225. \begin_layout Plain Layout
  226. var lv = Something.new()
  227. \end_layout
  228. \begin_layout Plain Layout
  229. print(lv.a)
  230. \end_layout
  231. \begin_layout Plain Layout
  232. \end_layout
  233. \begin_layout Plain Layout
  234. \end_layout
  235. \end_inset
  236. \end_layout
  237. \begin_layout Section
  238. Language
  239. \end_layout
  240. \begin_layout Subsection
  241. Identifiers
  242. \end_layout
  243. \begin_layout Standard
  244. Any string that restricts itself to alphabetic characters ('a' to 'z' and
  245. 'A' to 'Z'), digits ('0' to '9') and '_' qualifies as an identifier.
  246. As an extra restriction, identifiers must not begin with a digit.
  247. Identifiers are case-sensitive ('foo' is different to 'FOO').
  248. \end_layout
  249. \begin_layout Subsection
  250. Keywords
  251. \end_layout
  252. \begin_layout Standard
  253. The following is the list of keywords supported by the language.
  254. Since keywords are reserved words (tokens), they can't be used as identifiers.
  255. \end_layout
  256. \begin_layout Subsection
  257. Operators
  258. \end_layout
  259. \begin_layout Standard
  260. The following is the list of supported operators and their precedence (TODO,
  261. change since this was made to reflect python operators)
  262. \end_layout
  263. \begin_layout Standard
  264. \begin_inset Tabular
  265. <lyxtabular version="3" rows="18" columns="2">
  266. <features tabularvalignment="middle">
  267. <column alignment="center" valignment="top" width="0">
  268. <column alignment="center" valignment="top" width="0">
  269. <row>
  270. <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
  271. \begin_inset Text
  272. \begin_layout Plain Layout
  273. Operator
  274. \end_layout
  275. \end_inset
  276. </cell>
  277. <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
  278. \begin_inset Text
  279. \begin_layout Plain Layout
  280. Note
  281. \end_layout
  282. \end_inset
  283. </cell>
  284. </row>
  285. <row>
  286. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  287. \begin_inset Text
  288. \begin_layout Plain Layout
  289. x[index]
  290. \end_layout
  291. \end_inset
  292. </cell>
  293. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  294. \begin_inset Text
  295. \begin_layout Plain Layout
  296. Subscription, Highest Priority
  297. \end_layout
  298. \end_inset
  299. </cell>
  300. </row>
  301. <row>
  302. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  303. \begin_inset Text
  304. \begin_layout Plain Layout
  305. x.attribute
  306. \end_layout
  307. \end_inset
  308. </cell>
  309. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  310. \begin_inset Text
  311. \begin_layout Plain Layout
  312. Attribute Reference
  313. \end_layout
  314. \end_inset
  315. </cell>
  316. </row>
  317. <row>
  318. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  319. \begin_inset Text
  320. \begin_layout Plain Layout
  321. extends
  322. \end_layout
  323. \end_inset
  324. </cell>
  325. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  326. \begin_inset Text
  327. \begin_layout Plain Layout
  328. Instance Type Checker
  329. \end_layout
  330. \end_inset
  331. </cell>
  332. </row>
  333. <row>
  334. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  335. \begin_inset Text
  336. \begin_layout Plain Layout
  337. ~
  338. \end_layout
  339. \end_inset
  340. </cell>
  341. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  342. \begin_inset Text
  343. \begin_layout Plain Layout
  344. Bitwise NOT
  345. \end_layout
  346. \end_inset
  347. </cell>
  348. </row>
  349. <row>
  350. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  351. \begin_inset Text
  352. \begin_layout Plain Layout
  353. -x
  354. \end_layout
  355. \end_inset
  356. </cell>
  357. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  358. \begin_inset Text
  359. \begin_layout Plain Layout
  360. Negative
  361. \end_layout
  362. \end_inset
  363. </cell>
  364. </row>
  365. <row>
  366. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  367. \begin_inset Text
  368. \begin_layout Plain Layout
  369. * / %
  370. \end_layout
  371. \end_inset
  372. </cell>
  373. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  374. \begin_inset Text
  375. \begin_layout Plain Layout
  376. Mult / Div / Remainder
  377. \end_layout
  378. \end_inset
  379. </cell>
  380. </row>
  381. <row>
  382. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  383. \begin_inset Text
  384. \begin_layout Plain Layout
  385. + -
  386. \end_layout
  387. \end_inset
  388. </cell>
  389. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  390. \begin_inset Text
  391. \begin_layout Plain Layout
  392. Addition / Substraction
  393. \end_layout
  394. \end_inset
  395. </cell>
  396. </row>
  397. <row>
  398. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  399. \begin_inset Text
  400. \begin_layout Plain Layout
  401. << >>
  402. \end_layout
  403. \end_inset
  404. </cell>
  405. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  406. \begin_inset Text
  407. \begin_layout Plain Layout
  408. Bit Shifting
  409. \end_layout
  410. \end_inset
  411. </cell>
  412. </row>
  413. <row>
  414. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  415. \begin_inset Text
  416. \begin_layout Plain Layout
  417. &
  418. \end_layout
  419. \end_inset
  420. </cell>
  421. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  422. \begin_inset Text
  423. \begin_layout Plain Layout
  424. Bitwise AND
  425. \end_layout
  426. \end_inset
  427. </cell>
  428. </row>
  429. <row>
  430. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  431. \begin_inset Text
  432. \begin_layout Plain Layout
  433. ^
  434. \end_layout
  435. \end_inset
  436. </cell>
  437. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  438. \begin_inset Text
  439. \begin_layout Plain Layout
  440. Bitwise XOR
  441. \end_layout
  442. \end_inset
  443. </cell>
  444. </row>
  445. <row>
  446. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  447. \begin_inset Text
  448. \begin_layout Plain Layout
  449. |
  450. \end_layout
  451. \end_inset
  452. </cell>
  453. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  454. \begin_inset Text
  455. \begin_layout Plain Layout
  456. Bitwise OR
  457. \end_layout
  458. \end_inset
  459. </cell>
  460. </row>
  461. <row>
  462. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  463. \begin_inset Text
  464. \begin_layout Plain Layout
  465. < > == != >= <=
  466. \end_layout
  467. \end_inset
  468. </cell>
  469. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  470. \begin_inset Text
  471. \begin_layout Plain Layout
  472. Comparisons
  473. \end_layout
  474. \end_inset
  475. </cell>
  476. </row>
  477. <row>
  478. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  479. \begin_inset Text
  480. \begin_layout Plain Layout
  481. in
  482. \end_layout
  483. \end_inset
  484. </cell>
  485. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  486. \begin_inset Text
  487. \begin_layout Plain Layout
  488. Content Test
  489. \end_layout
  490. \end_inset
  491. </cell>
  492. </row>
  493. <row>
  494. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  495. \begin_inset Text
  496. \begin_layout Plain Layout
  497. ! not
  498. \end_layout
  499. \end_inset
  500. </cell>
  501. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  502. \begin_inset Text
  503. \begin_layout Plain Layout
  504. Boolean NOT
  505. \end_layout
  506. \end_inset
  507. </cell>
  508. </row>
  509. <row>
  510. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  511. \begin_inset Text
  512. \begin_layout Plain Layout
  513. and &&
  514. \end_layout
  515. \end_inset
  516. </cell>
  517. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  518. \begin_inset Text
  519. \begin_layout Plain Layout
  520. Boolean AND
  521. \end_layout
  522. \end_inset
  523. </cell>
  524. </row>
  525. <row>
  526. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  527. \begin_inset Text
  528. \begin_layout Plain Layout
  529. or ||
  530. \end_layout
  531. \end_inset
  532. </cell>
  533. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  534. \begin_inset Text
  535. \begin_layout Plain Layout
  536. Boolean OR
  537. \end_layout
  538. \end_inset
  539. </cell>
  540. </row>
  541. <row>
  542. <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
  543. \begin_inset Text
  544. \begin_layout Plain Layout
  545. = += -= *= /= ^= &= |=
  546. \end_layout
  547. \end_inset
  548. </cell>
  549. <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
  550. \begin_inset Text
  551. \begin_layout Plain Layout
  552. Assignment, Lowest Priority
  553. \end_layout
  554. \end_inset
  555. </cell>
  556. </row>
  557. </lyxtabular>
  558. \end_inset
  559. \end_layout
  560. \begin_layout Subsection
  561. Literals
  562. \end_layout
  563. \begin_layout Standard
  564. \begin_inset Tabular
  565. <lyxtabular version="3" rows="6" columns="2">
  566. <features tabularvalignment="middle">
  567. <column alignment="center" valignment="top" width="0">
  568. <column alignment="center" valignment="top" width="0">
  569. <row>
  570. <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
  571. \begin_inset Text
  572. \begin_layout Plain Layout
  573. Literal
  574. \end_layout
  575. \end_inset
  576. </cell>
  577. <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
  578. \begin_inset Text
  579. \begin_layout Plain Layout
  580. Name
  581. \end_layout
  582. \end_inset
  583. </cell>
  584. </row>
  585. <row>
  586. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  587. \begin_inset Text
  588. \begin_layout Plain Layout
  589. 45
  590. \end_layout
  591. \end_inset
  592. </cell>
  593. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  594. \begin_inset Text
  595. \begin_layout Plain Layout
  596. Base 10 Integer
  597. \end_layout
  598. \end_inset
  599. </cell>
  600. </row>
  601. <row>
  602. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  603. \begin_inset Text
  604. \begin_layout Plain Layout
  605. 0x8F51
  606. \end_layout
  607. \end_inset
  608. </cell>
  609. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  610. \begin_inset Text
  611. \begin_layout Plain Layout
  612. Base 16 (hex) Integer
  613. \end_layout
  614. \end_inset
  615. </cell>
  616. </row>
  617. <row>
  618. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  619. \begin_inset Text
  620. \begin_layout Plain Layout
  621. 3.14, 58.1e-10
  622. \end_layout
  623. \end_inset
  624. </cell>
  625. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  626. \begin_inset Text
  627. \begin_layout Plain Layout
  628. Floating Point Number (real)
  629. \end_layout
  630. \end_inset
  631. </cell>
  632. </row>
  633. <row>
  634. <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
  635. \begin_inset Text
  636. \begin_layout Plain Layout
  637. 'Hello',
  638. \begin_inset Quotes eld
  639. \end_inset
  640. Hi
  641. \begin_inset Quotes erd
  642. \end_inset
  643. \end_layout
  644. \end_inset
  645. </cell>
  646. <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
  647. \begin_inset Text
  648. \begin_layout Plain Layout
  649. Strings
  650. \end_layout
  651. \end_inset
  652. </cell>
  653. </row>
  654. <row>
  655. <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
  656. \begin_inset Text
  657. \begin_layout Plain Layout
  658. @'Hello', @'Hi'
  659. \end_layout
  660. \end_inset
  661. </cell>
  662. <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
  663. \begin_inset Text
  664. \begin_layout Plain Layout
  665. Internationalized Strings
  666. \end_layout
  667. \end_inset
  668. </cell>
  669. </row>
  670. </lyxtabular>
  671. \end_inset
  672. \end_layout
  673. \begin_layout Subsection
  674. Comments
  675. \end_layout
  676. \begin_layout Standard
  677. Anything from a '#' to the end of the line is ignored and is considered
  678. a comment.
  679. \end_layout
  680. \begin_layout Standard
  681. \begin_inset listings
  682. lstparams "language=Python"
  683. inline false
  684. status open
  685. \begin_layout Plain Layout
  686. # This is a comment
  687. \end_layout
  688. \end_inset
  689. \end_layout
  690. \begin_layout Section
  691. Built-in Types
  692. \end_layout
  693. \begin_layout Subsection
  694. Basic Bult-In Types
  695. \end_layout
  696. \begin_layout Standard
  697. A variable in GDScript can be assigned many of several built-in types.
  698. \end_layout
  699. \begin_layout Subsubsection
  700. null
  701. \end_layout
  702. \begin_layout Standard
  703. 'null' is a data type that contains no information, nothing assigned, and
  704. it's just empy.
  705. It can only be set to one value: 'null'.
  706. \end_layout
  707. \begin_layout Subsubsection
  708. bool
  709. \end_layout
  710. \begin_layout Standard
  711. Boolean data type, can only contain 'true' or 'false'.
  712. \end_layout
  713. \begin_layout Subsubsection
  714. int
  715. \end_layout
  716. \begin_layout Standard
  717. Integer data type, can only contain integer numbers, negative and positive.
  718. \end_layout
  719. \begin_layout Subsubsection
  720. float
  721. \end_layout
  722. \begin_layout Standard
  723. contains a floating point value (real).
  724. \end_layout
  725. \begin_layout Subsubsection
  726. String
  727. \end_layout
  728. \begin_layout Standard
  729. Sequence of characters in unicode format.
  730. Strings can contain the standard C escape sequences.
  731. \end_layout
  732. \begin_layout Subsection
  733. Vector Built-In Types
  734. \end_layout
  735. \begin_layout Subsubsection
  736. Vector2/Size2
  737. \end_layout
  738. \begin_layout Standard
  739. 2D vector type, containing x and y fields.
  740. Can alternatively access fields as width and height for readability.
  741. Can also be accessed as array.
  742. \end_layout
  743. \begin_layout Subsubsection
  744. Rect2
  745. \end_layout
  746. \begin_layout Standard
  747. 2D Rectangle type.
  748. Contains 2 vectors fields,
  749. \begin_inset Quotes eld
  750. \end_inset
  751. pos
  752. \begin_inset Quotes erd
  753. \end_inset
  754. and size
  755. \begin_inset Quotes erd
  756. \end_inset
  757. .
  758. Alternatively contains an
  759. \begin_inset Quotes eld
  760. \end_inset
  761. end
  762. \begin_inset Quotes erd
  763. \end_inset
  764. field which is
  765. \begin_inset Quotes eld
  766. \end_inset
  767. pos+size
  768. \begin_inset Quotes erd
  769. \end_inset
  770. .
  771. \end_layout
  772. \begin_layout Subsubsection
  773. Vector3
  774. \end_layout
  775. \begin_layout Standard
  776. 3D vector type.
  777. Contains x, y and z fields.
  778. Can also be accessed as array.
  779. \end_layout
  780. \begin_layout Subsubsection
  781. Matrix32
  782. \end_layout
  783. \begin_layout Standard
  784. 3x2 matrix used for 2D transforms.
  785. \end_layout
  786. \begin_layout Subsubsection
  787. Plane
  788. \end_layout
  789. \begin_layout Standard
  790. 3D Plane type in normalized form.
  791. Contains a
  792. \begin_inset Quotes eld
  793. \end_inset
  794. normal
  795. \begin_inset Quotes erd
  796. \end_inset
  797. vector field and a
  798. \begin_inset Quotes eld
  799. \end_inset
  800. d
  801. \begin_inset Quotes erd
  802. \end_inset
  803. scalar distance.
  804. \end_layout
  805. \begin_layout Subsubsection
  806. Quat
  807. \end_layout
  808. \begin_layout Standard
  809. Quaternion, datatype used for representing a 3D rotation.
  810. It's useful for interpolating rotations.
  811. \end_layout
  812. \begin_layout Subsubsection
  813. AABB/Box3
  814. \end_layout
  815. \begin_layout Standard
  816. Axis Aligned bounding box (or alternatively, 3D box).
  817. Contains 2 vectors fields,
  818. \begin_inset Quotes eld
  819. \end_inset
  820. pos
  821. \begin_inset Quotes erd
  822. \end_inset
  823. and size
  824. \begin_inset Quotes erd
  825. \end_inset
  826. .
  827. Alternatively contains an
  828. \begin_inset Quotes eld
  829. \end_inset
  830. end
  831. \begin_inset Quotes erd
  832. \end_inset
  833. field which is
  834. \begin_inset Quotes eld
  835. \end_inset
  836. pos+size
  837. \begin_inset Quotes erd
  838. \end_inset
  839. .
  840. \end_layout
  841. \begin_layout Subsubsection
  842. Matrix3
  843. \end_layout
  844. \begin_layout Standard
  845. 3x3 matrix used for 3D rotation and scale.
  846. Contains 3 vector fields x,y and z.
  847. Can also be accessed as array of 3D vectors.
  848. \end_layout
  849. \begin_layout Subsubsection
  850. Transform
  851. \end_layout
  852. \begin_layout Standard
  853. 3D Transform, contains a Matrix3 field
  854. \begin_inset Quotes eld
  855. \end_inset
  856. basis
  857. \begin_inset Quotes erd
  858. \end_inset
  859. and a Vector3 field
  860. \begin_inset Quotes eld
  861. \end_inset
  862. origin
  863. \begin_inset Quotes erd
  864. \end_inset
  865. .
  866. \end_layout
  867. \begin_layout Subsection
  868. Engine Built-In Types
  869. \end_layout
  870. \begin_layout Subsubsection
  871. Color
  872. \end_layout
  873. \begin_layout Standard
  874. Color datatype, contains r,g,b,a fields.
  875. Can also be accessed as h,s,v for hue/saturation/value.
  876. \end_layout
  877. \begin_layout Subsubsection
  878. Image
  879. \end_layout
  880. \begin_layout Standard
  881. Contains a 2D Image of custom format and allows direct access to the pixels.
  882. \end_layout
  883. \begin_layout Subsubsection
  884. NodePath
  885. \end_layout
  886. \begin_layout Standard
  887. Compiled path to a node, used mainly in the scene system.
  888. Can be easily asigned from/to a String.
  889. \end_layout
  890. \begin_layout Subsubsection
  891. RID
  892. \end_layout
  893. \begin_layout Standard
  894. Resource ID (RID).
  895. Servers use generic RIDs to reference opaque data.
  896. \end_layout
  897. \begin_layout Subsubsection
  898. Object
  899. \end_layout
  900. \begin_layout Standard
  901. Base class for anything not a built-in type.
  902. \end_layout
  903. \begin_layout Subsubsection
  904. InputEvent
  905. \end_layout
  906. \begin_layout Standard
  907. Events from input devices are contained in very compact form in InputEvent
  908. objects.
  909. Due to fact they can be received in high amounts from frame to frame, they
  910. are optimized in their own datatype.
  911. \end_layout
  912. \begin_layout Subsection
  913. Container Built-In Types
  914. \end_layout
  915. \begin_layout Subsubsection
  916. Array
  917. \end_layout
  918. \begin_layout Standard
  919. Generic sequence of objects.
  920. It's size can be changed to anything and starts from index 0.
  921. \end_layout
  922. \begin_layout Standard
  923. \begin_inset listings
  924. inline false
  925. status open
  926. \begin_layout Plain Layout
  927. var arr=[]
  928. \end_layout
  929. \begin_layout Plain Layout
  930. arr=[1,2,3]
  931. \end_layout
  932. \begin_layout Plain Layout
  933. arr[0]="Hi!"
  934. \end_layout
  935. \end_inset
  936. \end_layout
  937. \begin_layout Standard
  938. Arrays are allocated linearly in memory, so they are fast, but very large
  939. arrays (more than tens of thousands of elements) may cause fragmentation.
  940. There are specialized arrays for some built-in datatypes which do not suffer
  941. from this and use much less memory.
  942. \end_layout
  943. \begin_layout Subsubsection
  944. Dictionary
  945. \end_layout
  946. \begin_layout Standard
  947. Associative container which contains values referenced by unique keys.
  948. \end_layout
  949. \begin_layout Standard
  950. \begin_inset listings
  951. inline false
  952. status open
  953. \begin_layout Plain Layout
  954. var dict={4:5, "a key":"a value", 28:[1,2,3]}
  955. \end_layout
  956. \begin_layout Plain Layout
  957. dict["Hi!"]=0
  958. \end_layout
  959. \end_inset
  960. \end_layout
  961. \begin_layout Subsubsection
  962. ByteArray
  963. \end_layout
  964. \begin_layout Standard
  965. Array of bytes.
  966. Can only contains bytes (integers from 0 to 255).
  967. Optimized for memory usage, can't fragment the memory.
  968. \end_layout
  969. \begin_layout Subsubsection
  970. IntArray
  971. \end_layout
  972. \begin_layout Standard
  973. Array of integers.
  974. Can only contain integers.
  975. Optimized for memory usage, can't fragment the memory.
  976. \end_layout
  977. \begin_layout Subsubsection
  978. FloatArray
  979. \end_layout
  980. \begin_layout Standard
  981. Array of floats, can only contain floats.
  982. Optimized for memory usage, can't fragment the memory.
  983. \end_layout
  984. \begin_layout Subsubsection
  985. StringArray
  986. \end_layout
  987. \begin_layout Standard
  988. Array of strings, can only contain strings.
  989. Optimized for memory usage, can't fragment the memory.
  990. \end_layout
  991. \begin_layout Subsubsection
  992. Vector2Array
  993. \end_layout
  994. \begin_layout Standard
  995. Array of Vector2, can only contain 2D Vectors.
  996. Optimized for memory usage, can't fragment the memory.
  997. \end_layout
  998. \begin_layout Subsubsection
  999. Vector3Array
  1000. \end_layout
  1001. \begin_layout Standard
  1002. Array of Vector3, can only contain 3D Vectors.
  1003. Optimized for memory usage, can't fragment the memory.
  1004. \end_layout
  1005. \begin_layout Subsubsection
  1006. ColorArray
  1007. \end_layout
  1008. \begin_layout Standard
  1009. Array of Color, can only contains colors.
  1010. Optimized for memory usage, can't fragment the memory.
  1011. \end_layout
  1012. \begin_layout Section
  1013. Data
  1014. \end_layout
  1015. \begin_layout Subsection
  1016. Variables
  1017. \end_layout
  1018. \begin_layout Standard
  1019. Variables can exist as class members or local to functions.
  1020. They are created with the
  1021. \begin_inset Quotes eld
  1022. \end_inset
  1023. var
  1024. \begin_inset Quotes erd
  1025. \end_inset
  1026. keyword and may be, optionally, be assigned a value upon initialization.
  1027. \end_layout
  1028. \begin_layout Standard
  1029. \begin_inset listings
  1030. inline false
  1031. status open
  1032. \begin_layout Plain Layout
  1033. var a # datatype is null by default
  1034. \end_layout
  1035. \begin_layout Plain Layout
  1036. var b = 5
  1037. \end_layout
  1038. \begin_layout Plain Layout
  1039. var c = 3.8
  1040. \end_layout
  1041. \begin_layout Plain Layout
  1042. var d = b+c # variables are always initialized in order
  1043. \end_layout
  1044. \end_inset
  1045. \end_layout
  1046. \begin_layout Subsection
  1047. Constants
  1048. \end_layout
  1049. \begin_layout Standard
  1050. Constants are similar to variables, but must be constants or constant expression
  1051. s and must be assigned on initialization.
  1052. \end_layout
  1053. \begin_layout Standard
  1054. \begin_inset listings
  1055. inline false
  1056. status open
  1057. \begin_layout Plain Layout
  1058. const a = 5
  1059. \end_layout
  1060. \begin_layout Plain Layout
  1061. const b = Vector2(20,20)
  1062. \end_layout
  1063. \begin_layout Plain Layout
  1064. const c = 10+20 # constant expression
  1065. \end_layout
  1066. \begin_layout Plain Layout
  1067. const d = Vector2(20,30).x # constant expression: 20
  1068. \end_layout
  1069. \begin_layout Plain Layout
  1070. const e = [1,2,3,4][0] # constant expression: 1
  1071. \end_layout
  1072. \begin_layout Plain Layout
  1073. const f = sin(20) # sin() can be used in constant expression
  1074. \end_layout
  1075. \begin_layout Plain Layout
  1076. const g = x+20 # invalid, not a constant expression!
  1077. \end_layout
  1078. \begin_layout Plain Layout
  1079. \end_layout
  1080. \end_inset
  1081. \end_layout
  1082. \begin_layout Subsection
  1083. Functions
  1084. \end_layout
  1085. \begin_layout Standard
  1086. Functions always belong to a class.
  1087. The scope priority for variable look-up is: local -> class member -> global.
  1088. \begin_inset Quotes eld
  1089. \end_inset
  1090. self
  1091. \begin_inset Quotes erd
  1092. \end_inset
  1093. is provided as an option for accessing class members but is not required
  1094. always (and must
  1095. \emph on
  1096. not
  1097. \emph default
  1098. be defined as first parameter, like in Python).
  1099. For performance reasons, functions are not considered class members, so
  1100. they can't be referenced directly.
  1101. A function can return at any point.
  1102. The default return value is null.
  1103. \end_layout
  1104. \begin_layout Standard
  1105. \begin_inset listings
  1106. inline false
  1107. status open
  1108. \begin_layout Plain Layout
  1109. func myfunction(a,b):
  1110. \end_layout
  1111. \begin_layout Plain Layout
  1112. print(a)
  1113. \end_layout
  1114. \begin_layout Plain Layout
  1115. print(b)
  1116. \end_layout
  1117. \begin_layout Plain Layout
  1118. return a+b # return is optional, otherwise null is returned
  1119. \end_layout
  1120. \end_inset
  1121. \end_layout
  1122. \begin_layout Subsubsection
  1123. Statements and Control Flow
  1124. \end_layout
  1125. \begin_layout Standard
  1126. Statements are standard, and can be assignments, function calls, control
  1127. flow structures, etc (see below).
  1128. \begin_inset Quotes eld
  1129. \end_inset
  1130. ;
  1131. \begin_inset Quotes erd
  1132. \end_inset
  1133. as separator is entirely optional.
  1134. \end_layout
  1135. \begin_layout Subsubsection
  1136. if/else/elif
  1137. \end_layout
  1138. \begin_layout Standard
  1139. Simple conditions are created by using the
  1140. \emph on
  1141. if/else/elif
  1142. \emph default
  1143. syntax.
  1144. Parenthesis around statements is allowed but not requiered.
  1145. Given the nature of the tab-based indentation, elif can be used instead
  1146. of else:/if: to mantain a level of indentation.
  1147. \end_layout
  1148. \begin_layout Standard
  1149. \begin_inset listings
  1150. inline false
  1151. status open
  1152. \begin_layout Plain Layout
  1153. if [expression]:
  1154. \end_layout
  1155. \begin_layout Plain Layout
  1156. statement(s)
  1157. \end_layout
  1158. \begin_layout Plain Layout
  1159. elif [expression]:
  1160. \end_layout
  1161. \begin_layout Plain Layout
  1162. statement(s)
  1163. \end_layout
  1164. \begin_layout Plain Layout
  1165. else:
  1166. \end_layout
  1167. \begin_layout Plain Layout
  1168. statement(s)
  1169. \end_layout
  1170. \end_inset
  1171. \end_layout
  1172. \begin_layout Subsubsection
  1173. while
  1174. \end_layout
  1175. \begin_layout Standard
  1176. Simple loops are created by using
  1177. \emph on
  1178. while
  1179. \emph default
  1180. syntax.
  1181. Loops can be broken using
  1182. \emph on
  1183. break
  1184. \emph default
  1185. , or continued using
  1186. \emph on
  1187. continue
  1188. \emph default
  1189. :
  1190. \end_layout
  1191. \begin_layout Standard
  1192. \begin_inset listings
  1193. inline false
  1194. status open
  1195. \begin_layout Plain Layout
  1196. while [expression]:
  1197. \end_layout
  1198. \begin_layout Plain Layout
  1199. statement(s)
  1200. \end_layout
  1201. \end_inset
  1202. \end_layout
  1203. \begin_layout Subsubsection
  1204. for
  1205. \end_layout
  1206. \begin_layout Standard
  1207. To iterate a range, array or table a
  1208. \emph on
  1209. for
  1210. \emph default
  1211. loop is used.
  1212. For loops store the index in the loop variable on each iteration.
  1213. \end_layout
  1214. \begin_layout Standard
  1215. \begin_inset listings
  1216. inline false
  1217. status open
  1218. \begin_layout Plain Layout
  1219. for i in [0,1,2]:
  1220. \end_layout
  1221. \begin_layout Plain Layout
  1222. statement # loop iterates 3 times, i being 0,1 and 2
  1223. \end_layout
  1224. \begin_layout Plain Layout
  1225. \end_layout
  1226. \begin_layout Plain Layout
  1227. var dict = {"a":0, "b":1, "c": 2}
  1228. \end_layout
  1229. \begin_layout Plain Layout
  1230. for i in dict:
  1231. \end_layout
  1232. \begin_layout Plain Layout
  1233. print(dict[i]) # loop iterates the keys, i being "a","b" and c".
  1234. It prints 0, 1 and 2.
  1235. \end_layout
  1236. \begin_layout Plain Layout
  1237. \end_layout
  1238. \begin_layout Plain Layout
  1239. for i in range(3):
  1240. \end_layout
  1241. \begin_layout Plain Layout
  1242. statement # similar to [0,1,2] but does not allocate an array
  1243. \end_layout
  1244. \begin_layout Plain Layout
  1245. \end_layout
  1246. \begin_layout Plain Layout
  1247. for i in range(1,3):
  1248. \end_layout
  1249. \begin_layout Plain Layout
  1250. statement # similar to [1,2] but does not allocate an array
  1251. \end_layout
  1252. \begin_layout Plain Layout
  1253. \end_layout
  1254. \begin_layout Plain Layout
  1255. for i in range(2,8,2):
  1256. \end_layout
  1257. \begin_layout Plain Layout
  1258. statement # similar to [2,4,6] but does not allocate an array
  1259. \end_layout
  1260. \begin_layout Plain Layout
  1261. \end_layout
  1262. \end_inset
  1263. \end_layout
  1264. \begin_layout Section
  1265. Classes
  1266. \end_layout
  1267. \begin_layout Standard
  1268. By default, the body of a script file is an unnamed class, and it can only
  1269. be referenced externally as a resource or file.
  1270. Class syntax is meant to be very compact and can only contain member variables
  1271. or functions.
  1272. Static functions are allowed, but not static members (in the spirit of
  1273. thread safety, since scripts can be initialized in separate threads without
  1274. the user knowing).
  1275. In the same way, member variables (including arrays and dictionaries) are
  1276. initialized every time an instance is created.
  1277. \end_layout
  1278. \begin_layout Subsection
  1279. Class File Example
  1280. \end_layout
  1281. \begin_layout Standard
  1282. Example of a class file, imagine it being stored in a file like myclass.gd.
  1283. \end_layout
  1284. \begin_layout Standard
  1285. \begin_inset listings
  1286. inline false
  1287. status open
  1288. \begin_layout Plain Layout
  1289. \end_layout
  1290. \begin_layout Plain Layout
  1291. var a=5
  1292. \end_layout
  1293. \begin_layout Plain Layout
  1294. \end_layout
  1295. \begin_layout Plain Layout
  1296. function print_value_of_a():
  1297. \end_layout
  1298. \begin_layout Plain Layout
  1299. print(a)
  1300. \end_layout
  1301. \begin_layout Plain Layout
  1302. \end_layout
  1303. \end_inset
  1304. \end_layout
  1305. \begin_layout Subsection
  1306. Inheritance
  1307. \end_layout
  1308. \begin_layout Standard
  1309. A class-file can inherit from a global class, another file or a subclass
  1310. inside another file.
  1311. Multiple inheritance is not allowed.
  1312. The
  1313. \begin_inset Quotes eld
  1314. \end_inset
  1315. extends
  1316. \begin_inset Quotes erd
  1317. \end_inset
  1318. syntax is used:
  1319. \end_layout
  1320. \begin_layout Standard
  1321. \begin_inset listings
  1322. inline false
  1323. status open
  1324. \begin_layout Plain Layout
  1325. # extend from some class (global)
  1326. \end_layout
  1327. \begin_layout Plain Layout
  1328. extends SomeClass
  1329. \end_layout
  1330. \begin_layout Plain Layout
  1331. \end_layout
  1332. \begin_layout Plain Layout
  1333. # optionally, extend from another file
  1334. \end_layout
  1335. \begin_layout Plain Layout
  1336. extends "somefile.gd"
  1337. \end_layout
  1338. \begin_layout Plain Layout
  1339. \end_layout
  1340. \begin_layout Plain Layout
  1341. # extend from a subclass in another file
  1342. \end_layout
  1343. \begin_layout Plain Layout
  1344. extends "somefile.gd".Subclass
  1345. \end_layout
  1346. \end_inset
  1347. \end_layout
  1348. \begin_layout Subsection
  1349. Inheritance Testing
  1350. \end_layout
  1351. \begin_layout Standard
  1352. It is possible to check if an instance inherits from a given class.
  1353. For this the
  1354. \begin_inset Quotes eld
  1355. \end_inset
  1356. extends
  1357. \begin_inset Quotes erd
  1358. \end_inset
  1359. keyword can be used as an operator instead:
  1360. \end_layout
  1361. \begin_layout Standard
  1362. \begin_inset listings
  1363. inline false
  1364. status open
  1365. \begin_layout Plain Layout
  1366. static var enemy_class = preload("enemy.gd") # cache the enemy class
  1367. \end_layout
  1368. \begin_layout Plain Layout
  1369. \end_layout
  1370. \begin_layout Plain Layout
  1371. [..]
  1372. \end_layout
  1373. \begin_layout Plain Layout
  1374. \end_layout
  1375. \begin_layout Plain Layout
  1376. if ( entity extends enemy_class ):
  1377. \end_layout
  1378. \begin_layout Plain Layout
  1379. entity.apply_damage()
  1380. \end_layout
  1381. \begin_layout Plain Layout
  1382. \end_layout
  1383. \begin_layout Plain Layout
  1384. \end_layout
  1385. \begin_layout Plain Layout
  1386. \end_layout
  1387. \end_inset
  1388. \end_layout
  1389. \begin_layout Subsection
  1390. Constructor
  1391. \end_layout
  1392. \begin_layout Standard
  1393. A class can have an optional constructor, a function named
  1394. \begin_inset Quotes eld
  1395. \end_inset
  1396. _init
  1397. \begin_inset Quotes erd
  1398. \end_inset
  1399. that is called when the class is instanced.
  1400. \end_layout
  1401. \begin_layout Subsection
  1402. Sub Classes
  1403. \end_layout
  1404. \begin_layout Standard
  1405. A class file can have subclasses.
  1406. Syntax should be straightforward:
  1407. \end_layout
  1408. \begin_layout Standard
  1409. \begin_inset listings
  1410. inline false
  1411. status open
  1412. \begin_layout Plain Layout
  1413. class SomeSubClass:
  1414. \end_layout
  1415. \begin_layout Plain Layout
  1416. var a=5
  1417. \end_layout
  1418. \begin_layout Plain Layout
  1419. function print_value_of_a():
  1420. \end_layout
  1421. \begin_layout Plain Layout
  1422. print(a)
  1423. \end_layout
  1424. \begin_layout Plain Layout
  1425. \end_layout
  1426. \begin_layout Plain Layout
  1427. function _init():
  1428. \end_layout
  1429. \begin_layout Plain Layout
  1430. var sc = SomeSubClass.new() #instance by calling built-in new
  1431. \end_layout
  1432. \begin_layout Plain Layout
  1433. sc.print_value_of_a()
  1434. \end_layout
  1435. \end_inset
  1436. \end_layout
  1437. \begin_layout Subsection
  1438. Classes as Objects
  1439. \end_layout
  1440. \begin_layout Standard
  1441. It may be desired at some point to load a class from a file and then instance
  1442. it.
  1443. Since the global scope does not exist, classes must be loaded as a resource.
  1444. Instancing is done by calling the
  1445. \begin_inset Quotes eld
  1446. \end_inset
  1447. new
  1448. \begin_inset Quotes erd
  1449. \end_inset
  1450. function in a class object:
  1451. \end_layout
  1452. \begin_layout Standard
  1453. \begin_inset listings
  1454. inline false
  1455. status open
  1456. \begin_layout Plain Layout
  1457. #load the class (loaded every time the script is instanced)
  1458. \end_layout
  1459. \begin_layout Plain Layout
  1460. var MyClass = load("myclass.gd")
  1461. \end_layout
  1462. \begin_layout Plain Layout
  1463. \end_layout
  1464. \begin_layout Plain Layout
  1465. #alternatively, using the preload() function preloads the class at compile
  1466. time
  1467. \end_layout
  1468. \begin_layout Plain Layout
  1469. var MyClass2 = preload("myclass.gd")
  1470. \end_layout
  1471. \begin_layout Plain Layout
  1472. \end_layout
  1473. \begin_layout Plain Layout
  1474. function _init():
  1475. \end_layout
  1476. \begin_layout Plain Layout
  1477. var a = MyClass.new()
  1478. \end_layout
  1479. \begin_layout Plain Layout
  1480. a.somefunction()
  1481. \end_layout
  1482. \end_inset
  1483. \end_layout
  1484. \begin_layout Subsection
  1485. Exports
  1486. \end_layout
  1487. \begin_layout Standard
  1488. Class members can be exported.
  1489. This means their value gets saved along with a scene.
  1490. If class members have initializers to constant expressions, they will be
  1491. available for editing in the property editor.
  1492. Exporting is done by using the export keyword:
  1493. \end_layout
  1494. \begin_layout Standard
  1495. \begin_inset listings
  1496. inline false
  1497. status open
  1498. \begin_layout Plain Layout
  1499. extends Button
  1500. \end_layout
  1501. \begin_layout Plain Layout
  1502. \end_layout
  1503. \begin_layout Plain Layout
  1504. export var data # value will be saved
  1505. \end_layout
  1506. \begin_layout Plain Layout
  1507. export var number=5 # also available to the property editor
  1508. \end_layout
  1509. \begin_layout Plain Layout
  1510. \end_layout
  1511. \end_inset
  1512. \end_layout
  1513. \begin_layout Standard
  1514. One of the fundamental benefits of exporting member variables is to have
  1515. them visible in the property editor.
  1516. This way artists and game designers can modify values that later influence
  1517. how the program runs.
  1518. For this, a special export syntax is provided for more detail in the exported
  1519. variables:
  1520. \end_layout
  1521. \begin_layout Standard
  1522. \begin_inset listings
  1523. inline false
  1524. status open
  1525. \begin_layout Plain Layout
  1526. #if the exported value assigns a constant or constant expression, the type
  1527. will be infered and used in the editor
  1528. \end_layout
  1529. \begin_layout Plain Layout
  1530. \end_layout
  1531. \begin_layout Plain Layout
  1532. export var number=5
  1533. \end_layout
  1534. \begin_layout Plain Layout
  1535. \end_layout
  1536. \begin_layout Plain Layout
  1537. #export can take a basic datatype as argument, which will be used in the
  1538. editor
  1539. \end_layout
  1540. \begin_layout Plain Layout
  1541. \end_layout
  1542. \begin_layout Plain Layout
  1543. export(int) var number
  1544. \end_layout
  1545. \begin_layout Plain Layout
  1546. \end_layout
  1547. \begin_layout Plain Layout
  1548. #export can also take a resource type as hint
  1549. \end_layout
  1550. \begin_layout Plain Layout
  1551. \end_layout
  1552. \begin_layout Plain Layout
  1553. export(Texture) var character_face
  1554. \end_layout
  1555. \begin_layout Plain Layout
  1556. \end_layout
  1557. \begin_layout Plain Layout
  1558. #integers and strings hint enumerated values
  1559. \end_layout
  1560. \begin_layout Plain Layout
  1561. \end_layout
  1562. \begin_layout Plain Layout
  1563. export(int,"Warrior","Magician","Thief") var character_class # (editor will
  1564. set them as 0,1 and 2)
  1565. \end_layout
  1566. \begin_layout Plain Layout
  1567. export(String,"Rebecca","Mary","Leah") var character_name
  1568. \end_layout
  1569. \begin_layout Plain Layout
  1570. \end_layout
  1571. \begin_layout Plain Layout
  1572. #strings as paths
  1573. \end_layout
  1574. \begin_layout Plain Layout
  1575. \end_layout
  1576. \begin_layout Plain Layout
  1577. export(String,FILE) var f # string is a path to a file
  1578. \end_layout
  1579. \begin_layout Plain Layout
  1580. export(String,DIR) var f # string is a path to a directory
  1581. \end_layout
  1582. \begin_layout Plain Layout
  1583. export(String,FILE,"*.txt") var f # string is a path to a file, custom filter
  1584. provided as hint
  1585. \end_layout
  1586. \begin_layout Plain Layout
  1587. \end_layout
  1588. \begin_layout Plain Layout
  1589. #integers and floats hint ranges
  1590. \end_layout
  1591. \begin_layout Plain Layout
  1592. \end_layout
  1593. \begin_layout Plain Layout
  1594. export(int,20) var i # 0 to 20 allowed
  1595. \end_layout
  1596. \begin_layout Plain Layout
  1597. export(int,-10,20) var j # -10 to 20 allowed
  1598. \end_layout
  1599. \begin_layout Plain Layout
  1600. export(float,-10,20,0.2) var k # -10 to 20 allowed, with stepping of 0.2
  1601. \end_layout
  1602. \begin_layout Plain Layout
  1603. \end_layout
  1604. \begin_layout Plain Layout
  1605. #color can hint availability of alpha
  1606. \end_layout
  1607. \begin_layout Plain Layout
  1608. \end_layout
  1609. \begin_layout Plain Layout
  1610. export(Color,RGB) var col # Color is RGB
  1611. \end_layout
  1612. \begin_layout Plain Layout
  1613. export(Color,RGBA) var col # Color is RGBA
  1614. \end_layout
  1615. \begin_layout Plain Layout
  1616. \end_layout
  1617. \begin_layout Plain Layout
  1618. \end_layout
  1619. \end_inset
  1620. \end_layout
  1621. \begin_layout Standard
  1622. It must be noted that even if the script is not being run while at the editor,
  1623. the exported properties are still editable (see below for
  1624. \begin_inset Quotes eld
  1625. \end_inset
  1626. tool
  1627. \begin_inset Quotes erd
  1628. \end_inset
  1629. ).
  1630. \end_layout
  1631. \begin_layout Subsection
  1632. Static Functions
  1633. \end_layout
  1634. \begin_layout Standard
  1635. A function can be declared static.
  1636. When static, it has no access to the instance member variables or
  1637. \begin_inset Quotes eld
  1638. \end_inset
  1639. self
  1640. \begin_inset Quotes erd
  1641. \end_inset
  1642. .
  1643. This is mainly useful to make libraries of helper functions:
  1644. \end_layout
  1645. \begin_layout Standard
  1646. \begin_inset listings
  1647. inline false
  1648. status open
  1649. \begin_layout Plain Layout
  1650. static func sum2(a,b):
  1651. \end_layout
  1652. \begin_layout Plain Layout
  1653. return a+b
  1654. \end_layout
  1655. \end_inset
  1656. \end_layout
  1657. \begin_layout Subsection
  1658. Asserting
  1659. \end_layout
  1660. \begin_layout Standard
  1661. It is possible to assert a condition, which will cause a debugger break
  1662. if false.
  1663. Just use the built-in 'assert' function.
  1664. \end_layout
  1665. \begin_layout Standard
  1666. \begin_inset listings
  1667. inline false
  1668. status open
  1669. \begin_layout Plain Layout
  1670. assert(a==2)
  1671. \end_layout
  1672. \begin_layout Plain Layout
  1673. # if a is not 2, it will generate a debugger break
  1674. \end_layout
  1675. \end_inset
  1676. \end_layout
  1677. \begin_layout Subsection
  1678. Tool Mode
  1679. \end_layout
  1680. \begin_layout Standard
  1681. Scripts by default don't run inside the editor, and only the exported properties
  1682. can be changed.
  1683. In some cases it is desired that they do (as long as they don't execute
  1684. game code or manually avoid doing so).
  1685. For this, the
  1686. \begin_inset Quotes eld
  1687. \end_inset
  1688. tool
  1689. \begin_inset Quotes erd
  1690. \end_inset
  1691. keyword exists, and must be placed at the top of the file:
  1692. \end_layout
  1693. \begin_layout Standard
  1694. \begin_inset listings
  1695. inline false
  1696. status open
  1697. \begin_layout Plain Layout
  1698. tool
  1699. \end_layout
  1700. \begin_layout Plain Layout
  1701. extends Button
  1702. \end_layout
  1703. \begin_layout Plain Layout
  1704. \end_layout
  1705. \begin_layout Plain Layout
  1706. func _init():
  1707. \end_layout
  1708. \begin_layout Plain Layout
  1709. print("Hello")
  1710. \end_layout
  1711. \end_inset
  1712. \end_layout
  1713. \begin_layout Subsection
  1714. Memory Management
  1715. \end_layout
  1716. \begin_layout Standard
  1717. If a class inherits from
  1718. \emph on
  1719. Reference
  1720. \emph default
  1721. , then instances will be freed when no longer in use.
  1722. No garbage collector exists, just simple reference counting.
  1723. By default, all classes that don't define inheritance extend
  1724. \emph on
  1725. Reference
  1726. \emph default
  1727. .
  1728. If this is not desired, then a class must inherit
  1729. \emph on
  1730. Object
  1731. \emph default
  1732. manually and must call instance.free().
  1733. To avoid reference cycles that can't be freed, a weakref() function is
  1734. provided for creating weak references.
  1735. \end_layout
  1736. \begin_layout Subsection
  1737. Function References
  1738. \end_layout
  1739. \begin_layout Standard
  1740. Functions can't be referenced because they are not treated as class members.
  1741. There are two alternatives to this, though.
  1742. The
  1743. \begin_inset Quotes eld
  1744. \end_inset
  1745. call
  1746. \begin_inset Quotes erd
  1747. \end_inset
  1748. function or the funcref() helper.
  1749. \end_layout
  1750. \begin_layout Standard
  1751. \begin_inset listings
  1752. inline false
  1753. status open
  1754. \begin_layout Plain Layout
  1755. instance.call("funcname",args) # call a function by bane
  1756. \end_layout
  1757. \begin_layout Plain Layout
  1758. \end_layout
  1759. \begin_layout Plain Layout
  1760. var fr = funcref(instance,"funcname") #create a function ref
  1761. \end_layout
  1762. \begin_layout Plain Layout
  1763. fr.exec(args)
  1764. \end_layout
  1765. \end_inset
  1766. \end_layout
  1767. \end_body
  1768. \end_document