I need to use a container that does not move its elements into memory when there is deletion or addition. so a TArray does not match me and so logically std :: List is what comes to mind first. but I understood that it was not good to use the STL with UE so I am oriented towards the API UE and I see TList … but the contents of the documentation is non-existent and I can not understand how do the constructors of these containers … I think it is necessary to go through “Init ()” … but in my case I have to initialize my list in a constructor and the item stored in this list does not have of default constructor …
In the documentation there is Array, TMap and TSet but not TList … is there another container similar to std :: List, or a documentation that I would not have seen?
I’m not sure how a TArray moves elements into memory when it is reordered, or has deletions/aditions. The array is an array of pointers to the full object, so removing a pointer from the list, is just removing an int.
To use TList, I searched the github repo, and found this example
TList<FGuid>* NewElement = new TList<FGuid>(TaskStateMessage->TaskGuid, NULL);
In this example, it looks a TList object is just one node in a linked list. By creating this node in the linked list, you also specify the next node. This doesn’t look like a std::list clone. I think TArray, is the closest you’d get to that, as it is a dynamic array.
Ah so TArray does not work as a std::vector. so it’s just the pointer that is moving in memory. cool then I can use the TArray. thank you to you for this enlightenment.
edit: I just returned to see the description of TArray and there is this:
A dynamically sized array of typed elements. Makes the assumption that your elements are relocate-able; i.e. that they can be transparently moved to new memory without a copy constructor. The main implication is that pointers to elements in the TArray may be invalidated by adding or removing other elements to the array. Removal of elements is O(N) and invalidates the indices of subsequent elements.
I do not speak English natively (I use google trad to avoid making mistakes) but it seems to me that it is specified that the elements must be able to be moved in memory because otherwise it will break the pointer that targets them. do I misunderstand?
The objects don’t move in memory, the pointers in the array move when elements move. This is similar to how vectors work in C++, and Lists work in C#.
The object is not stored in the array, just the pointer to it.
When the list has an object removed, the object still exists, and a pointer to the object remains the same, but its position in the array is shifted.
Arrays can hold primitive data, likestructs.
When the Array type is for an object, like Object, Actor, Component: The array holds pointers to those objects.
so if I have pointers to the objects pointed by the pointers stored by the TArray, there will be no link loss?
and what do you mean by “Arrays can only hold primitive data”, I can not store instances of class? what container should I use if I try to store complex objects in the same way as a std :: List? that is to say in order to be able to have pointers on these elements without loss of link.
and thank you again for taking time to answer me ^^.
It may be possible to store objects in arrays, but I recommend always storing pointers.
Here’s an example:
You have a list of 5 actors (you really have a list of 5 actor pointers).
Now, if Actor 5 is deleted from the game, like a player dying, or falling into lava. The list will still have a reference to that location, but the location doesn’t hold the actor anymore, so the reference is null.
If you also removed Actor 2 from the list, Actor 2 still exists.
If you moved Actor 1 to be after Actor 3 in the list, the Actors would not move in memory, just your pointers would move in the list.
This is what your list and memory would look like after those changes
I understand very well the principle of a pointer array but as I know that std :: list did exactly what I needed and I assumed that TList was doing it as well. I prefer to avoid creating my own pointer array when a container already does it for me ^^. and if TArray does it, then no worries, because if I create a TArray containing my own pointers, requiring me to use “new” to dynamically create the stored objects, it also gives me the responsibility to destroy them (except in using the smart pointer, but is it provided bye UE or can I use the std ?. anyway, thank you for taking as much time (even to make a chart, even if was not necessary).
Conclusion I will use TArray and see what it gives ^^.