x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Pointers, Garbage Collection, and Template classes

Hello!

I am experiencing some odd behavior with a custom template container class. What is happening is I fill this container with pointers to a class that inherits from AActor after I spawn them in the world like so

     for (int32 x = -Size.X; x <= Size.X; x++) {
         for (int32 y = -Size.Y; y <= Size.Y; y++) {
             for (int32 z = -Size.Z; z <= Size.Z; z++) {
                 AMyClass *NewMyClass = (AMyClass*) GetWorld()->SpawnActor(MyClass::StaticClass());
                 NewMyClass->SetActorLocation(FVector(x, y, z)); 
                 Grid.Add(NewMyClass, x, y, z);
             }
         }
     }

Then later on I attempt to call methods from MyClass.

     for (int32 x = Grid.GetMinX(); x <= Grid.GetMaxX(); x++) {
         for (int32 y = Grid.GetMinY(); y <= Grid.GetMaxY(); y++) {
             for (int32 z = Grid.GetMinZ(); z <= Grid.GetMaxZ(); z++) {
                 MyClass *Class = Grid.Get(x, y, z);
                 if (!Class) { continue; } // If nothing here, skip it
 
                 if (Class->IsDirty()) {
                     Class->DoStuff();
                 }
             }
         }
     }

But it seems when I call Grid.Get(...) I always retrieve a default version (without any properties initialized). I am still learning all the ins and outs of UE4 C++, but could this have something to do with UE4 garbage collector?

The header for my Grid class is as follows

 #pragma once
 #include <vector>
 
 template <typename T>
 class MYPROJECT_API TGrid
 {
 
 public:
     TGrid<T>::TGrid() :
         MinX(0),
         MinY(0),
         MinZ(0),
         MaxX(0),
         MaxY(0),
         MaxZ(0),
         SizeX(0),
         SizeY(0),
         SizeZ(0)
     {}
     TGrid<T>::~TGrid() {}
 
     // The following 6 methods return the min or max values of the grid
     int32 GetMinX() { return MinX; }
     int32 GetMinY() { return MinY; }
     int32 GetMinZ() { return MinZ; }
     int32 GetMaxX() { return MaxX - 1; }
     int32 GetMaxY() { return MaxY - 1; }
     int32 GetMaxZ() { return MaxZ - 1; }
 
     // Get the object at current valid location
     T Get(const int32 &x, const int32 &y, const int32 &z);
 
     // Add or replace at given grid coordinates (expand grid if necessary)
     void Add(T obj, const int32 &x, const int32 &y, const int32 &z);
 
 private:
     std::vector<std::vector<std::vector<T>>> Grid; // Pointer to 3D array of type T
     int32 MinX, MinY, MinZ, MaxX, MaxY, MaxZ; // Virtual array size
     int32 SizeX, SizeY, SizeZ; // Actual size
 
     // Check to make sure grid[x,y,z] is valid
     bool IsCorrectIndex(const int32 &x, const int32 &y, const int32 &z);
 
     // Set the object at current valid location
     void Set(T obj, const int32 &x, const int32 &y, const int32 &z);
 
     // Expand the grid size
     void ExpandGrid(const int32 &dMinX, const int32 &dMinY, 
                     const int32 &dMinZ, const int32 &dMaxX, 
                     const int32 &dMaxY, const int32 &dMaxZ);
 };
Product Version: Not Selected
Tags:
more ▼

asked Oct 25 '14 at 01:26 AM in C++ Programming

avatar image

S0rn0
71 8 13 12

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Had a problem posting the question, apparently it ended up being posted twice. Will mark this as asnwered and refer to the other question.

https://answers.unrealengine.com/questions/122902/custom-template-container-and-pointers.html

more ▼

answered Oct 27 '14 at 08:28 PM

avatar image

S0rn0
71 8 13 12

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question