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"

[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.

https://paste.ee/p/lRrQA (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.

Product Version: UE 4.19
more ▼

asked Apr 09 '19 at 11:34 AM in C++ Programming

avatar image

1 1 2

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

1 answer: sort voted first

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:

    //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) https://docs.unrealengine.com/en-us/Programming/Development/CodingStandard

more ▼

answered Apr 09 '19 at 12:53 PM

avatar image

1.2k 24 13 29

avatar image ue4-2019 Apr 09 '19 at 01:06 PM

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.

(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