objects_binding.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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="Objects binding and lifetime" />
  6. <meta property="og:type" content="website" />
  7. <meta property="og:url" content="https://docs.crownengine.org/html/latest/gameplay/objects_binding.html" />
  8. <meta property="og:site_name" content="Crown 0.60.0 Manual" />
  9. <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..." />
  10. <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..." />
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  12. <title>Objects binding and lifetime &mdash; Crown 0.60.0 Manual 0.60.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/objects_binding.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="Interacting with Units" href="unit_interaction.html" />
  30. <link rel="prev" title="Scripting in Lua" href="lua_scripting.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"><a class="reference internal" href="lua_scripting.html">Scripting in Lua</a></li>
  59. <li class="toctree-l2 current"><a class="current reference internal" href="#">Objects binding and lifetime</a><ul>
  60. <li class="toctree-l3"><a class="reference internal" href="#light-userdata-binding">Light userdata binding</a></li>
  61. <li class="toctree-l3"><a class="reference internal" href="#full-userdata-binding">Full userdata binding</a></li>
  62. <li class="toctree-l3"><a class="reference internal" href="#singleton-objects">Singleton objects</a></li>
  63. <li class="toctree-l3"><a class="reference internal" href="#temporary-objects">Temporary objects</a></li>
  64. </ul>
  65. </li>
  66. <li class="toctree-l2"><a class="reference internal" href="unit_interaction.html">Interacting with Units</a></li>
  67. </ul>
  68. </li>
  69. <li class="toctree-l1"><a class="reference internal" href="../deploying/index.html">Deploying</a></li>
  70. <li class="toctree-l1"><a class="reference internal" href="../reference/index.html">Reference</a></li>
  71. <li class="toctree-l1"><a class="reference internal" href="../lua_api.html">Lua API reference</a></li>
  72. <li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
  73. <li class="toctree-l1"><a class="reference internal" href="../copyright.html">License</a></li>
  74. <li class="toctree-l1"><a class="reference internal" href="../hackers/index.html">Hackers</a></li>
  75. </ul>
  76. </div>
  77. </div>
  78. </nav>
  79. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
  80. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  81. <a href="../index.html">Crown 0.60.0 Manual</a>
  82. </nav>
  83. <div class="wy-nav-content">
  84. <div class="rst-content">
  85. <div role="navigation" aria-label="Page navigation">
  86. <ul class="wy-breadcrumbs">
  87. <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
  88. <li><a href="index.html">Writing Gameplay</a> &raquo;</li>
  89. <li>Objects binding and lifetime</li>
  90. <li class="wy-breadcrumbs-aside">
  91. <a href="../_sources/gameplay/objects_binding.rst.txt" rel="nofollow"> View page source</a>
  92. </li>
  93. </ul>
  94. <hr/>
  95. </div>
  96. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  97. <div itemprop="articleBody">
  98. <section id="objects-binding-and-lifetime">
  99. <h1>Objects binding and lifetime<a class="headerlink" href="#objects-binding-and-lifetime" title="Permalink to this headline">¶</a></h1>
  100. <p>Crown has a native runtime written in C++. To guarantee good performance and
  101. ease of use, we employ a number of different techniques when binding C-side
  102. objects to the Lua environment, depending on their type and lifetime.</p>
  103. <section id="light-userdata-binding">
  104. <h2>Light userdata binding<a class="headerlink" href="#light-userdata-binding" title="Permalink to this headline">¶</a></h2>
  105. <p>Most objects are exposed to Lua via Light userdata. Light userdata objects are
  106. owned by the C side and do not require garbage collection.</p>
  107. <p>One disadvantage is that individual Light userdata objects cannot have a
  108. metatable, so the regular object notation is not available when calling them:</p>
  109. <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">&quot;units/grass&quot;</span><span class="p">)</span>
  110. </pre></div>
  111. </div>
  112. <p>Instead you must explicitly look up the function in the World table and pass
  113. <cite>world</cite> as a parameter:</p>
  114. <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">&quot;units/grass&quot;</span><span class="p">)</span>
  115. </pre></div>
  116. </div>
  117. </section>
  118. <section id="full-userdata-binding">
  119. <h2>Full userdata binding<a class="headerlink" href="#full-userdata-binding" title="Permalink to this headline">¶</a></h2>
  120. <p>Full userdata objects are owned by the Lua side, they are heap-allocated and
  121. subject to gargbage collection. They support metatables and thus offer object
  122. notation and operator overloading.</p>
  123. <p>We make sporadic use of full userdata, due to their runtime overhead. One such
  124. example are the “boxed” versions of regular math objects (Vector3Box,
  125. Matrix4x4Box etc).</p>
  126. </section>
  127. <section id="singleton-objects">
  128. <h2>Singleton objects<a class="headerlink" href="#singleton-objects" title="Permalink to this headline">¶</a></h2>
  129. <p>Objects which only have a single instance of them in the runtime are called
  130. singletons. Singletons are typically created at program start and live until the
  131. runtime exits.</p>
  132. <p>Since they always refer to the same instance, you don’t need to specify it when
  133. calling their functions:</p>
  134. <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>
  135. </pre></div>
  136. </div>
  137. </section>
  138. <section id="temporary-objects">
  139. <h2>Temporary objects<a class="headerlink" href="#temporary-objects" title="Permalink to this headline">¶</a></h2>
  140. <p>Sometimes you want an object to be owned by the Lua side, but without the
  141. performance implications of a garbage-collected Full userdata object. Vectors
  142. and matrices are a perfect example of that.</p>
  143. <p>Crown uses a fixed-size memory pool to allocate temporary objects from. The pool
  144. is reset at each frame so temporaries are only valid in the frame they are
  145. created.</p>
  146. <p>To store a temporary Vector3 for use in later frames, you need to copy its value
  147. into something else with a longer lifespan, for example, you could “box” it in a
  148. Vector3Box:</p>
  149. <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>
  150. <span class="kr">function</span> <span class="nf">update</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>
  151. <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>
  152. <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>
  153. <span class="kr">end</span>
  154. </pre></div>
  155. </div>
  156. <p>And later retrieve its value with:</p>
  157. <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>
  158. </pre></div>
  159. </div>
  160. <p>Crown will report an error when it detects a stale temporary object that is used
  161. across-frames.</p>
  162. <p>If you have particularly long computations, you may run out of temporary
  163. objects. This could happen when doing math inside loops for example. You can use
  164. <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
  165. number of temporary objects used before and after computations:</p>
  166. <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>
  167. <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>
  168. <span class="p">...</span>
  169. <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>
  170. <span class="kr">end</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="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>
  179. <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>
  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>