squirrel.lyx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 1
  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. Squirrel Usage in Godot
  61. \end_layout
  62. \begin_layout Section
  63. Introduction
  64. \end_layout
  65. \begin_layout Standard
  66. Squirrel is a nice scripting language.
  67. It's sort of a mix between Lua, Java and JavaScript and ends up being easy
  68. to learn for most programmers.
  69. It has more language features than GDScript but it's also slower, more
  70. limited and not as well integrated.
  71. This guide will explain how Squirrel is integrated to Godot and all the
  72. quirks that are needed to know in order to use it properly.
  73. \end_layout
  74. \begin_layout Section
  75. Enabling Squirrel
  76. \end_layout
  77. \begin_layout Standard
  78. Squirrel may not be enabled by default in a Godot build.
  79. To enable it, execute SCons with the following parameters:
  80. \end_layout
  81. \begin_layout Standard
  82. \begin_inset listings
  83. inline false
  84. status open
  85. \begin_layout Plain Layout
  86. shell$ scons squirrel=yes <target_binary>
  87. \end_layout
  88. \end_inset
  89. \end_layout
  90. \begin_layout Section
  91. Documentation
  92. \end_layout
  93. \begin_layout Standard
  94. Godot utilizes Squirrel 2.2.
  95. Documentation can be found at:
  96. \begin_inset CommandInset href
  97. LatexCommand href
  98. target "http://squirrel-lang.org/#documentation"
  99. \end_inset
  100. \end_layout
  101. \begin_layout Section
  102. Class Files
  103. \end_layout
  104. \begin_layout Standard
  105. Unless writing a library, Godot expects a class for scripting an object.
  106. Since a Squirrel source file can contain many classes, the main class must
  107. be returned.
  108. The following is an example of extending a button:
  109. \end_layout
  110. \begin_layout Standard
  111. \begin_inset listings
  112. inline false
  113. status open
  114. \begin_layout Plain Layout
  115. class MyButton extends Button {
  116. \end_layout
  117. \begin_layout Plain Layout
  118. \end_layout
  119. \begin_layout Plain Layout
  120. \end_layout
  121. \begin_layout Plain Layout
  122. constructor() {
  123. \end_layout
  124. \begin_layout Plain Layout
  125. // ALWAYS call parent constructor
  126. \end_layout
  127. \begin_layout Plain Layout
  128. Button.constructor()
  129. \end_layout
  130. \begin_layout Plain Layout
  131. }
  132. \end_layout
  133. \begin_layout Plain Layout
  134. }
  135. \end_layout
  136. \begin_layout Plain Layout
  137. \end_layout
  138. \begin_layout Plain Layout
  139. return MyButton // main class returned
  140. \end_layout
  141. \end_inset
  142. \end_layout
  143. \begin_layout Standard
  144. Additionally, classes are all copied to the root table, so all class names
  145. in scripts must be different if they are attempted to be loaded simultaneously.
  146. The same can be said for any other globals declared in the script.
  147. \end_layout
  148. \begin_layout Standard
  149. Finally, squirrel scripts must be saved with the .nut or .sq extensions (both
  150. are recognized).
  151. \end_layout
  152. \begin_layout Section
  153. Including Other Scripts
  154. \end_layout
  155. \begin_layout Standard
  156. Other scripts can be included with the include() directive.
  157. Full and relative paths are supported.
  158. When included, the classes and globals are moved to the root table, so
  159. they become immediately available.
  160. Constants, however, are only inlined in the current class on load, so Squirrel
  161. does not make them available.
  162. Example of including scripts:
  163. \end_layout
  164. \begin_layout Standard
  165. \begin_inset listings
  166. inline false
  167. status open
  168. \begin_layout Plain Layout
  169. include("my_button.nut") # // relative to current script, expected to be
  170. in the same path
  171. \end_layout
  172. \begin_layout Plain Layout
  173. include("res://buttons/my_button.nut") // using resource path
  174. \end_layout
  175. \end_inset
  176. \end_layout
  177. \begin_layout Section
  178. Built-In Types
  179. \end_layout
  180. \begin_layout Standard
  181. There are some small differences between the Built-In types in Godot and
  182. the ones in Squirrel, so the documentation will not match.
  183. The differences are documented here.
  184. \end_layout
  185. \begin_layout Standard
  186. An attempt will be made to document everything here,but if in doubt about
  187. bindings on built-in types, you can always take a loot at the bindings
  188. source file in script/squirrel/sq_bind_types.cpp.
  189. \end_layout
  190. \begin_layout Standard
  191. Built-In Types in Squirrel are passed by reference (unlike by value like
  192. in GD).
  193. They also don't need to be freed.
  194. \end_layout
  195. \begin_layout Subsection
  196. AABB
  197. \end_layout
  198. \begin_layout Standard
  199. \begin_inset Quotes eld
  200. \end_inset
  201. pos
  202. \begin_inset Quotes erd
  203. \end_inset
  204. ,
  205. \begin_inset Quotes eld
  206. \end_inset
  207. size
  208. \begin_inset Quotes erd
  209. \end_inset
  210. and
  211. \begin_inset Quotes eld
  212. \end_inset
  213. end
  214. \begin_inset Quotes erd
  215. \end_inset
  216. are not available Use get_pos()/set_pos and get_size()/set_size().
  217. \end_layout
  218. \begin_layout Subsection
  219. InputEvent
  220. \end_layout
  221. \begin_layout Standard
  222. InputEvent is a single datatype and contains everything.
  223. Use only the fields meant for the event type:
  224. \end_layout
  225. \begin_layout Standard
  226. \begin_inset listings
  227. inline false
  228. status open
  229. \begin_layout Plain Layout
  230. //for mouse motion and button
  231. \end_layout
  232. \begin_layout Plain Layout
  233. int mouse_x
  234. \end_layout
  235. \begin_layout Plain Layout
  236. int mouse_y
  237. \end_layout
  238. \begin_layout Plain Layout
  239. int mouse_button_mask
  240. \end_layout
  241. \begin_layout Plain Layout
  242. int mouse_global_x
  243. \end_layout
  244. \begin_layout Plain Layout
  245. int mouse_global_y
  246. \end_layout
  247. \begin_layout Plain Layout
  248. \end_layout
  249. \begin_layout Plain Layout
  250. //for mouse button
  251. \end_layout
  252. \begin_layout Plain Layout
  253. \end_layout
  254. \begin_layout Plain Layout
  255. int mouse_button_index
  256. \end_layout
  257. \begin_layout Plain Layout
  258. bool mouse_button_pressed
  259. \end_layout
  260. \begin_layout Plain Layout
  261. bool mouse_button_doubleclick
  262. \end_layout
  263. \begin_layout Plain Layout
  264. \end_layout
  265. \begin_layout Plain Layout
  266. //for mouse motion
  267. \end_layout
  268. \begin_layout Plain Layout
  269. \end_layout
  270. \begin_layout Plain Layout
  271. int mouse_motion_x
  272. \end_layout
  273. \begin_layout Plain Layout
  274. int mouse_motion_y
  275. \end_layout
  276. \begin_layout Plain Layout
  277. \end_layout
  278. \begin_layout Plain Layout
  279. //for keyboard
  280. \end_layout
  281. \begin_layout Plain Layout
  282. \end_layout
  283. \begin_layout Plain Layout
  284. int key_scancode
  285. \end_layout
  286. \begin_layout Plain Layout
  287. int key_unicode
  288. \end_layout
  289. \begin_layout Plain Layout
  290. bool key_pressed
  291. \end_layout
  292. \begin_layout Plain Layout
  293. bool key_echo
  294. \end_layout
  295. \begin_layout Plain Layout
  296. \end_layout
  297. \begin_layout Plain Layout
  298. //for keyboard and mouse
  299. \end_layout
  300. \begin_layout Plain Layout
  301. \end_layout
  302. \begin_layout Plain Layout
  303. bool mod_alt
  304. \end_layout
  305. \begin_layout Plain Layout
  306. bool mod_shift
  307. \end_layout
  308. \begin_layout Plain Layout
  309. bool mod_meta
  310. \end_layout
  311. \begin_layout Plain Layout
  312. bool mod_control
  313. \end_layout
  314. \begin_layout Plain Layout
  315. \end_layout
  316. \begin_layout Plain Layout
  317. //joy button
  318. \end_layout
  319. \begin_layout Plain Layout
  320. \end_layout
  321. \begin_layout Plain Layout
  322. int joy_button_index
  323. \end_layout
  324. \begin_layout Plain Layout
  325. bool joy_button_pressed
  326. \end_layout
  327. \begin_layout Plain Layout
  328. \end_layout
  329. \begin_layout Plain Layout
  330. //joy axis
  331. \end_layout
  332. \begin_layout Plain Layout
  333. \end_layout
  334. \begin_layout Plain Layout
  335. int joy_axis
  336. \end_layout
  337. \begin_layout Plain Layout
  338. float joy_axis_value
  339. \end_layout
  340. \begin_layout Plain Layout
  341. \end_layout
  342. \begin_layout Plain Layout
  343. //screen drag and touch
  344. \end_layout
  345. \begin_layout Plain Layout
  346. \end_layout
  347. \begin_layout Plain Layout
  348. int screen_index
  349. \end_layout
  350. \begin_layout Plain Layout
  351. int screen_x
  352. \end_layout
  353. \begin_layout Plain Layout
  354. int screen_y
  355. \end_layout
  356. \begin_layout Plain Layout
  357. \end_layout
  358. \begin_layout Plain Layout
  359. //screen touch
  360. \end_layout
  361. \begin_layout Plain Layout
  362. \end_layout
  363. \begin_layout Plain Layout
  364. int screen_index
  365. \end_layout
  366. \begin_layout Plain Layout
  367. \end_layout
  368. \begin_layout Plain Layout
  369. //action
  370. \end_layout
  371. \begin_layout Plain Layout
  372. \end_layout
  373. \begin_layout Plain Layout
  374. int action_id
  375. \end_layout
  376. \begin_layout Plain Layout
  377. bool action_pressed
  378. \end_layout
  379. \begin_layout Plain Layout
  380. \end_layout
  381. \end_inset
  382. \end_layout
  383. \begin_layout Subsection
  384. Matrix3
  385. \end_layout
  386. \begin_layout Standard
  387. x,y,z member vectors are not available.
  388. Use get_row() and set_row() instead.
  389. Individual float values of the matrix are available as swizzle masks such
  390. as xxy, xyz, zzx, etc.
  391. \end_layout
  392. \begin_layout Standard
  393. Additional in-place versions of some functions are available: transpose(),
  394. invert(), rotate(), scale(), orthonormalize().
  395. \end_layout
  396. \begin_layout Subsection
  397. Transform
  398. \end_layout
  399. \begin_layout Standard
  400. \begin_inset Quotes eld
  401. \end_inset
  402. basis
  403. \begin_inset Quotes erd
  404. \end_inset
  405. and
  406. \begin_inset Quotes eld
  407. \end_inset
  408. origin
  409. \begin_inset Quotes erd
  410. \end_inset
  411. members are not available.
  412. Use get_basis()/set_basis() and get_origin()/set_origin() instead.
  413. Additional in-place versions of some functions are available: invert(),
  414. affine_invert(), orthonormalize(), rotate(), translate(), scale().
  415. \end_layout
  416. \begin_layout Standard
  417. Vector2
  418. \end_layout
  419. \begin_layout Subsection
  420. Plane
  421. \end_layout
  422. \begin_layout Standard
  423. \begin_inset Quotes eld
  424. \end_inset
  425. normal
  426. \begin_inset Quotes erd
  427. \end_inset
  428. member vector is not available.
  429. Use get_normal(), set_normal() instead.
  430. \end_layout
  431. \begin_layout Subsection
  432. Rect2
  433. \end_layout
  434. \begin_layout Standard
  435. \begin_inset Quotes eld
  436. \end_inset
  437. pos
  438. \begin_inset Quotes erd
  439. \end_inset
  440. ,
  441. \begin_inset Quotes eld
  442. \end_inset
  443. size
  444. \begin_inset Quotes erd
  445. \end_inset
  446. and
  447. \begin_inset Quotes eld
  448. \end_inset
  449. end
  450. \begin_inset Quotes erd
  451. \end_inset
  452. are not available Use get_pos()/set_pos and get_size()/set_size().
  453. \end_layout
  454. \begin_layout Subsection
  455. Native Arrays
  456. \end_layout
  457. \begin_layout Standard
  458. Native arrays such as RawArray, IntArray,StringArray, etc are not supported.
  459. Use regular squirrel arrays instead, since conversion to/from them will
  460. happen automatically.
  461. \end_layout
  462. \begin_layout Subsection
  463. Math Functions
  464. \end_layout
  465. \begin_layout Standard
  466. Math functions are inside the Math namespace in Squirrel.
  467. For example Math.sin , Math.PI, Math.atan2().
  468. \end_layout
  469. \begin_layout Subsection
  470. Native Types
  471. \end_layout
  472. \begin_layout Standard
  473. Array, Dictionary and NodePath are not available.
  474. Use a native array, table and string respectively.
  475. \end_layout
  476. \begin_layout Section
  477. _get , _set
  478. \end_layout
  479. \begin_layout Standard
  480. _get and _set are reserved in Squirrel, for overriding Godot Object property
  481. getter/setter, use _get_property and _set_property.
  482. \end_layout
  483. \begin_layout Section
  484. Member Export
  485. \end_layout
  486. \begin_layout Standard
  487. Simple exporting of members (so far only integer, floating point and string
  488. are supported) is supported by the @export extension.
  489. It is used like this:
  490. \end_layout
  491. \begin_layout Standard
  492. \begin_inset listings
  493. inline false
  494. status open
  495. \begin_layout Plain Layout
  496. class MyButton extends Button {
  497. \end_layout
  498. \begin_layout Plain Layout
  499. \end_layout
  500. \begin_layout Plain Layout
  501. aprop=1 // @export
  502. \end_layout
  503. \begin_layout Plain Layout
  504. bprop=2.0 // @export
  505. \end_layout
  506. \begin_layout Plain Layout
  507. cprop="3" // @export
  508. \end_layout
  509. \begin_layout Plain Layout
  510. \end_layout
  511. \begin_layout Plain Layout
  512. //these will be available to the property editor, and will be loaded/saved
  513. with the scene.
  514. \end_layout
  515. \begin_layout Plain Layout
  516. }
  517. \end_layout
  518. \end_inset
  519. \end_layout
  520. \begin_layout Section
  521. Always Enabled Scripts
  522. \end_layout
  523. \begin_layout Standard
  524. Scripts are not enabled in the editor by default.
  525. To enable a script always, add an @always_enabled comment.
  526. Example:
  527. \end_layout
  528. \begin_layout Standard
  529. \begin_inset listings
  530. inline false
  531. status open
  532. \begin_layout Plain Layout
  533. //@always_enabled
  534. \end_layout
  535. \begin_layout Plain Layout
  536. \end_layout
  537. \begin_layout Plain Layout
  538. class MyButton extends Button {
  539. \end_layout
  540. \begin_layout Plain Layout
  541. \end_layout
  542. \begin_layout Plain Layout
  543. ...
  544. \end_layout
  545. \end_inset
  546. \end_layout
  547. \begin_layout Section
  548. Threads
  549. \end_layout
  550. \begin_layout Standard
  551. Thread support in Squirrel is very poor.
  552. This is because of the stack-based nature of the language implementation.
  553. Since godot can run in multiple threads, it will forcibily lock the whole
  554. VM when accessed from multiple threads, which will result in degraded performan
  555. ce.
  556. Creating user threads in Squirrel is definitely not recomended, as it may
  557. completely lock the main thread.
  558. \end_layout
  559. \begin_layout Section
  560. References
  561. \end_layout
  562. \begin_layout Standard
  563. Godot has a built-in reference counted type used in conjunction with a template
  564. (objects that inherit the
  565. \begin_inset Quotes eld
  566. \end_inset
  567. Reference
  568. \begin_inset Quotes erd
  569. \end_inset
  570. class).
  571. Since Squirrel also uses reference counting, it becomes impossible for
  572. such types in godot to contain a script, because it would result in an
  573. un-breakable reference cycle.
  574. To avoid this, a Ref() class was created in Squirrel.
  575. \end_layout
  576. \begin_layout Standard
  577. When calling Godot API functions, returned references are wrapped inside
  578. Ref() transparently, but the problem arises when creating a Reference-derived
  579. object from the code.
  580. In such cases, the reference must be wrapped manually like this:
  581. \end_layout
  582. \begin_layout Standard
  583. \begin_inset listings
  584. inline false
  585. status open
  586. \begin_layout Plain Layout
  587. local f = Ref( File() )
  588. \end_layout
  589. \begin_layout Plain Layout
  590. local err = f.open("hello.txt",File.READ)
  591. \end_layout
  592. \end_inset
  593. \end_layout
  594. \begin_layout Standard
  595. Anything not a reference that inherits from Object can be freed manually
  596. by calling .free(), just like in GDScript.
  597. Object classes are in itself weak references to engine objects, and their
  598. validity can be checked by calling the
  599. \begin_inset Quotes eld
  600. \end_inset
  601. has_instance()
  602. \begin_inset Quotes erd
  603. \end_inset
  604. built-in method.
  605. \end_layout
  606. \begin_layout Section
  607. Unicode
  608. \end_layout
  609. \begin_layout Standard
  610. Squirrel source code is supposed to support Unicode, but the implementation
  611. is very broken (Squirrel attempts to use 16 bit chars no matter what, making
  612. it incompatible when the host OS is 32 bit, like Linux).
  613. Squirrel source code is parsed as UTF-8, and strings also contain UTF-8.
  614. Wide char access in strings is not supported.
  615. \end_layout
  616. \begin_layout Section
  617. Debugging
  618. \end_layout
  619. \begin_layout Standard
  620. Squirrel is well integrated into the Godot debugger.
  621. To run the project in debug mode, execute the godot binary with the -debug
  622. argument.
  623. Godot will break on squirrel errors and allow the programmer to debug.
  624. \end_layout
  625. \begin_layout Section
  626. Utility Functions
  627. \end_layout
  628. \begin_layout Standard
  629. There are a few squirrel-only utility functions available:
  630. \end_layout
  631. \begin_layout Subsection
  632. print(value[,value])
  633. \end_layout
  634. \begin_layout Standard
  635. Print stuff to stdout.
  636. \end_layout
  637. \begin_layout Subsection
  638. dofile(path)
  639. \end_layout
  640. \begin_layout Standard
  641. Execute a squirrel script file and return whatever the file returns.
  642. Not recommended to use in production because it can't be optimized.
  643. \end_layout
  644. \begin_layout Subsection
  645. nativeref(var)
  646. \end_layout
  647. \begin_layout Standard
  648. Convert any squirrel type to an engine type.
  649. When this type returns to squirrel, it's converted back.
  650. This is useful to add to Godot callbacks to ensure that the datatype is
  651. not converted.
  652. \end_layout
  653. \begin_layout Subsection
  654. unicode_split(string)
  655. \end_layout
  656. \begin_layout Standard
  657. Split an unicode string (utf8) into an array of widechars.
  658. Useful since there is no wide char access from Squirrel.
  659. \end_layout
  660. \begin_layout Subsection
  661. breakpoint()
  662. \end_layout
  663. \begin_layout Standard
  664. Stop the debugger when reaches here (when run inside the debugger).
  665. \end_layout
  666. \begin_layout Subsection
  667. backtrace()
  668. \end_layout
  669. \begin_layout Standard
  670. Print a backtrace of the call stack.
  671. \end_layout
  672. \begin_layout Subsection
  673. tr(text)
  674. \end_layout
  675. \begin_layout Standard
  676. Translate text (use string lookup in Godot translation system).
  677. \end_layout
  678. \begin_layout Subsection
  679. printerr(text)
  680. \end_layout
  681. \begin_layout Standard
  682. Print a string to stderr.
  683. \end_layout
  684. \end_body
  685. \end_document