| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <!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="Objects binding and lifetime" />
- <meta property="og:type" content="website" />
- <meta property="og:url" content="https://docs.crownengine.org/html/latest/gameplay/objects_binding.html" />
- <meta property="og:site_name" content="Crown 0.60.0 Manual" />
- <meta property="og:description" content="Crown has a native runtime written in C++. To guarantee good performance and ease of use, we employ a number of different techniques when binding C-side objects to the Lua environment, depending on their type and lifetime. Light userdata binding: Most objects are exposed to Lu..." />
- <meta name="description" content="Crown has a native runtime written in C++. To guarantee good performance and ease of use, we employ a number of different techniques when binding C-side objects to the Lua environment, depending on their type and lifetime. Light userdata binding: Most objects are exposed to Lu..." />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Objects binding and lifetime — Crown 0.60.0 Manual 0.60.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/objects_binding.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="Interacting with Units" href="unit_interaction.html" />
- <link rel="prev" title="Scripting in Lua" href="lua_scripting.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"><a class="reference internal" href="lua_scripting.html">Scripting in Lua</a></li>
- <li class="toctree-l2 current"><a class="current reference internal" href="#">Objects binding and lifetime</a><ul>
- <li class="toctree-l3"><a class="reference internal" href="#light-userdata-binding">Light userdata binding</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#full-userdata-binding">Full userdata binding</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#singleton-objects">Singleton objects</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#temporary-objects">Temporary objects</a></li>
- </ul>
- </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>Objects binding and lifetime</li>
- <li class="wy-breadcrumbs-aside">
- <a href="../_sources/gameplay/objects_binding.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="objects-binding-and-lifetime">
- <h1>Objects binding and lifetime<a class="headerlink" href="#objects-binding-and-lifetime" title="Permalink to this headline">¶</a></h1>
- <p>Crown has a native runtime written in C++. To guarantee good performance and
- ease of use, we employ a number of different techniques when binding C-side
- objects to the Lua environment, depending on their type and lifetime.</p>
- <section id="light-userdata-binding">
- <h2>Light userdata binding<a class="headerlink" href="#light-userdata-binding" title="Permalink to this headline">¶</a></h2>
- <p>Most objects are exposed to Lua via Light userdata. Light userdata objects are
- owned by the C side and do not require garbage collection.</p>
- <p>One disadvantage is that individual Light userdata objects cannot have a
- metatable, so the regular object notation is not available when calling them:</p>
- <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="n">world</span><span class="p">:</span><span class="n">spawn_unit</span><span class="p">(</span><span class="s2">"units/grass"</span><span class="p">)</span>
- </pre></div>
- </div>
- <p>Instead you must explicitly look up the function in the World table and pass
- <cite>world</cite> as a parameter:</p>
- <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="n">World</span><span class="p">.</span><span class="n">spawn_unit</span><span class="p">(</span><span class="n">world</span><span class="p">,</span> <span class="s2">"units/grass"</span><span class="p">)</span>
- </pre></div>
- </div>
- </section>
- <section id="full-userdata-binding">
- <h2>Full userdata binding<a class="headerlink" href="#full-userdata-binding" title="Permalink to this headline">¶</a></h2>
- <p>Full userdata objects are owned by the Lua side, they are heap-allocated and
- subject to gargbage collection. They support metatables and thus offer object
- notation and operator overloading.</p>
- <p>We make sporadic use of full userdata, due to their runtime overhead. One such
- example are the “boxed” versions of regular math objects (Vector3Box,
- Matrix4x4Box etc).</p>
- </section>
- <section id="singleton-objects">
- <h2>Singleton objects<a class="headerlink" href="#singleton-objects" title="Permalink to this headline">¶</a></h2>
- <p>Objects which only have a single instance of them in the runtime are called
- singletons. Singletons are typically created at program start and live until the
- runtime exits.</p>
- <p>Since they always refer to the same instance, you don’t need to specify it when
- calling their functions:</p>
- <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="n">Device</span><span class="p">.</span><span class="n">version</span><span class="p">()</span>
- </pre></div>
- </div>
- </section>
- <section id="temporary-objects">
- <h2>Temporary objects<a class="headerlink" href="#temporary-objects" title="Permalink to this headline">¶</a></h2>
- <p>Sometimes you want an object to be owned by the Lua side, but without the
- performance implications of a garbage-collected Full userdata object. Vectors
- and matrices are a perfect example of that.</p>
- <p>Crown uses a fixed-size memory pool to allocate temporary objects from. The pool
- is reset at each frame so temporaries are only valid in the frame they are
- created.</p>
- <p>To store a temporary Vector3 for use in later frames, you need to copy its value
- into something else with a longer lifespan, for example, you could “box” it in a
- Vector3Box:</p>
- <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="kd">local</span> <span class="n">pos</span> <span class="o">=</span> <span class="n">Vector3Box</span><span class="p">()</span>
- <span class="kr">function</span> <span class="nf">update</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>
- <span class="kd">local</span> <span class="n">p</span> <span class="o">=</span> <span class="n">SceneGraph</span><span class="p">.</span><span class="n">local_position</span><span class="p">(...)</span>
- <span class="n">pos</span><span class="p">:</span><span class="n">store</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
- <span class="kr">end</span>
- </pre></div>
- </div>
- <p>And later retrieve its value with:</p>
- <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="kd">local</span> <span class="n">p</span> <span class="o">=</span> <span class="n">pos</span><span class="p">:</span><span class="n">unbox</span><span class="p">()</span>
- </pre></div>
- </div>
- <p>Crown will report an error when it detects a stale temporary object that is used
- across-frames.</p>
- <p>If you have particularly long computations, you may run out of temporary
- objects. This could happen when doing math inside loops for example. You can use
- <code class="docutils literal notranslate"><span class="pre">Device.temp_count()</span></code> and <code class="docutils literal notranslate"><span class="pre">Device.set_temp_count()</span></code> to save and restore the
- number of temporary objects used before and after computations:</p>
- <div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="kr">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="kr">in</span> <span class="nb">pairs</span><span class="p">(</span><span class="n">objects</span><span class="p">)</span> <span class="kr">do</span>
- <span class="kd">local</span> <span class="n">nv</span><span class="p">,</span> <span class="n">nq</span><span class="p">,</span> <span class="n">nm</span> <span class="o">=</span> <span class="n">Device</span><span class="p">.</span><span class="n">temp_count</span><span class="p">()</span>
- <span class="p">...</span>
- <span class="n">Device</span><span class="p">.</span><span class="n">set_temp_count</span><span class="p">(</span><span class="n">nv</span><span class="p">,</span> <span class="n">nq</span><span class="p">,</span> <span class="n">nm</span><span class="p">)</span>
- <span class="kr">end</span>
- </pre></div>
- </div>
- </section>
- </section>
- </div>
- </div>
- <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
- <a href="lua_scripting.html" class="btn btn-neutral float-left" title="Scripting in Lua" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
- <a href="unit_interaction.html" class="btn btn-neutral float-right" title="Interacting with Units" 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>
|