Download

How do I prevent non-visible member variables from being reset to the CDO defaults?

Hey everyone!

I have a component which I’m using to add clothing to a character’s material. It uses a few TSubclassOf variables where I assign the equipment the character should wear, which is then assigned to a private member variable so that I can unequip it later.

However, whenever I try to switch between armors, the CDO has already nulled out the private variables, and I can’t access certain texture and model data I need to use to remove the clothing properly from the model and its material in InitializeComponent.

Is there a way of preventing the CDO from resetting certain variables, or another way of doing this?

EDIT: Code:

.h

.cpp

There’s no way CDO could affect variables of other object. If your private member variables is not marked is UPROPERTY then it may be the garbage collector that clears out your objects.
If you could post some code snippets, it’d be easier to help.

I’m not sure which parts would be applicable, so I’ve added the full source of the component to the OP via pastebin. Thanks for having a look!

Ahh, I see now why mentioned CDOs in your question. If you get error in these lines


Unequip(EGameEquipmentSlot::EQT_Head);
Equip(Headwear.GetDefaultObject());

then the problem may be in the following. CDOs default variables is initialized using YourClass::YourClass(FObjectInitializer) constructor signature.
So if you are using constructor without arguments with your objects, that may be the case.

Ah, I should have been more clear - those lines work fine. It’s just that whenever InitializeComponent runs, any EquippedItem variable is null again, no matter what I set it to in Equip or Unequip.

Well, then… InitializeComponent runs once per instance of component - when it is created. So you will always have default values of your properties when it happens.