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"

Custom (Template) Container and Pointers


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++) {
                 AMyClass *Class = Grid.Get(x, y, z);
                 if (!Class) { continue; } // If nothing here, skip it
                 if (Class->IsDirty()) {

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>
     TGrid<T>::TGrid() :
     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);
     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
more ▼

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

avatar image

71 8 13 12

avatar image BaderThanBad Oct 27 '14 at 05:10 PM

What does the constructor of MyClass look like?

avatar image S0rn0 Oct 27 '14 at 08:31 PM
 AMyClass::AMyClass(const class FPostConstructInitializeProperties& PCIP)
     : Super(PCIP),
     // Create a mesh with name
     Mesh = PCIP.CreateDefaultSubobject<UGeneratedMeshComponent>(this, TEXT("MyClassMesh"));
     RootComponent = Mesh; // Set mesh as root component
     //Component Setup
     Mesh->CastShadow = true;
     Mesh->bCastDynamicShadow = true;
     // Set material
     static ConstructorHelpers::FObjectFinder<UMaterial> Mat(TEXT("Material'/Game/StarterContent/Materials/M_Ground_Grass.M_Ground_Grass'"));
     if (Mat.Object != NULL) {
         UMaterial *Material = (UMaterial*)Mat.Object;
         UMaterialInstanceDynamic *Material_dyn = UMaterialInstanceDynamic::Create(Material, this);
         Mesh->SetMaterial(0, Material_dyn);

That is the constructor for MyClass.

avatar image BaderThanBad Oct 29 '14 at 01:26 PM

Could you also link the instance of TGrid in your code, where do you declare it and how. If it is not templatized on declaration that might cause some construction issues.

avatar image BaderThanBad Oct 29 '14 at 01:40 PM

My bad you already linked it. I don't have any ideas why it is doing that, but try replacing vector with ue4's TArray.

avatar image ogrewizardninja Oct 27 '14 at 09:52 PM

It might be that the code you have included has been quickly modified for this post, but you are in some instances referring to AMyClass and sometimes MyClass. So you're filling the container with AMyClass pointers, but you're retrieving MyClass pointers. Can you confirm what type your TGrid is taking?

avatar image S0rn0 Oct 28 '14 at 01:30 AM

Sorry, slightly modified yes. AMyClass is correct. As for Grid, is it a part of another class as a static member like so

 static TGrid<AMyClass*> Grid;

avatar image ogrewizardninja Oct 28 '14 at 04:28 AM

I notice that TGrid::Add(...) is commented saying that it can add or replace elements of the grid. Have you verified that your code is correctly adding each of the elements to a unique location? If so, what happens if you try to immediately access the elements of the grid after you store them? Do you still get default objects returned?

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

0 answers: sort voted first
Be the first one to answer this question
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