What an i missing in regards to parenting?

CreateDefaultSubobject is supposed to be used in the constructor so the CDO has is set up correctly. You wanna use NewObject for anything that happens on runtime or editor time.

Just call NewObject with your Components and register/attach them manually.

Apparently there’s something I just don’t quite understand, and I’d appreciate if one of you could hear my situation and tell me what I’m falling to understand.

My APlayerCharacter class (APawn) has a pointer to a custom component, PlayerCharacterInventory (AActor). On construction, I CreateDefaultSubobject and this seems to work fine.

My APlayerCharacterInventory class has 5 pointers to skeletal mesh components for headgear, arm gear, etc… On construction, I CreateDefaultSubobject.

My character blueprint, derived of course from APlayerCharacter appears to work great in the blueprint editor. It has spawned the inventory component, inventory component has spawned my gear slots, and my gear slots allow me to spawn my gear meshes correctly.

HOWEVER, when I test this in game, I find that my slots ALL spawn at (0,0,0) regardless of my player’s position.

What am I misunderstanding? Are my components not actually parented correctly? If so, why is it automatically offsetting? And if not, what am I confusing here? As a child, shouldn’t it have the parents location, with offset if specified?

Denny, are you saying I’ll have to use BOTH CreateDefaultSubobject AND New object?

I mean, i have the CreateDefaultSubobject calls in my constructors. My character creates the inventory component, which in turn creates 5 skeletal mesh components. And my character acts fine in the editor, and even at runtime, with the exception of my equipment slots being spawned at 0,0,0.

Denny, are you saying I’ll have to use BOTH CreateDefaultSubobject AND New object?
No either or.

You wrote OnConstruction, that is different from the constructor. Can you clarify please.

Just again, in the constructor you would use CreateDefaultSubobject. OnConstruction you would use NewObject

Oh sorry! By that I meant in the constructor. I have it create default sub objects.

I must be missing a bit more to it than this though. Lol

Denny,
Let me show you my problem right now. It’s a bit different now that I’ve manually attached them. FYI, just moved, no Internet yet so going to have to show 3 screenshots I took with my phone:

This is my header:
https://postimg.org/image/66fcz1m13/

I’ve got my inventory component with 5 skeletal mesh components. Only other things are a reference to a data table which houses my item info and a test function for changing armor late . Pretty bare bones.

My constructor:
https://postimg.org/image/4hbwzu0xj/

Bare bones. Create the object and manually attached them to the inventory component.

Now here’s by problem:
https://postimg.org/image/uu7smjkcx/

My character blueprint which is derived from a ACHARACTER custom class contains the inventory component, it listed my equipment slots as None. When I change and compile my changes, it goes back to none.

WTH am I missing here?

Yeah, in your header file change the declaration like this:

from UPROPERTY(EditDefaultsOnly, Category = InventoryProperty)

to UPROPERTY(Category = "InventoryProperty", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))

Also if you want them attached you have to do it manually in your .cpp constructor like this:

YourComponent->AttachToComponent(AttachComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));

AttachComponent is the component you want to attach it to, YourComponent is one of the components you define with CreateDefaultSubobject.

Denny, please repost that solution as an answer (not a reply) so that I may tag it directly as the solution.

THANK YOU!

k, I will :slight_smile: