[C++] How can I access variables from a TArray of UChildActorComponents?

As part of my assignment (due in next month), I am trying to recreate a space game using C++ to exercise my knowledge in OOP - I admit that I am somewhat new to C++, but I have had far more extensive use of C# in the past. I’ve right now made a few classes to test things out.

I am trying to get a Pawn C++ class to access variables in the UChildActorComponents that I have added to it. The UChildActorComponents have their own type that inherit from AActor. The code I will provide will be in the following link - I apologize in advance if it’s very rough.

Paste.ee (lasts around one year from now).

The key points to take away from here is that in line 59 of TestPawn.cpp in the above link, I often receive nullptr errors. I don’t know how to resolve this.

p_im = this->cubeComps[0]->GetChildActorClass()->GetDefaultObject<ATestCubeComponent>()->p_isMaster;

Feel free to advise me also on how else I could do this code. The key requirements are for me to create classes that can move and fire, with each of them having at least one or multiple components.

You are not getting an instance, but the default object, which is a static object generated for all uobject classes. This object is not your instance.

Try this:

if(cubeComps.IsValidIndex(0))  
{  
   //Actually get the reference to the child actor and cast it to the type you want.  
   ATestCubeComponent* CC = Cast<ATestCubeComponent>(cubeComps[0]->GetChildActor());  
   p_im = CC->p_isMaster;   
} else p_im = false; //handle the out of bound index as you see fit.  

Also, try to stick to the Coding Standars for UE4 (I know is just for an assignment, but it makes it easier for people to help you out here) Coding Standard | Unreal Engine Documentation

Thanks for the suggestion and the explanation, but so far, I’ve not had much luck with this approach because of nullptr and memory access violation errors. I’m currently attempting a different approach.