@@ -20,11 +20,11 @@ more difficult to read.
20
20
In general, care is not taken to use the most efficient datatype for a
21
21
given task unless using large structures or arrays. ``int `` is used
22
22
through most of the code unless necessary. This is done because nowadays
23
- every device has at least a 32 bits bus and can do such operations in
23
+ every device has at least a 32- bits bus and can do such operations in
24
24
one cycle. It makes code more readable too.
25
25
26
- For files or memory sizes, ``size_t `` is used, which is warranted to be
27
- 64 bits .
26
+ For files or memory sizes, ``size_t `` is used, which is guaranteed to be
27
+ 64-bit .
28
28
29
29
For Unicode characters, CharType instead of wchar_t is used, because
30
30
many architectures have 4 bytes long wchar_t, where 2 bytes might be
@@ -63,7 +63,7 @@ remain constant. In other words, leave 10-20% of your memory free
63
63
and perform all small allocations and you are fine.
64
64
65
65
Godot ensures that all objects that can be allocated dynamically are
66
- small (less than a few kb at most). But what happens if an allocation is
66
+ small (less than a few KB at most). But what happens if an allocation is
67
67
too large (like an image or mesh geometry or large array)? In this case
68
68
Godot has the option to use a dynamic memory pool. This memory needs to
69
69
be locked to be accessed, and if an allocation runs out of memory, the
@@ -85,20 +85,21 @@ For C-style allocation, Godot provides a few macros:
85
85
memrealloc()
86
86
memfree()
87
87
88
- These are equivalent to the usual malloc, realloc, free of the standard C
89
- library.
88
+ These are equivalent to the usual `` malloc() ``, `` realloc() ``, and `` free() ``
89
+ of the C standard library.
90
90
91
91
For C++-style allocation, special macros are provided:
92
92
93
93
.. code-block :: none
94
94
95
- memnew( Class / Class(args) )
96
- memdelete( instance )
95
+ memnew(Class / Class(args))
96
+ memdelete(instance)
97
97
98
- memnew_arr( Class , amount )
99
- memdelete_arr( pointer to array )
98
+ memnew_arr(Class, amount)
99
+ memdelete_arr(pointer_to_array )
100
100
101
- which are equivalent to new, delete, new[] and delete[].
101
+ These are equivalent to ``new ``, ``delete ``, ``new[] `` and ``delete[] ``
102
+ respectively.
102
103
103
104
memnew/memdelete also use a little C++ magic and notify Objects right
104
105
after they are created, and right before they are deleted.
@@ -128,107 +129,132 @@ its storage strategy. Prefer ``Vector<>`` (or ``LocalVector<>``) over
128
129
``List<> `` unless you're sure you need it, as cache locality and memory
129
130
fragmentation tend to be more important with small collections.
130
131
131
- References:
132
- ~~~~~~~~~~~
132
+ References
133
+ ~~~~~~~~~~
133
134
134
135
- `core/os/memory.h <https://github.com/godotengine/godot/blob/master/core/os/memory.h >`__
135
136
136
137
Containers
137
138
----------
138
139
139
- Godot provides also a set of common containers:
140
-
141
- - Vector
142
- - List
143
- - Set
144
- - Map
145
-
146
- They aim to be as minimal as possible, as templates
147
- in C++ are often inlined and make the binary size much fatter, both in
148
- debug symbols and code. List, Set and Map can be iterated using
149
- pointers, like this:
150
-
151
- .. code-block :: cpp
152
-
153
- for(List<int>::Element *E=somelist.front();E;E=E->next()) {
154
- print_line(E->get()); // print the element
155
- }
156
-
157
- The Vector<> class also has a few nice features:
158
-
159
- - It does copy on write, so making copies of it is cheap as long as
160
- they are not modified.
161
- - It supports multi-threading, by using atomic operations on the
162
- reference counter.
163
-
164
- References:
165
- ~~~~~~~~~~~
140
+ Godot provides its own set of containers, which means STL containers like ``std::string ``
141
+ and ``std::vector `` are genearally not used in the codebase.
142
+
143
+ A 📜 icon denotes the type is part of :ref: `Variant <doc_variant_class >`. This
144
+ means it can be used as a parameter or return value of a method exposed to the
145
+ scripting API.
146
+
147
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
148
+ | Godot datatype | Closest C++ STL datatype | Comment |
149
+ +========================+==========================+=======================================================================================+
150
+ | ``String `` 📜 | ``std::string `` | **Use this as the "default" string type. ** ``String `` uses UTF-32 encoding |
151
+ | | | to improve performance thanks to its fixed character size. |
152
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
153
+ | ``StringName `` 📜 | ``std::string `` | Uses string interning for fast comparisons. Use this for static strings that are |
154
+ | | | referenced frequently and used in multiple locations in the engine. |
155
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
156
+ | ``Vector `` | ``std::vector `` | **Use this as the "default" vector type. ** Uses copy-on-write (COW) semantics. |
157
+ | | | This means it's generally slower but can be copied around almost for free. |
158
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
159
+ | ``LocalVector `` | ``std::vector `` | Closer to ``std::vector `` in semantics. In most situations, ``Vector `` should be |
160
+ | | | preferred. |
161
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
162
+ | ``Array `` 📜 | ``std::vector `` | Values can be of any Variant type. No static typing is imposed. |
163
+ | | | Uses shared reference counting, similar to ``std::shared_ptr ``. |
164
+ | | | Uses Vector<Variant> internally. |
165
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
166
+ | ``TypedArray `` 📜 | ``std::vector `` | Subclass of ``Array `` but with static typing for its elements. |
167
+ | | | Not to be confused with ``Packed*Array ``, which is internally a ``Vector ``. |
168
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
169
+ | ``Packed*Array `` 📜 | ``std::vector `` | Alias of ``Vector ``, e.g. ``PackedColorArray = Vector<Color> ``. |
170
+ | | | Only a limited list of packed array types are available |
171
+ | | | (use ``TypedArray `` otherwise). |
172
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
173
+ | ``List `` | ``std::list `` | Linked list type. Generally slower than other array/vector types. Prefer using |
174
+ | | | other types in new code, unless using ``List `` avoids the need for type conversions. |
175
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
176
+ | ``FixedVector `` | ``std::array `` | Vector with a fixed capacity (more similar to ``boost::container::static_vector ``). |
177
+ | | | This container type is more efficient than other vector-like types because it makes |
178
+ | | | no heap allocations. |
179
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
180
+ | ``Span `` | ``std::span `` | Represents read-only access to a contiguous array without needing to copy any data. |
181
+ | | | See `pull request description <https://github.com/godotengine/godot/pull/100293 >`__ |
182
+ | | | for details. |
183
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
184
+ | ``HashSet `` | ``std::unordered_set `` | **Use this as the "default" set type. ** |
185
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
186
+ | ``RBSet `` | ``std::set `` | Uses a `red-black tree <https://en.wikipedia.org/wiki/Red-black_tree >`__ |
187
+ | | | for faster access. |
188
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
189
+ | ``VSet `` | ``std::flat_set `` | Uses copy-on-write (COW) semantics. |
190
+ | | | This means it's generally slower but can be copied around almost for free. |
191
+ | | | The performance benefits of ``VSet `` aren't established, so prefer using other types. |
192
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
193
+ | ``HashMap `` | ``std::unordered_map `` | **Use this as the "default" map type. ** Preserves insertion order. |
194
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
195
+ | ``AHashMap `` | ``std::unordered_map `` | Array-based implementation of a hash map. Does not preserve insertion order. |
196
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
197
+ | ``OAHashMap `` | ``std::unordered_map `` | Does not preserve insertion order. |
198
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
199
+ | ``RBMap `` | ``std::map `` | Uses a `red-black tree <https://en.wikipedia.org/wiki/Red-black_tree >`__ |
200
+ | | | for faster access. |
201
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
202
+ | ``Dictionary `` 📜 | ``std::unordered_map `` | Keys and values can be of any Variant type. No static typing is imposed. |
203
+ | | | Uses shared reference counting, similar to ``std::shared_ptr ``. |
204
+ | | | Preserves insertion order. Uses ``HashMap<Variant> `` internally. |
205
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
206
+ | ``TypedDictionary `` 📜 | ``std::unordered_map `` | Subclass of ``Dictionary `` but with static typing for its keys and values. |
207
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
208
+ | ``Pair `` | ``std::pair `` | Stores a single key-value pair. |
209
+ +------------------------+--------------------------+---------------------------------------------------------------------------------------+
210
+
211
+ References
212
+ ~~~~~~~~~~
166
213
167
214
- `core/templates/vector.h <https://github.com/godotengine/godot/blob/master/core/templates/vector.h >`__
215
+ - `core/templates/local_vector.h <https://github.com/godotengine/godot/blob/master/core/templates/local_vector.h >`__
168
216
- `core/templates/list.h <https://github.com/godotengine/godot/blob/master/core/templates/list.h >`__
169
- - `core/templates/set.h <https://github.com/godotengine/godot/blob/master/core/templates/hash_set.h >`__
170
- - `core/templates/map.h <https://github.com/godotengine/godot/blob/master/core/templates/hash_map.h >`__
171
-
172
- String
173
- ------
174
-
175
- Godot also provides a String class. This class has a huge amount of
176
- features, full Unicode support in all the functions (like case
177
- operations) and utf8 parsing/extracting, as well as helpers for
178
- conversion and visualization.
179
-
180
- References:
181
- ~~~~~~~~~~~
182
-
217
+ - `core/templates/hash_set.h <https://github.com/godotengine/godot/blob/master/core/templates/hash_set.h >`__
218
+ - `core/templates/rb_set.h <https://github.com/godotengine/godot/blob/master/core/templates/rb_set.h >`__
219
+ - `core/templates/span.h <https://github.com/godotengine/godot/blob/master/core/templates/span.h >`__
220
+ - `core/templates/hash_map.h <https://github.com/godotengine/godot/blob/master/core/templates/hash_map.h >`__
221
+ - `core/templates/rb_map.h <https://github.com/godotengine/godot/blob/master/core/templates/rb_map.h >`__
222
+ - `core/templates/a_hash_map.h <https://github.com/godotengine/godot/blob/master/core/templates/a_hash_map.h >`__
223
+ - `core/templates/oa_hash_map.h <https://github.com/godotengine/godot/blob/master/core/templates/oa_hash_map.h >`__
183
224
- `core/string/ustring.h <https://github.com/godotengine/godot/blob/master/core/string/ustring.h >`__
184
-
185
- StringName
186
- ----------
187
-
188
- StringNames are like a String, but they are unique. Creating a
189
- StringName from a string results in a unique internal pointer for all
190
- equal strings. StringNames are useful for using strings as
191
- identifier, as comparing them is basically comparing a pointer.
192
-
193
- Creation of a StringName (especially a new one) is slow, but comparison
194
- is fast.
195
-
196
- References:
197
- ~~~~~~~~~~~
198
-
199
225
- `core/string/string_name.h <https://github.com/godotengine/godot/blob/master/core/string/string_name.h >`__
200
226
201
227
Math types
202
228
----------
203
229
204
- There are several linear math types available in the core/math
230
+ There are several linear math types available in the `` core/math ``
205
231
directory.
206
232
207
- References:
208
- ~~~~~~~~~~~
233
+ References
234
+ ~~~~~~~~~~
209
235
210
236
- `core/math <https://github.com/godotengine/godot/tree/master/core/math >`__
211
237
212
238
NodePath
213
239
--------
214
240
215
241
This is a special datatype used for storing paths in a scene tree and
216
- referencing them fast .
242
+ referencing them in an optimized manner .
217
243
218
- References:
219
- ~~~~~~~~~~~
244
+ References
245
+ ~~~~~~~~~~
220
246
221
247
- `core/string/node_path.h <https://github.com/godotengine/godot/blob/master/core/string/node_path.h >`__
222
248
223
249
RID
224
250
---
225
251
226
- RIDs are resource IDs. Servers use these to reference data stored in
252
+ RIDs are * resource IDs * . Servers use these to reference data stored in
227
253
them. RIDs are opaque, meaning that the data they reference can't be
228
254
accessed directly. RIDs are unique, even for different types of
229
255
referenced data.
230
256
231
- References:
232
- ~~~~~~~~~~~
257
+ References
258
+ ~~~~~~~~~~
233
259
234
260
- `core/templates/rid.h <https://github.com/godotengine/godot/blob/master/core/templates/rid.h >`__
0 commit comments