Files
entt/md_docs_md_faq.html
2021-01-09 18:58:42 +01:00

172 lines
15 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.20"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>EnTT: Frequently Asked Questions</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">EnTT
&#160;<span id="projectnumber">3.6.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.20 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- top -->
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">Frequently Asked Questions </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="autotoc_md78"></a>
Introduction</h1>
<p>This is a constantly updated section where I'll try to put the answers to the most frequently asked questions.<br />
If you don't find your answer here, there are two cases: nobody has done it yet or this section needs updating. In both cases, try to <a href="https://github.com/skypjack/entt/issues/new">open a new issue</a> or enter the <a href="https://gitter.im/skypjack/entt">gitter channel</a> and ask your question. Probably someone already has an answer for you and we can then integrate this part of the documentation.</p>
<h1><a class="anchor" id="autotoc_md79"></a>
FAQ</h1>
<h2><a class="anchor" id="autotoc_md80"></a>
Why is my debug build on Windows so slow?</h2>
<p><code>EnTT</code> is an experimental project that I also use to keep me up-to-date with the latest revision of the language and the standard library. For this reason, it's likely that some classes you're working with are using standard containers under the hood.<br />
Unfortunately, it's known that the standard containers aren't particularly performing in debugging (the reasons for this go beyond this document) and are even less so on Windows apparently. Fortunately this can also be mitigated a lot, achieving good results in many cases.</p>
<p>First of all, there are two things to do in a Windows project:</p>
<ul>
<li>Disable the <a href="https://docs.microsoft.com/cpp/build/reference/jmc"><code>/JMC</code></a> option (<em>Just My Code</em> debugging), available starting in Visual Studio 2017 version 15.8.</li>
<li>Set the <a href="https://docs.microsoft.com/cpp/standard-library/iterator-debug-level"><code>_ITERATOR_DEBUG_LEVEL</code></a> macro to 0. This will disable checked iterators and iterator debugging.</li>
</ul>
<p>Moreover, the macro <code>ENTT_ASSERT</code> should be redefined to disable internal checks made by <code>EnTT</code> in debug:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#define ENTT_ASSERT(...) ((void)0)</span></div>
</div><!-- fragment --><p>These asserts are introduced to help the users but they require to access to the underlying containers and therefore risk ruining the performance in some cases.</p>
<p>With these changes, debug performance should increase enough for most cases. If you want something more, you can can also switch to an optimization level <code>O0</code> or preferably <code>O1</code>.</p>
<h2><a class="anchor" id="autotoc_md81"></a>
How can I represent hierarchies with my components?</h2>
<p>This is one of the first questions that anyone makes when starting to work with the entity-component-system architectural pattern.<br />
There are several approaches to the problem and whats the best one depends mainly on the real problem one is facing. In all cases, how to do it doesn't strictly depend on the library in use, but the latter can certainly allow or not different techniques depending on how the data are laid out.</p>
<p>I tried to describe some of the techniques that fit well with the model of <code>EnTT</code>. <a href="https://skypjack.github.io/2019-06-25-ecs-baf-part-4/">Here</a> is the first post of a series that tries to explore the problem. More will probably come in future.</p>
<p>Long story short, you can always define a tree where the nodes expose implicit lists of children by means of the following type:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>relationship {</div>
<div class="line"> std::size_t children{};</div>
<div class="line"> <a class="code" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entt::entity</a> first{<a class="code" href="namespaceentt.html#a2f0c0a1c1d953ea991591748744cdd8b">entt::null</a>};</div>
<div class="line"> <a class="code" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entt::entity</a> prev{<a class="code" href="namespaceentt.html#a2f0c0a1c1d953ea991591748744cdd8b">entt::null</a>};</div>
<div class="line"> <a class="code" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entt::entity</a> next{<a class="code" href="namespaceentt.html#a2f0c0a1c1d953ea991591748744cdd8b">entt::null</a>};</div>
<div class="line"> <a class="code" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entt::entity</a> parent{<a class="code" href="namespaceentt.html#a2f0c0a1c1d953ea991591748744cdd8b">entt::null</a>};</div>
<div class="line"> <span class="comment">// ... other data members ...</span></div>
<div class="line">};</div>
</div><!-- fragment --><p>The sort functionalities of <code>EnTT</code>, the groups and all the other features of the library can help then to get the best in terms of data locality and therefore performance from this component.</p>
<h2><a class="anchor" id="autotoc_md82"></a>
Custom entity identifiers: yay or nay?</h2>
<p>Custom entity identifiers are definitely a good idea in two cases at least:</p>
<ul>
<li>If <code>std::uint32_t</code> is too large or isn't large enough for your purposes, since this is the underlying type of <code><a class="el" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a" title="Default entity identifier.">entt::entity</a></code>.</li>
<li>If you want to avoid conflicts when using multiple registries.</li>
</ul>
<p>Identifiers can be defined through enum classes and custom types for which a specialization of <code>entt_traits</code> exists. For this purpose, <code>entt_traits</code> is also defined as a <em>sfinae-friendly</em> class template.<br />
In fact, this is a definition equivalent to that of <code><a class="el" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a" title="Default entity identifier.">entt::entity</a></code>:</p>
<div class="fragment"><div class="line"><span class="keyword">enum class</span> <a class="code" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entity</a>: std::uint32_t {};</div>
</div><!-- fragment --><p>In theory, integral types can also be used as entity identifiers, even though this may break in future and isn't recommended in general.</p>
<h2><a class="anchor" id="autotoc_md83"></a>
Warning C4307: integral constant overflow</h2>
<p>According to <a href="https://github.com/skypjack/entt/issues/121">this</a> issue, using a hashed string under VS could generate a warning.<br />
First of all, I want to reassure you: it's expected and harmless. However, it can be annoying.</p>
<p>To suppress it and if you don't want to suppress all the other warnings as well, here is a workaround in the form of a macro:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#if defined(_MSC_VER)</span></div>
<div class="line"><span class="preprocessor">#define HS(str) __pragma(warning(suppress:4307)) entt::hashed_string{str}</span></div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor">#define HS(str) entt::hashed_string{str}</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
</div><!-- fragment --><p>With an example of use included:</p>
<div class="fragment"><div class="line">constexpr <span class="keyword">auto</span> identifier = HS(<span class="stringliteral">&quot;my/resource/identifier&quot;</span>);</div>
</div><!-- fragment --><p>Thanks to <a href="https://github.com/huwpascoe">huwpascoe</a> for the courtesy.</p>
<h2><a class="anchor" id="autotoc_md84"></a>
Warning C4003: the min, the max and the macro</h2>
<p>On Windows, a header file defines two macros <code>min</code> and <code>max</code> which may result in conflicts with their counterparts in the standard library and therefore in errors during compilation.</p>
<p>It's a pretty big problem but fortunately it's not a problem of <code>EnTT</code> and there is a fairly simple solution to it.<br />
It consists in defining the <code>NOMINMAX</code> macro before to include any other header so as to get rid of the extra definitions:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#define NOMINMAX</span></div>
</div><!-- fragment --><p>Please refer to <a href="https://github.com/skypjack/entt/issues/96">this</a> issue for more details.</p>
<h2><a class="anchor" id="autotoc_md85"></a>
The standard and the non-copyable types</h2>
<p><code>EnTT</code> uses internally the trait <code>std::is_copy_constructible_v</code> to check if a component is actually copyable. This trait doesn't check if an object can actually be copied but only verifies if there is a copy constructor available.<br />
This can lead to surprising results due to some idiosyncrasies of the standard mainly related to the need to guarantee backward compatibility.</p>
<p>For example, <code>std::vector</code> defines a copy constructor no matter if its value type is copyable or not. As a result, <code>std::is_copy_constructible_v</code> is true for the following specialization:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>type {</div>
<div class="line"> std::vector&lt;std::unique_ptr&lt;action&gt;&gt; vec;</div>
<div class="line">};</div>
</div><!-- fragment --><p>When trying to assign an instance of this type to an entity in the ECS part, this may trigger a compilation error because we cannot really make a copy of it.<br />
As a workaround, users can mark the type explicitly as non-copyable:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>type {</div>
<div class="line"> type(<span class="keyword">const</span> type &amp;) = <span class="keyword">delete</span>;</div>
<div class="line"> type &amp; operator=(<span class="keyword">const</span> type &amp;) = <span class="keyword">delete</span>;</div>
<div class="line"> </div>
<div class="line"> std::vector&lt;std::unique_ptr&lt;action&gt;&gt; vec;</div>
<div class="line">};</div>
</div><!-- fragment --><p>Unfortunately, this will also disable aggregate initialization.</p>
<h2><a class="anchor" id="autotoc_md86"></a>
Which functions trigger which signals</h2>
<p>The <code>registry</code> class offers three signals that are emitted following specific operations. Maybe not everyone knows what these operations are, though.<br />
If this isn't clear, below you can find a <em>vademecum</em> for this purpose:</p>
<ul>
<li><code>on_created</code> is invoked when a component is first added (neither modified nor replaced) to an entity.</li>
<li><code>on_update</code> is called whenever an existing component is modified or replaced.</li>
<li><code>on_destroyed</code> is called when a component is explicitly or implicitly removed from an entity.</li>
</ul>
<p>Among the most controversial functions can be found <code>emplace_or_replace</code> and <code>destroy</code>. However, following the above rules, it's quite simple to know what will happen.<br />
In the first case, <code>on_created</code> is invoked if the entity has not the component, otherwise the latter is replaced and therefore <code>on_update</code> is triggered. As for the second case, components are removed from their entities and thus freed when they are recycled. It means that <code>on_destroyed</code> is triggered for every component owned by the entity that is destroyed. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<div class="ttc" id="anamespaceentt_html_a2f0c0a1c1d953ea991591748744cdd8b"><div class="ttname"><a href="namespaceentt.html#a2f0c0a1c1d953ea991591748744cdd8b">entt::null</a></div><div class="ttdeci">constexpr null_t null</div><div class="ttdoc">Compile-time constant for null entities.</div><div class="ttdef"><b>Definition:</b> <a href="entity_8hpp_source.html#l00189">entity.hpp:189</a></div></div>
<div class="ttc" id="anamespaceentt_html_a0b54e231d069e8a231e14b223388808a"><div class="ttname"><a href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entt::entity</a></div><div class="ttdeci">entity</div><div class="ttdoc">Default entity identifier.</div><div class="ttdef"><b>Definition:</b> <a href="entity_2fwd_8hpp_source.html#l00060">fwd.hpp:60</a></div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="http://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.8.20
</small></address>
</body>
</html>