lua_scripting.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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="../importing_resources/index.html">Importing Resources</a></li>
  57. <li class="toctree-l1 current"><a class="reference internal" href="index.html">Writing Gameplay</a><ul class="current">
  58. <li class="toctree-l2 current"><a class="current reference internal" href="#">Scripting in Lua</a><ul>
  59. <li class="toctree-l3"><a class="reference internal" href="#what-is-lua">What is Lua</a></li>
  60. <li class="toctree-l3"><a class="reference internal" href="#runtime-entry-points">Runtime entry points</a></li>
  61. <li class="toctree-l3"><a class="reference internal" href="#the-boot-script">The boot script</a></li>
  62. <li class="toctree-l3"><a class="reference internal" href="#the-gamebase-framework">The GameBase framework</a></li>
  63. <li class="toctree-l3"><a class="reference internal" href="#hot-reloading">Hot reloading</a></li>
  64. </ul>
  65. </li>
  66. <li class="toctree-l2"><a class="reference internal" href="objects_binding.html">Objects binding and lifetime</a></li>
  67. <li class="toctree-l2"><a class="reference internal" href="unit_interaction.html">Interacting with Units</a></li>
  68. </ul>
  69. </li>
  70. <li class="toctree-l1"><a class="reference internal" href="../deploying/index.html">Deploying</a></li>
  71. <li class="toctree-l1"><a class="reference internal" href="../reference/index.html">Reference</a></li>
  72. <li class="toctree-l1"><a class="reference internal" href="../lua_api.html">Lua API reference</a></li>
  73. <li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
  74. <li class="toctree-l1"><a class="reference internal" href="../copyright.html">License</a></li>
  75. <li class="toctree-l1"><a class="reference internal" href="../hackers/index.html">Hackers</a></li>
  76. </ul>
  77. </div>
  78. </div>
  79. </nav>
  80. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
  81. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  82. <a href="../index.html">Crown 0.60.0 Manual</a>
  83. </nav>
  84. <div class="wy-nav-content">
  85. <div class="rst-content">
  86. <div role="navigation" aria-label="Page navigation">
  87. <ul class="wy-breadcrumbs">
  88. <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
  89. <li><a href="index.html">Writing Gameplay</a> &raquo;</li>
  90. <li>Scripting in Lua</li>
  91. <li class="wy-breadcrumbs-aside">
  92. <a href="../_sources/gameplay/lua_scripting.rst.txt" rel="nofollow"> View page source</a>
  93. </li>
  94. </ul>
  95. <hr/>
  96. </div>
  97. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  98. <div itemprop="articleBody">
  99. <section id="scripting-in-lua">
  100. <h1>Scripting in Lua<a class="headerlink" href="#scripting-in-lua" title="Permalink to this headline">¶</a></h1>
  101. <p>All gameplay code in Crown is written in Lua. This section will give you a basic
  102. understanding of how Lua is used and can be used in Crown.</p>
  103. <section id="what-is-lua">
  104. <h2>What is Lua<a class="headerlink" href="#what-is-lua" title="Permalink to this headline">¶</a></h2>
  105. <p>Lua is a simple, fast and lightweight scripting language which is easy to learn
  106. and supports all features needed to write any type of game.</p>
  107. <p>Lua is widely known in the games industry and has been used for decades to write
  108. many successful commercial games.</p>
  109. </section>
  110. <section id="runtime-entry-points">
  111. <h2>Runtime entry points<a class="headerlink" href="#runtime-entry-points" title="Permalink to this headline">¶</a></h2>
  112. <p>When Crown runs, it calls a small set of predefined global functions at
  113. specific times:</p>
  114. <blockquote>
  115. <div><ul class="simple">
  116. <li><p>init()</p></li>
  117. <li><p>shutdown()</p></li>
  118. <li><p>update(dt)</p></li>
  119. <li><p>render(dt)</p></li>
  120. </ul>
  121. </div></blockquote>
  122. <p>The init() function is called right after the runtime starts. It can be used to
  123. initialize global state etc. while shutdown() is called just before the runtime
  124. exits.</p>
  125. <p>update(dt) and render(dt) are called periodically as part of the game loop. You
  126. can use them to implement any gameplay logic, input handling and other per-frame
  127. updates your game may need.</p>
  128. </section>
  129. <section id="the-boot-script">
  130. <h2>The boot script<a class="headerlink" href="#the-boot-script" title="Permalink to this headline">¶</a></h2>
  131. <p>The boot script is the first Lua script that is executed when Crown starts.</p>
  132. <p>In the boot script you will typically define the <a class="reference internal" href="#runtime-entry-points">runtime entry points</a> and
  133. possibly require() additional lua scripts, like you would do in regular Lua
  134. programs. One difference is that Crown expects the .lua extension to be omitted
  135. (i.e. require(“foo/bar”) instead of “foo/bar.lua”). This is for consistency with
  136. how resources are referenced elsewhere.</p>
  137. <p>Projects may contain multiple boot scripts for different purposes (for example,
  138. separate boot scripts for the editors and the game). You can specify which boot
  139. script to use in the <a class="reference internal" href="../reference/boot_config.html"><span class="doc">Boot Config</span></a> file.</p>
  140. </section>
  141. <section id="the-gamebase-framework">
  142. <h2>The GameBase framework<a class="headerlink" href="#the-gamebase-framework" title="Permalink to this headline">¶</a></h2>
  143. <p>Crown automatically generates a <code class="docutils literal notranslate"><span class="pre">main.lua</span></code> script in the root folder of
  144. <a class="reference internal" href="../getting_started/create_new_project.html"><span class="doc">new projects</span></a>.</p>
  145. <p>You may notice that it contains some strangely named
  146. Game.init()/Game.update()/etc. functions, as well as other utility functions.</p>
  147. <p>Those functions are part of the GameBase framework, which is a tiny layer built
  148. on top of the fundamental <a class="reference internal" href="#runtime-entry-points">runtime entry points</a>.</p>
  149. <p>The GameBase layer allows for integration with the editors and adds support to
  150. common functionalities such as loading levels, creating a default camera etc.</p>
  151. <p>Expert users can avoid the GameBase althogheter by defining plain
  152. init/update/shutdown but we recommend to stick with it if you plan to use our
  153. tools.</p>
  154. </section>
  155. <section id="hot-reloading">
  156. <h2>Hot reloading<a class="headerlink" href="#hot-reloading" title="Permalink to this headline">¶</a></h2>
  157. <p>Crown fully supports reloading of gameplay code while the game is running.</p>
  158. <p>Hot reloading is achieved by re-executing modified Lua files. This method works
  159. well in general, but needs some care in specific occasions. Consider the
  160. following script:</p>
  161. <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="n">Foo</span> <span class="o">=</span> <span class="p">{}</span>
  162. </pre></div>
  163. </div>
  164. <p>Every time Crown reloads such file, a new table will be created and its
  165. reference will be stored in the variable Foo, making the previous state
  166. unreachable.</p>
  167. <p>To make it hot-reload safe you could check whether the Foo objects exists
  168. already, and skip its creation if that is the case:</p>
  169. <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>
  170. </pre></div>
  171. </div>
  172. </section>
  173. </section>
  174. </div>
  175. </div>
  176. <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
  177. <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>
  178. <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>
  179. </div>
  180. <hr/>
  181. <div role="contentinfo">
  182. <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.
  183. <span class="lastupdated">Last updated on Oct 16, 2025.
  184. </span></p>
  185. </div>
  186. Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
  187. <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
  188. provided by <a href="https://readthedocs.org">Read the Docs</a>.
  189. </footer>
  190. </div>
  191. </div>
  192. </section>
  193. </div>
  194. <script>
  195. jQuery(function () {
  196. SphinxRtdTheme.Navigation.enable(true);
  197. });
  198. </script>
  199. <!-- Theme Analytics -->
  200. <script async src="https://www.googletagmanager.com/gtag/js?id=G-XNVGCMNDZH"></script>
  201. <script>
  202. window.dataLayer = window.dataLayer || [];
  203. function gtag(){dataLayer.push(arguments);}
  204. gtag('js', new Date());
  205. gtag('config', 'G-XNVGCMNDZH', {
  206. 'anonymize_ip': true,
  207. });
  208. </script>
  209. </body>
  210. </html>