Core types

Godot has a rich set of classes and templates that compose its core,and everything is built upon them.

This reference will try to list them in order for their betterunderstanding.

Definitions

Godot uses the standard C99 datatypes, such asuint8_t,uint32_t,int64_t, etc. which are nowadays supported by everycompiler. Reinventing the wheel for those is not fun, as it makes codemore difficult to read.

In general, care is not taken to use the most efficient datatype for agiven task unless using large structures or arrays.int is usedthrough most of the code unless necessary. This is done because nowadaysevery device has at least a 32 bits bus and can do such operations inone cycle. It makes code more readable too.

For files or memory sizes,size_t is used, which is warranted to be64 bits.

For Unicode characters, CharType instead of wchar_t is used, becausemany architectures have 4 bytes long wchar_t, where 2 bytes might bedesired. However, by default, this has not been forced and CharType mapsdirectly to wchar_t.

References:

Memory model

PC is a wonderful architecture. Computers often have gigabytes of RAM,terabytes of storage and gigahertz of CPU, and when an application needsmore resources the OS will swap out the inactive ones. Otherarchitectures (like mobile or consoles) are in general more limited.

The most common memory model is the heap, where an application willrequest a region of memory, and the underlying OS will try to fit itsomewhere and return it. This often works best and is flexible,but over time and with abuse, this can lead to segmentation.

Segmentation slowly creates holes that are too small for most commonallocations, so that memory is wasted. There is a lot of literatureabout heap and segmentation, so this topic will not be developedfurther here. Modern operating systems use paged memory, which helpsmitigate the problem of segmentation but doesn't solve it.

However, in many studies and tests, it is shown that given enoughmemory, if the maximum allocation size is below a given threshold inproportion to the maximum heap size and proportion of memory intended tobe unused, segmentation will not be a problem over time as it willremain constant. In other words, leave 10-20% of your memory freeand perform all small allocations and you are fine.

Godot ensures that all objects that can be allocated dynamically aresmall (less than a few kb at most). But what happens if an allocation istoo large (like an image or mesh geometry or large array)? In this caseGodot has the option to use a dynamic memory pool. This memory needs tobe locked to be accessed, and if an allocation runs out of memory, thepool will be rearranged and compacted on demand. Depending on the needof the game, the programmer can configure the dynamic memory pool size.

Allocating memory

Godot has many tools for tracking memory usage in a game, especiallyduring debug. Because of this, the regular C and C++ library callsshould not be used. Instead, a few other ones are provided.

For C-style allocation, Godot provides a few macros:

memalloc()memrealloc()memfree()

These are equivalent to the usual malloc, realloc, free of the standard Clibrary.

For C++-style allocation, special macros are provided:

memnew( Class / Class(args) )memdelete( instance )memnew_arr( Class , amount )memdelete_arr( pointer to array )

which are equivalent to new, delete, new[] and delete[].

memnew/memdelete also use a little C++ magic and notify Objects rightafter they are created, and right before they are deleted.

For dynamic memory, the PoolVector<> template is provided. PoolVector is astandard vector class, and is very similar to vector in the C++ standard library.To create a PoolVector buffer, use this:

PoolVector<int>data;

PoolVector can be accessed using the [] operator and a few helpers exist for this:

PoolVector<int>::Readr=data.read()intsomeint=r[4]
PoolVector<int>::Writew=data.write()w[4]=22;

These operations allow fast read/write from PoolVectors and keep itlocked until they go out of scope. However, PoolVectors should be usedfor small, dynamic memory operations, as read() and write() are too slow for alarge amount of accesses.

References:

Containers

Godot provides also a set of common containers:

  • Vector

  • List

  • Set

  • Map

They are simple and aim to be as minimal as possible, as templatesin C++ are often inlined and make the binary size much fatter, both indebug symbols and code. List, Set and Map can be iterated usingpointers, like this:

for(List<int>::Element*E=somelist.front();E;E=E->next()){print_line(E->get());// print the element}

The Vector<> class also has a few nice features:

  • It does copy on write, so making copies of it is cheap as long asthey are not modified.

  • It supports multi-threading, by using atomic operations on thereference counter.

References:

String

Godot also provides a String class. This class has a huge amount offeatures, full Unicode support in all the functions (like caseoperations) and utf8 parsing/extracting, as well as helpers forconversion and visualization.

References:

StringName

StringNames are like a String, but they are unique. Creating aStringName from a string results in a unique internal pointer for allequal strings. StringNames are useful for using strings asidentifier, as comparing them is basically comparing a pointer.

Creation of a StringName (especially a new one) is slow, but comparisonis fast.

References:

Math types

There are several linear math types available in the core/mathdirectory.

References:

NodePath

This is a special datatype used for storing paths in a scene tree andreferencing them fast.

References:

RID

RIDs are resource IDs. Servers use these to reference data stored inthem. RIDs are opaque, meaning that the data they reference can't beaccessed directly. RIDs are unique, even for different types ofreferenced data.

References: