1# Frequently Asked Questions 2 3<!-- 4@cond TURN_OFF_DOXYGEN 5--> 6# Table of Contents 7 8* [Introduction](#introduction) 9* [FAQ](#faq) 10 * [Why is my debug build on Windows so slow?](#why-is-my-debug-build-on-windows-so-slow) 11 * [How can I represent hierarchies with my components?](#how-can-i-represent-hierarchies-with-my-components) 12 * [Custom entity identifiers: yay or nay?](#custom-entity-identifiers-yay-or-nay) 13 * [Warning C4307: integral constant overflow](#warning-C4307-integral-constant-overflow) 14 * [Warning C4003: the min, the max and the macro](#warning-C4003-the-min-the-max-and-the-macro) 15 * [The standard and the non-copyable types](#the-standard-and-the-non-copyable-types) 16 * [Which functions trigger which signals](#which-functions-trigger-which-signals) 17<!-- 18@endcond TURN_OFF_DOXYGEN 19--> 20 21# Introduction 22 23This is a constantly updated section where I'll try to put the answers to the 24most frequently asked questions.<br/> 25If you don't find your answer here, there are two cases: nobody has done it yet 26or this section needs updating. In both cases, try to 27[open a new issue](https://github.com/skypjack/entt/issues/new) or enter the 28[gitter channel](https://gitter.im/skypjack/entt) and ask your question. 29Probably someone already has an answer for you and we can then integrate this 30part of the documentation. 31 32# FAQ 33 34## Why is my debug build on Windows so slow? 35 36`EnTT` is an experimental project that I also use to keep me up-to-date with the 37latest revision of the language and the standard library. For this reason, it's 38likely that some classes you're working with are using standard containers under 39the hood.<br/> 40Unfortunately, it's known that the standard containers aren't particularly 41performing in debugging (the reasons for this go beyond this document) and are 42even less so on Windows apparently. Fortunately this can also be mitigated a 43lot, achieving good results in many cases. 44 45First of all, there are two things to do in a Windows project: 46 47* Disable the [`/JMC`](https://docs.microsoft.com/cpp/build/reference/jmc) 48 option (_Just My Code_ debugging), available starting in Visual Studio 2017 49 version 15.8. 50 51* Set the [`_ITERATOR_DEBUG_LEVEL`](https://docs.microsoft.com/cpp/standard-library/iterator-debug-level) 52 macro to 0. This will disable checked iterators and iterator debugging. 53 54Moreover, the macro `ENTT_ASSERT` should be redefined to disable internal checks 55made by `EnTT` in debug: 56 57```cpp 58#define ENTT_ASSERT(...) ((void)0) 59``` 60 61These asserts are introduced to help the users but they require to access to the 62underlying containers and therefore risk ruining the performance in some cases. 63 64With these changes, debug performance should increase enough for most cases. If 65you want something more, you can can also switch to an optimization level `O0` 66or preferably `O1`. 67 68## How can I represent hierarchies with my components? 69 70This is one of the first questions that anyone makes when starting to work with 71the entity-component-system architectural pattern.<br/> 72There are several approaches to the problem and what’s the best one depends 73mainly on the real problem one is facing. In all cases, how to do it doesn't 74strictly depend on the library in use, but the latter can certainly allow or 75not different techniques depending on how the data are laid out. 76 77I tried to describe some of the techniques that fit well with the model of 78`EnTT`. [Here](https://skypjack.github.io/2019-06-25-ecs-baf-part-4/) is the 79first post of a series that tries to explore the problem. More will probably 80come in future.<br/> 81In addition, `EnTT` also offers the possibility to create stable storage types 82and therefore have pointer stability for one, all or some components. This is by 83far the most convenient solution when it comes to creating hierarchies and 84whatnot. See the documentation for the ECS part of the library and in particular 85what concerns the `component_traits` class for further details. 86 87## Custom entity identifiers: yay or nay? 88 89Custom entity identifiers are definitely a good idea in two cases at least: 90 91* If `std::uint32_t` isn't large enough for your purposes, since this is the 92 underlying type of `entt::entity`. 93* If you want to avoid conflicts when using multiple registries. 94 95Identifiers can be defined through enum classes and class types that define an 96`entity_type` member of type `std::uint32_t` or `std::uint64_t`.<br/> 97In fact, this is a definition equivalent to that of `entt::entity`: 98 99```cpp 100enum class entity: std::uint32_t {}; 101``` 102 103There is no limit to the number of identifiers that can be defined. 104 105## Warning C4307: integral constant overflow 106 107According to [this](https://github.com/skypjack/entt/issues/121) issue, using a 108hashed string under VS could generate a warning.<br/> 109First of all, I want to reassure you: it's expected and harmless. However, it 110can be annoying. 111 112To suppress it and if you don't want to suppress all the other warnings as well, 113here is a workaround in the form of a macro: 114 115```cpp 116#if defined(_MSC_VER) 117#define HS(str) __pragma(warning(suppress:4307)) entt::hashed_string{str} 118#else 119#define HS(str) entt::hashed_string{str} 120#endif 121``` 122 123With an example of use included: 124 125```cpp 126constexpr auto identifier = HS("my/resource/identifier"); 127``` 128 129Thanks to [huwpascoe](https://github.com/huwpascoe) for the courtesy. 130 131## Warning C4003: the min, the max and the macro 132 133On Windows, a header file defines two macros `min` and `max` which may result in 134conflicts with their counterparts in the standard library and therefore in 135errors during compilation. 136 137It's a pretty big problem but fortunately it's not a problem of `EnTT` and there 138is a fairly simple solution to it.<br/> 139It consists in defining the `NOMINMAX` macro before to include any other header 140so as to get rid of the extra definitions: 141 142```cpp 143#define NOMINMAX 144``` 145 146Please refer to [this](https://github.com/skypjack/entt/issues/96) issue for 147more details. 148 149## The standard and the non-copyable types 150 151`EnTT` uses internally the trait `std::is_copy_constructible_v` to check if a 152component is actually copyable. This trait doesn't check if an object can 153actually be copied but only verifies if there is a copy constructor 154available.<br/> 155This can lead to surprising results due to some idiosyncrasies of the standard 156mainly related to the need to guarantee backward compatibility. 157 158For example, `std::vector` defines a copy constructor no matter if its value 159type is copyable or not. As a result, `std::is_copy_constructible_v` is true 160for the following specialization: 161 162```cpp 163struct type { 164 std::vector<std::unique_ptr<action>> vec; 165}; 166``` 167 168When trying to assign an instance of this type to an entity in the ECS part, 169this may trigger a compilation error because we cannot really make a copy of 170it.<br/> 171As a workaround, users can mark the type explicitly as non-copyable: 172 173```cpp 174struct type { 175 type(const type &) = delete; 176 type & operator=(const type &) = delete; 177 178 std::vector<std::unique_ptr<action>> vec; 179}; 180``` 181 182Unfortunately, this will also disable aggregate initialization. 183 184## Which functions trigger which signals 185 186The `registry` class offers three signals that are emitted following specific 187operations. Maybe not everyone knows what these operations are, though.<br/> 188If this isn't clear, below you can find a _vademecum_ for this purpose: 189 190* `on_created` is invoked when a component is first added (neither modified nor 191 replaced) to an entity. 192* `on_update` is called whenever an existing component is modified or replaced. 193* `on_destroyed` is called when a component is explicitly or implicitly removed 194 from an entity. 195 196Among the most controversial functions can be found `emplace_or_replace` and 197`destroy`. However, following the above rules, it's quite simple to know what 198will happen.<br/> 199In the first case, `on_created` is invoked if the entity has not the component, 200otherwise the latter is replaced and therefore `on_update` is triggered. As for 201the second case, components are removed from their entities and thus freed when 202they are recycled. It means that `on_destroyed` is triggered for every component 203owned by the entity that is destroyed. 204