html5_shell_classref.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. :article_outdated: True
  2. .. _doc_html5_shell_classref:
  3. HTML5 shell class reference
  4. ===========================
  5. Projects exported for the Web expose the :js:class:`Engine` class to the JavaScript environment, that allows
  6. fine control over the engine's start-up process.
  7. This API is built in an asynchronous manner and requires basic understanding
  8. of `Promises <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises>`__.
  9. Engine
  10. ------
  11. The ``Engine`` class provides methods for loading and starting exported projects on the Web. For default export
  12. settings, this is already part of the exported HTML page. To understand practical use of the ``Engine`` class,
  13. see :ref:`Custom HTML page for Web export <doc_customizing_html5_shell>`.
  14. Static Methods
  15. ^^^^^^^^^^^^^^
  16. +---------+-----------------------------------------------------------------------------------------------+
  17. | Promise | :js:attr:`load <Engine.load>` **(** string basePath **)** |
  18. +---------+-----------------------------------------------------------------------------------------------+
  19. | void | :js:attr:`unload <Engine.unload>` **(** **)** |
  20. +---------+-----------------------------------------------------------------------------------------------+
  21. | boolean | :js:attr:`isWebGLAvailable <Engine.isWebGLAvailable>` **(** *[ number majorVersion=1 ]* **)** |
  22. +---------+-----------------------------------------------------------------------------------------------+
  23. Instance Methods
  24. ^^^^^^^^^^^^^^^^
  25. +---------+---------------------------------------------------------------------------------------------------------------+
  26. | Promise | :js:attr:`init <Engine.prototype.init>` **(** *[ string basePath ]* **)** |
  27. +---------+---------------------------------------------------------------------------------------------------------------+
  28. | Promise | :js:attr:`preloadFile <Engine.prototype.preloadFile>` **(** string\|ArrayBuffer file *[, string path ]* **)** |
  29. +---------+---------------------------------------------------------------------------------------------------------------+
  30. | Promise | :js:attr:`start <Engine.prototype.start>` **(** EngineConfig override **)** |
  31. +---------+---------------------------------------------------------------------------------------------------------------+
  32. | Promise | :js:attr:`startGame <Engine.prototype.startGame>` **(** EngineConfig override **)** |
  33. +---------+---------------------------------------------------------------------------------------------------------------+
  34. | void | :js:attr:`copyToFS <Engine.prototype.copyToFS>` **(** string path, ArrayBuffer buffer **)** |
  35. +---------+---------------------------------------------------------------------------------------------------------------+
  36. | void | :js:attr:`requestQuit <Engine.prototype.requestQuit>` **(** **)** |
  37. +---------+---------------------------------------------------------------------------------------------------------------+
  38. .. js:class:: Engine( initConfig )
  39. Create a new Engine instance with the given configuration.
  40. :param EngineConfig initConfig:
  41. The initial config for this instance.
  42. **Static Methods**
  43. .. js:function:: load( basePath )
  44. Load the engine from the specified base path.
  45. :param string basePath:
  46. Base path of the engine to load.
  47. :return:
  48. A Promise that resolves once the engine is loaded.
  49. :rtype: Promise
  50. .. js:function:: unload( )
  51. Unload the engine to free memory.
  52. This method will be called automatically depending on the configuration. See :js:attr:`unloadAfterInit`.
  53. .. js:function:: isWebGLAvailable( [ majorVersion=1 ] )
  54. Check whether WebGL is available. Optionally, specify a particular version of WebGL to check for.
  55. :param number majorVersion:
  56. The major WebGL version to check for.
  57. :return:
  58. If the given major version of WebGL is available.
  59. :rtype: boolean
  60. **Instance Methods**
  61. .. js:function:: prototype.init( [ basePath ] )
  62. Initialize the engine instance. Optionally, pass the base path to the engine to load it,
  63. if it hasn't been loaded yet. See :js:meth:`Engine.load`.
  64. :param string basePath:
  65. Base path of the engine to load.
  66. :return:
  67. A ``Promise`` that resolves once the engine is loaded and initialized.
  68. :rtype: Promise
  69. .. js:function:: prototype.preloadFile( file [, path ] )
  70. Load a file so it is available in the instance's file system once it runs. Must be called **before** starting the
  71. instance.
  72. If not provided, the ``path`` is derived from the URL of the loaded file.
  73. :param string\|ArrayBuffer file:
  74. The file to preload.
  75. If a ``string`` the file will be loaded from that path.
  76. If an ``ArrayBuffer`` or a view on one, the buffer will used as the content of the file.
  77. :param string path:
  78. Path by which the file will be accessible. Required, if ``file`` is not a string.
  79. :return:
  80. A Promise that resolves once the file is loaded.
  81. :rtype: Promise
  82. .. js:function:: prototype.start( override )
  83. Start the engine instance using the given override configuration (if any).
  84. :js:meth:`startGame <Engine.prototype.startGame>` can be used in typical cases instead.
  85. This will initialize the instance if it is not initialized. For manual initialization, see :js:meth:`init <Engine.prototype.init>`.
  86. The engine must be loaded beforehand.
  87. Fails if a canvas cannot be found on the page, or not specified in the configuration.
  88. :param EngineConfig override:
  89. An optional configuration override.
  90. :return:
  91. Promise that resolves once the engine started.
  92. :rtype: Promise
  93. .. js:function:: prototype.startGame( override )
  94. Start the game instance using the given configuration override (if any).
  95. This will initialize the instance if it is not initialized. For manual initialization, see :js:meth:`init <Engine.prototype.init>`.
  96. This will load the engine if it is not loaded, and preload the main pck.
  97. This method expects the initial config (or the override) to have both the :js:attr:`executable` and :js:attr:`mainPack`
  98. properties set (normally done by the editor during export).
  99. :param EngineConfig override:
  100. An optional configuration override.
  101. :return:
  102. Promise that resolves once the game started.
  103. :rtype: Promise
  104. .. js:function:: prototype.copyToFS( path, buffer )
  105. Create a file at the specified ``path`` with the passed as ``buffer`` in the instance's file system.
  106. :param string path:
  107. The location where the file will be created.
  108. :param ArrayBuffer buffer:
  109. The content of the file.
  110. .. js:function:: prototype.requestQuit( )
  111. Request that the current instance quit.
  112. This is akin the user pressing the close button in the window manager, and will
  113. have no effect if the engine has crashed, or is stuck in a loop.
  114. Engine configuration
  115. --------------------
  116. An object used to configure the Engine instance based on godot export options, and to override those in custom HTML
  117. templates if needed.
  118. Properties
  119. ^^^^^^^^^^
  120. +-------------------+-------------------------------+
  121. | type | name |
  122. +-------------------+-------------------------------+
  123. | boolean | :js:attr:`unloadAfterInit` |
  124. +-------------------+-------------------------------+
  125. | HTMLCanvasElement | :js:attr:`canvas` |
  126. +-------------------+-------------------------------+
  127. | string | :js:attr:`executable` |
  128. +-------------------+-------------------------------+
  129. | string | :js:attr:`mainPack` |
  130. +-------------------+-------------------------------+
  131. | string | :js:attr:`locale` |
  132. +-------------------+-------------------------------+
  133. | number | :js:attr:`canvasResizePolicy` |
  134. +-------------------+-------------------------------+
  135. | Array.<string> | :js:attr:`args` |
  136. +-------------------+-------------------------------+
  137. | function | :js:attr:`onExecute` |
  138. +-------------------+-------------------------------+
  139. | function | :js:attr:`onExit` |
  140. +-------------------+-------------------------------+
  141. | function | :js:attr:`onProgress` |
  142. +-------------------+-------------------------------+
  143. | function | :js:attr:`onPrint` |
  144. +-------------------+-------------------------------+
  145. | function | :js:attr:`onPrintError` |
  146. +-------------------+-------------------------------+
  147. .. js:attribute:: EngineConfig
  148. The Engine configuration object. This is just a typedef, create it like a regular object, e.g.:
  149. ``const MyConfig = { executable: 'godot', unloadAfterInit: false }``
  150. **Property Descriptions**
  151. .. js:attribute:: unloadAfterInit
  152. Whether the unload the engine automatically after the instance is initialized.
  153. :type: boolean
  154. :value: ``true``
  155. .. js:attribute:: canvas
  156. The HTML DOM Canvas object to use.
  157. By default, the first canvas element in the document will be used is none is specified.
  158. :type: HTMLCanvasElement
  159. :value: ``null``
  160. .. js:attribute:: executable
  161. The name of the WASM file without the extension. (Set by Godot Editor export process).
  162. :type: string
  163. :value: ``""``
  164. .. js:attribute:: mainPack
  165. An alternative name for the game pck to load. The executable name is used otherwise.
  166. :type: string
  167. :value: ``null``
  168. .. js:attribute:: locale
  169. Specify a language code to select the proper localization for the game.
  170. The browser locale will be used if none is specified. See complete list of
  171. :ref:`supported locales <doc_locales>`.
  172. :type: string
  173. :value: ``null``
  174. .. js:attribute:: canvasResizePolicy
  175. The canvas resize policy determines how the canvas should be resized by Godot.
  176. ``0`` means Godot won't do any resizing. This is useful if you want to control the canvas size from
  177. javascript code in your template.
  178. ``1`` means Godot will resize the canvas on start, and when changing window size via engine functions.
  179. ``2`` means Godot will adapt the canvas size to match the whole browser window.
  180. :type: number
  181. :value: ``2``
  182. .. js:attribute:: args
  183. The arguments to be passed as command line arguments on startup.
  184. See :ref:`command line tutorial <doc_command_line_tutorial>`.
  185. **Note**: :js:meth:`startGame <Engine.prototype.startGame>` will always add the ``--main-pack`` argument.
  186. :type: Array.<string>
  187. :value: ``[]``
  188. .. js:function:: onExecute( path, args )
  189. A callback function for handling Godot's ``OS.execute`` calls.
  190. This is for example used in the Web Editor template to switch between Project Manager and editor, and for running the game.
  191. :param string path:
  192. The path that Godot's wants executed.
  193. :param Array.<string> args:
  194. The arguments of the "command" to execute.
  195. .. js:function:: onExit( status_code )
  196. A callback function for being notified when the Godot instance quits.
  197. **Note**: This function will not be called if the engine crashes or become unresponsive.
  198. :param number status_code:
  199. The status code returned by Godot on exit.
  200. .. js:function:: onProgress( current, total )
  201. A callback function for displaying download progress.
  202. The function is called once per frame while downloading files, so the usage of ``requestAnimationFrame()``
  203. is not necessary.
  204. If the callback function receives a total amount of bytes as 0, this means that it is impossible to calculate.
  205. Possible reasons include:
  206. - Files are delivered with server-side chunked compression
  207. - Files are delivered with server-side compression on Chromium
  208. - Not all file downloads have started yet (usually on servers without multi-threading)
  209. :param number current:
  210. The current amount of downloaded bytes so far.
  211. :param number total:
  212. The total amount of bytes to be downloaded.
  213. .. js:function:: onPrint( [ ...var_args ] )
  214. A callback function for handling the standard output stream. This method should usually only be used in debug pages.
  215. By default, ``console.log()`` is used.
  216. :param * var_args:
  217. A variadic number of arguments to be printed.
  218. .. js:function:: onPrintError( [ ...var_args ] )
  219. A callback function for handling the standard error stream. This method should usually only be used in debug pages.
  220. By default, ``console.error()`` is used.
  221. :param * var_args:
  222. A variadic number of arguments to be printed as errors.