lua_scripting.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <!DOCTYPE html>
  2. <html class="writer-html5" lang="en" >
  3. <head>
  4. <meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  5. <meta property="article:modified_time" content="2025-10-16T08:33:32+00:00" /><meta property="og:title" content="Scripting in Lua" />
  6. <meta property="og:type" content="website" />
  7. <meta property="og:url" content="https://docs.crownengine.org/html/latest/gameplay/lua_scripting.html" />
  8. <meta property="og:site_name" content="Crown 0.60.0 Manual" />
  9. <meta property="og:description" content="All gameplay code in Crown is written in Lua. This section will give you a basic understanding of how Lua is used and can be used in Crown. What is Lua: Lua is a simple, fast and lightweight scripting language which is easy to learn and supports all features needed to write an..." />
  10. <meta name="description" content="All gameplay code in Crown is written in Lua. This section will give you a basic understanding of how Lua is used and can be used in Crown. What is Lua: Lua is a simple, fast and lightweight scripting language which is easy to learn and supports all features needed to write an..." />
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  12. <title>Scripting in Lua &mdash; Crown 0.60.0 Manual 0.61.0 documentation</title>
  13. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  14. <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  15. <link rel="stylesheet" href="../_static/css/custom.css" type="text/css" />
  16. <link rel="shortcut icon" href="../_static/org.crownengine.Crown.svg"/>
  17. <link rel="canonical" href="https://docs.crownengine.org/html/latest/gameplay/lua_scripting.html" />
  18. <!--[if lt IE 9]>
  19. <script src="../_static/js/html5shiv.min.js"></script>
  20. <![endif]-->
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/js/theme.js"></script>
  26. <link rel="index" title="Index" href="../genindex.html" />
  27. <link rel="search" title="Search" href="../search.html" />
  28. <link rel="copyright" title="Copyright" href="../copyright.html" />
  29. <link rel="next" title="Objects binding and lifetime" href="objects_binding.html" />
  30. <link rel="prev" title="Writing Gameplay" href="index.html" />
  31. </head>
  32. <body class="wy-body-for-nav">
  33. <div class="wy-grid-for-nav">
  34. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  35. <div class="wy-side-scroll">
  36. <div class="wy-side-nav-search" >
  37. <a href="../index.html">
  38. <img src="../_static/org.crownengine.Crown.svg" class="logo" alt="Logo"/>
  39. </a>
  40. <div class="version">
  41. Crown 0.60.0 Manual
  42. </div>
  43. <div role="search">
  44. <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
  45. <input type="text" name="q" placeholder="Search docs" />
  46. <input type="hidden" name="check_keywords" value="yes" />
  47. <input type="hidden" name="area" value="default" />
  48. </form>
  49. </div>
  50. </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
  51. <ul class="current">
  52. <li class="toctree-l1"><a class="reference internal" href="../changelog.html">Changelog</a></li>
  53. <li class="toctree-l1"><a class="reference internal" href="../introduction.html">Introduction</a></li>
  54. <li class="toctree-l1"><a class="reference internal" href="../installing_crown/index.html">Installing Crown</a></li>
  55. <li class="toctree-l1"><a class="reference internal" href="../getting_started/index.html">Getting Started</a></li>
  56. <li class="toctree-l1"><a class="reference internal" href="../level_editor/index.html">The Level Editor</a></li>
  57. <li class="toctree-l1"><a class="reference internal" href="../importing_resources/index.html">Importing Resources</a></li>
  58. <li class="toctree-l1 current"><a class="reference internal" href="index.html">Writing Gameplay</a><ul class="current">
  59. <li class="toctree-l2 current"><a class="current reference internal" href="#">Scripting in Lua</a><ul>
  60. <li class="toctree-l3"><a class="reference internal" href="#what-is-lua">What is Lua</a></li>
  61. <li class="toctree-l3"><a class="reference internal" href="#runtime-entry-points">Runtime entry points</a></li>
  62. <li class="toctree-l3"><a class="reference internal" href="#the-boot-script">The boot script</a></li>
  63. <li class="toctree-l3"><a class="reference internal" href="#the-gamebase-framework">The GameBase framework</a></li>
  64. <li class="toctree-l3"><a class="reference internal" href="#hot-reloading">Hot reloading</a></li>
  65. </ul>
  66. </li>
  67. <li class="toctree-l2"><a class="reference internal" href="objects_binding.html">Objects binding and lifetime</a></li>
  68. <li class="toctree-l2"><a class="reference internal" href="unit_interaction.html">Interacting with Units</a></li>
  69. </ul>
  70. </li>
  71. <li class="toctree-l1"><a class="reference internal" href="../deploying/index.html">Deploying</a></li>
  72. <li class="toctree-l1"><a class="reference internal" href="../reference/index.html">Reference</a></li>
  73. <li class="toctree-l1"><a class="reference internal" href="../lua_api.html">Lua API reference</a></li>
  74. <li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
  75. <li class="toctree-l1"><a class="reference internal" href="../copyright.html">License</a></li>
  76. <li class="toctree-l1"><a class="reference internal" href="../hackers/index.html">Hackers</a></li>
  77. </ul>
  78. </div>
  79. </div>
  80. </nav>
  81. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
  82. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  83. <a href="../index.html">Crown 0.60.0 Manual</a>
  84. </nav>
  85. <div class="wy-nav-content">
  86. <div class="rst-content">
  87. <div role="navigation" aria-label="Page navigation">
  88. <ul class="wy-breadcrumbs">
  89. <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
  90. <li><a href="index.html">Writing Gameplay</a> &raquo;</li>
  91. <li>Scripting in Lua</li>
  92. <li class="wy-breadcrumbs-aside">
  93. <a href="../_sources/gameplay/lua_scripting.rst.txt" rel="nofollow"> View page source</a>
  94. </li>
  95. </ul>
  96. <hr/>
  97. </div>
  98. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  99. <div itemprop="articleBody">
  100. <section id="scripting-in-lua">
  101. <h1>Scripting in Lua<a class="headerlink" href="#scripting-in-lua" title="Permalink to this headline">¶</a></h1>
  102. <p>All gameplay code in Crown is written in Lua. This section will give you a basic
  103. understanding of how Lua is used and can be used in Crown.</p>
  104. <section id="what-is-lua">
  105. <h2>What is Lua<a class="headerlink" href="#what-is-lua" title="Permalink to this headline">¶</a></h2>
  106. <p>Lua is a simple, fast and lightweight scripting language which is easy to learn
  107. and supports all features needed to write any type of game.</p>
  108. <p>Lua is widely known in the games industry and has been used for decades to write
  109. many successful commercial games.</p>
  110. </section>
  111. <section id="runtime-entry-points">
  112. <h2>Runtime entry points<a class="headerlink" href="#runtime-entry-points" title="Permalink to this headline">¶</a></h2>
  113. <p>When Crown runs, it calls a small set of predefined global functions at
  114. specific times:</p>
  115. <blockquote>
  116. <div><ul class="simple">
  117. <li><p>init()</p></li>
  118. <li><p>shutdown()</p></li>
  119. <li><p>update(dt)</p></li>
  120. <li><p>render(dt)</p></li>
  121. </ul>
  122. </div></blockquote>
  123. <p>The init() function is called right after the runtime starts. It can be used to
  124. initialize global state etc. while shutdown() is called just before the runtime
  125. exits.</p>
  126. <p>update(dt) and render(dt) are called periodically as part of the game loop. You
  127. can use them to implement any gameplay logic, input handling and other per-frame
  128. updates your game may need.</p>
  129. </section>
  130. <section id="the-boot-script">
  131. <h2>The boot script<a class="headerlink" href="#the-boot-script" title="Permalink to this headline">¶</a></h2>
  132. <p>The boot script is the first Lua script that is executed when Crown starts.</p>
  133. <p>In the boot script you will typically define the <a class="reference internal" href="#runtime-entry-points">runtime entry points</a> and
  134. possibly require() additional lua scripts, like you would do in regular Lua
  135. programs. One difference is that Crown expects the .lua extension to be omitted
  136. (i.e. require(“foo/bar”) instead of “foo/bar.lua”). This is for consistency with
  137. how resources are referenced elsewhere.</p>
  138. <p>Projects may contain multiple boot scripts for different purposes (for example,
  139. separate boot scripts for the editors and the game). You can specify which boot
  140. script to use in the <a class="reference internal" href="../reference/boot_config.html"><span class="doc">Boot Config</span></a> file.</p>
  141. </section>
  142. <section id="the-gamebase-framework">
  143. <h2>The GameBase framework<a class="headerlink" href="#the-gamebase-framework" title="Permalink to this headline">¶</a></h2>
  144. <p>Crown automatically generates a <code class="docutils literal notranslate"><span class="pre">main.lua</span></code> script in the root folder of
  145. <a class="reference internal" href="../getting_started/create_new_project.html"><span class="doc">new projects</span></a>.</p>
  146. <p>You may notice that it contains some strangely named
  147. Game.init()/Game.update()/etc. functions, as well as other utility functions.</p>
  148. <p>Those functions are part of the GameBase framework, which is a tiny layer built
  149. on top of the fundamental <a class="reference internal" href="#runtime-entry-points">runtime entry points</a>.</p>
  150. <p>The GameBase layer allows for integration with the editors and adds support to
  151. common functionalities such as loading levels, creating a default camera etc.</p>
  152. <p>Expert users can avoid the GameBase althogheter by defining plain
  153. init/update/shutdown but we recommend to stick with it if you plan to use our
  154. tools.</p>
  155. </section>
  156. <section id="hot-reloading">
  157. <h2>Hot reloading<a class="headerlink" href="#hot-reloading" title="Permalink to this headline">¶</a></h2>
  158. <p>Crown fully supports reloading of gameplay code while the game is running.</p>
  159. <p>Hot reloading is achieved by re-executing modified Lua files. This method works
  160. well in general, but needs some care in specific occasions. Consider the
  161. following script:</p>
  162. <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="n">Foo</span> <span class="o">=</span> <span class="p">{}</span>
  163. </pre></div>
  164. </div>
  165. <p>Every time Crown reloads such file, a new table will be created and its
  166. reference will be stored in the variable Foo, making the previous state
  167. unreachable.</p>
  168. <p>To make it hot-reload safe you could check whether the Foo objects exists
  169. already, and skip its creation if that is the case:</p>
  170. <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="n">Foo</span> <span class="o">=</span> <span class="n">Foo</span> <span class="ow">or</span> <span class="p">{}</span>
  171. </pre></div>
  172. </div>
  173. </section>
  174. </section>
  175. </div>
  176. </div>
  177. <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
  178. <a href="index.html" class="btn btn-neutral float-left" title="Writing Gameplay" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
  179. <a href="objects_binding.html" class="btn btn-neutral float-right" title="Objects binding and lifetime" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
  180. </div>
  181. <hr/>
  182. <div role="contentinfo">
  183. <p>&#169; <a href="../copyright.html">Copyright</a> Except where otherwise noted, content on this page is licensed under a CC-BY-SA 4.0 Int. License.
  184. <span class="lastupdated">Last updated on Oct 16, 2025.
  185. </span></p>
  186. </div>
  187. Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
  188. <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
  189. provided by <a href="https://readthedocs.org">Read the Docs</a>.
  190. </footer>
  191. </div>
  192. </div>
  193. </section>
  194. </div>
  195. <script>
  196. jQuery(function () {
  197. SphinxRtdTheme.Navigation.enable(true);
  198. });
  199. </script>
  200. <!-- Theme Analytics -->
  201. <script async src="https://www.googletagmanager.com/gtag/js?id=G-XNVGCMNDZH"></script>
  202. <script>
  203. window.dataLayer = window.dataLayer || [];
  204. function gtag(){dataLayer.push(arguments);}
  205. gtag('js', new Date());
  206. gtag('config', 'G-XNVGCMNDZH', {
  207. 'anonymize_ip': true,
  208. });
  209. </script>
  210. </body>
  211. </html>