| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <!DOCTYPE html>
- <html class="writer-html5" lang="en" >
- <head>
- <meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
- <meta property="article:modified_time" content="2025-10-16T08:33:32+00:00" /><meta property="og:title" content="Scripting in Lua" />
- <meta property="og:type" content="website" />
- <meta property="og:url" content="https://docs.crownengine.org/html/latest/gameplay/lua_scripting.html" />
- <meta property="og:site_name" content="Crown 0.60.0 Manual" />
- <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..." />
- <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..." />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Scripting in Lua — Crown 0.60.0 Manual 0.61.0 documentation</title>
- <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
- <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
- <link rel="stylesheet" href="../_static/css/custom.css" type="text/css" />
- <link rel="shortcut icon" href="../_static/org.crownengine.Crown.svg"/>
- <link rel="canonical" href="https://docs.crownengine.org/html/latest/gameplay/lua_scripting.html" />
- <!--[if lt IE 9]>
- <script src="../_static/js/html5shiv.min.js"></script>
- <![endif]-->
-
- <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
- <script src="../_static/jquery.js"></script>
- <script src="../_static/underscore.js"></script>
- <script src="../_static/doctools.js"></script>
- <script src="../_static/js/theme.js"></script>
- <link rel="index" title="Index" href="../genindex.html" />
- <link rel="search" title="Search" href="../search.html" />
- <link rel="copyright" title="Copyright" href="../copyright.html" />
- <link rel="next" title="Objects binding and lifetime" href="objects_binding.html" />
- <link rel="prev" title="Writing Gameplay" href="index.html" />
- </head>
- <body class="wy-body-for-nav">
- <div class="wy-grid-for-nav">
- <nav data-toggle="wy-nav-shift" class="wy-nav-side">
- <div class="wy-side-scroll">
- <div class="wy-side-nav-search" >
- <a href="../index.html">
- <img src="../_static/org.crownengine.Crown.svg" class="logo" alt="Logo"/>
- </a>
- <div class="version">
- Crown 0.60.0 Manual
- </div>
- <div role="search">
- <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
- <input type="text" name="q" placeholder="Search docs" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- </div>
- </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
- <ul class="current">
- <li class="toctree-l1"><a class="reference internal" href="../changelog.html">Changelog</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../introduction.html">Introduction</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../installing_crown/index.html">Installing Crown</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../getting_started/index.html">Getting Started</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../importing_resources/index.html">Importing Resources</a></li>
- <li class="toctree-l1 current"><a class="reference internal" href="index.html">Writing Gameplay</a><ul class="current">
- <li class="toctree-l2 current"><a class="current reference internal" href="#">Scripting in Lua</a><ul>
- <li class="toctree-l3"><a class="reference internal" href="#what-is-lua">What is Lua</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#runtime-entry-points">Runtime entry points</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#the-boot-script">The boot script</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#the-gamebase-framework">The GameBase framework</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#hot-reloading">Hot reloading</a></li>
- </ul>
- </li>
- <li class="toctree-l2"><a class="reference internal" href="objects_binding.html">Objects binding and lifetime</a></li>
- <li class="toctree-l2"><a class="reference internal" href="unit_interaction.html">Interacting with Units</a></li>
- </ul>
- </li>
- <li class="toctree-l1"><a class="reference internal" href="../deploying/index.html">Deploying</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../reference/index.html">Reference</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../lua_api.html">Lua API reference</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../copyright.html">License</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../hackers/index.html">Hackers</a></li>
- </ul>
- </div>
- </div>
- </nav>
- <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
- <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
- <a href="../index.html">Crown 0.60.0 Manual</a>
- </nav>
- <div class="wy-nav-content">
- <div class="rst-content">
- <div role="navigation" aria-label="Page navigation">
- <ul class="wy-breadcrumbs">
- <li><a href="../index.html" class="icon icon-home"></a> »</li>
- <li><a href="index.html">Writing Gameplay</a> »</li>
- <li>Scripting in Lua</li>
- <li class="wy-breadcrumbs-aside">
- <a href="../_sources/gameplay/lua_scripting.rst.txt" rel="nofollow"> View page source</a>
- </li>
- </ul>
- <hr/>
- </div>
- <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
- <div itemprop="articleBody">
-
- <section id="scripting-in-lua">
- <h1>Scripting in Lua<a class="headerlink" href="#scripting-in-lua" title="Permalink to this headline">¶</a></h1>
- <p>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.</p>
- <section id="what-is-lua">
- <h2>What is Lua<a class="headerlink" href="#what-is-lua" title="Permalink to this headline">¶</a></h2>
- <p>Lua is a simple, fast and lightweight scripting language which is easy to learn
- and supports all features needed to write any type of game.</p>
- <p>Lua is widely known in the games industry and has been used for decades to write
- many successful commercial games.</p>
- </section>
- <section id="runtime-entry-points">
- <h2>Runtime entry points<a class="headerlink" href="#runtime-entry-points" title="Permalink to this headline">¶</a></h2>
- <p>When Crown runs, it calls a small set of predefined global functions at
- specific times:</p>
- <blockquote>
- <div><ul class="simple">
- <li><p>init()</p></li>
- <li><p>shutdown()</p></li>
- <li><p>update(dt)</p></li>
- <li><p>render(dt)</p></li>
- </ul>
- </div></blockquote>
- <p>The init() function is called right after the runtime starts. It can be used to
- initialize global state etc. while shutdown() is called just before the runtime
- exits.</p>
- <p>update(dt) and render(dt) are called periodically as part of the game loop. You
- can use them to implement any gameplay logic, input handling and other per-frame
- updates your game may need.</p>
- </section>
- <section id="the-boot-script">
- <h2>The boot script<a class="headerlink" href="#the-boot-script" title="Permalink to this headline">¶</a></h2>
- <p>The boot script is the first Lua script that is executed when Crown starts.</p>
- <p>In the boot script you will typically define the <a class="reference internal" href="#runtime-entry-points">runtime entry points</a> and
- possibly require() additional lua scripts, like you would do in regular Lua
- programs. One difference is that Crown expects the .lua extension to be omitted
- (i.e. require(“foo/bar”) instead of “foo/bar.lua”). This is for consistency with
- how resources are referenced elsewhere.</p>
- <p>Projects may contain multiple boot scripts for different purposes (for example,
- separate boot scripts for the editors and the game). You can specify which boot
- script to use in the <a class="reference internal" href="../reference/boot_config.html"><span class="doc">Boot Config</span></a> file.</p>
- </section>
- <section id="the-gamebase-framework">
- <h2>The GameBase framework<a class="headerlink" href="#the-gamebase-framework" title="Permalink to this headline">¶</a></h2>
- <p>Crown automatically generates a <code class="docutils literal notranslate"><span class="pre">main.lua</span></code> script in the root folder of
- <a class="reference internal" href="../getting_started/create_new_project.html"><span class="doc">new projects</span></a>.</p>
- <p>You may notice that it contains some strangely named
- Game.init()/Game.update()/etc. functions, as well as other utility functions.</p>
- <p>Those functions are part of the GameBase framework, which is a tiny layer built
- on top of the fundamental <a class="reference internal" href="#runtime-entry-points">runtime entry points</a>.</p>
- <p>The GameBase layer allows for integration with the editors and adds support to
- common functionalities such as loading levels, creating a default camera etc.</p>
- <p>Expert users can avoid the GameBase althogheter by defining plain
- init/update/shutdown but we recommend to stick with it if you plan to use our
- tools.</p>
- </section>
- <section id="hot-reloading">
- <h2>Hot reloading<a class="headerlink" href="#hot-reloading" title="Permalink to this headline">¶</a></h2>
- <p>Crown fully supports reloading of gameplay code while the game is running.</p>
- <p>Hot reloading is achieved by re-executing modified Lua files. This method works
- well in general, but needs some care in specific occasions. Consider the
- following script:</p>
- <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="n">Foo</span> <span class="o">=</span> <span class="p">{}</span>
- </pre></div>
- </div>
- <p>Every time Crown reloads such file, a new table will be created and its
- reference will be stored in the variable Foo, making the previous state
- unreachable.</p>
- <p>To make it hot-reload safe you could check whether the Foo objects exists
- already, and skip its creation if that is the case:</p>
- <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>
- </pre></div>
- </div>
- </section>
- </section>
- </div>
- </div>
- <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
- <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>
- <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>
- </div>
- <hr/>
- <div role="contentinfo">
- <p>© <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.
- <span class="lastupdated">Last updated on Oct 16, 2025.
- </span></p>
- </div>
- Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
- <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
- provided by <a href="https://readthedocs.org">Read the Docs</a>.
-
- </footer>
- </div>
- </div>
- </section>
- </div>
- <script>
- jQuery(function () {
- SphinxRtdTheme.Navigation.enable(true);
- });
- </script>
- <!-- Theme Analytics -->
- <script async src="https://www.googletagmanager.com/gtag/js?id=G-XNVGCMNDZH"></script>
- <script>
- window.dataLayer = window.dataLayer || [];
- function gtag(){dataLayer.push(arguments);}
- gtag('js', new Date());
- gtag('config', 'G-XNVGCMNDZH', {
- 'anonymize_ip': true,
- });
- </script>
- </body>
- </html>
|