I have figured out how to use the TOctree, and successfully implemented it. However I cannot seem to remove elements once they have been added. The function RemoveElement(FOctreeElementId ElementId); requires an ElementId. However I cannot find any way to get or make an ElementId.
Add element doesn’t return the ID even though the comment says otherwise:
/**
* Adds an element to the octree.
* @param Element - The element to add.
* @return An identifier for the element in the octree.
*/
void AddElement(typename TTypeTraits<ElementType>::ConstInitType Element);
The only useful constructor for ElementID is private:
I currently have a workaround but its not pretty. Basically I cloned all the code for the TOctree as TMyOctree and related classes/functions and then made changes necessary to remove an element. I would prefer not to have done this, and a real solution would be nice for anyone else trying to use TOctree.
I have had some success in figuring out the solution on my own.
In the OctreeSematic there is a function that is given an id when an object is created: FORCEINLINE static void SetElementId(const FOctreeElement& Element, FOctreeElementId Id) however this gives you a ‘const’ element that cannot be directly changed, so you can’t do anything with the Id here.
My solution is to use pointers to get this working. In my FOctreeElement I add the following:
I think I understand why it is crashing UE4. The struct MyIdStruct is always returning the same MyId no matter what element I am accessing. It looks like it always uses the same pointer for all elements. And I have yet to figure out how to fix that.
I just checked any my solution still works on 4.25. I am not sure if there has been any changes to the unreal engine side of the octree code since 4.25. Sometimes they change things. I could look into it if you are still having problems after looking at the source code. I would link my project but it’s just too big :-(.
I was able to put millions of cubes on the screen with no noticeable performance issues.