Adding SceneComponents in constructor, something going wrong

Hi all, I want to add a number of SceneComponents to my actor class. The components are just used for organization purposes. I use this code in the constructor of my actor class:



RootComponent = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("Root"));

ActiveGroup = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("ActiveGroup"));
InactiveGroup = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("InactiveGroup"));
LootLocations = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("LootLocations"));

ActiveGroup->AttachParent = RootComponent;
InactiveGroup->AttachParent = RootComponent;
LootLocations->AttachParent = RootComponent;


I’m experiencing some issues with this, though. When making a blueprint based on this class, adding static meshes and dragging it into the world, all is fine. However, after saving and reloading the map I always get these messages:



Error Failed import for SceneComponent /Game/Blueprints/Gathering/BP_RN_Chest.Default__BP_RN_Chest_C:ActiveGroup
Error Failed import for SceneComponent /Game/Blueprints/Gathering/BP_RN_Chest.Default__BP_RN_Chest_C:InactiveGroup
Error Failed import for SceneComponent /Game/Blueprints/Gathering/BP_RN_Chest.Default__BP_RN_Chest_C:LootLocations


Which leads me to believe I’m not using the correct way for adding scene components in code. Can anyone help?

Dear Nisshoku,

Can you show us the .h for your .cpp shown above?

Can you temporarily try something other than SceneComponent just to see if it is related to scenecomponent somehow?


**Must Attach to Root Component**

You are not setting the attach parent for these new components it looks like!



```


ActiveGroup->AttachParent = RootComponent;
InactiveGroup->AttachParent = RootComponent;
LootLocations->AttachParent = RootComponent;


```



:)

Rama

Thanks for replying! Here is the declarations in the .h file:



public:
	USceneComponent *	ActiveGroup;
	USceneComponent *	InactiveGroup;
	USceneComponent *	LootLocations;


I am setting the component’s AttachParents to the RootCompoent right?

I have tested with other components, like BoxComponent but the same result.

You need to add the UPROPERTY keyword to subobjects such as components, or the engine’s management internals will ignore it and blueprints will not be saved with said subobjects. This is what’s happening here – the initial construction goes fine, but when you save your level, the components are lost, leading to these errors when the level is subsequently loaded.



	UPROPERTY()
	USceneComponent *	ActiveGroup;
	UPROPERTY()
	USceneComponent *	InactiveGroup;
	UPROPERTY()
	USceneComponent *	LootLocations;


That’s just the very basics. There is a lot you can do with managed properties in the editor, like making them editable so these components don’t always have the same locations. This is done through the VisibleInstanceOnly/VisibleAnywhere/EditInstanceOnly/EditAnywhere property keywords. I won’t go into too much detail as this is just touching the tip of the iceberg and there are lots of examples and tutorials out there to delve in.

Also:

SceneComponentName->AttachTo(RootComponent), might be a bit clearer.

cmartel, thanks, that makes perfect sense. I didn’t make them UPROPERTIES because I figured I didn’t need them as variables for event graphs and such, but it makes sense that that also affects saving. Adding UPROPERTY() and recompiling the affected blueprints solved the issue!

, good point. I read somewhere that ->AttachParent = is the way to do it in constructor (ref)](Attaching one component to another crash: 'template mismatch during attachment' - C++ - Epic Developer Community Forums), but I think that is outdated and now that everything is working fine again I’ll switch back to AttachTo.

Thanks all!

UPROPERTY() is NOT just for “variables for event graphs and such” or saving. The main point of UPROPERTY() is to prevent from being garbage collected. UPROPERTY is a part of reflection mechanism in UE.