Component instance reference

Hello :slight_smile:

I’m creating a C++ script and I want to get a reference to a component instance.

For example, imagine that I want to get the player camera, which is a UCameraComponent. I would create a UPROPERTY like this:

	UPROPERTY(EditAnywhere, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent* PlayerCamera;

However, I can’t assign it in the editor details panel like I would in Unity. I would expect to be able to drag and drop a component from an Actor on the World Outline to this property but it seems that’s not possible. Moreover, when clicking on the dropdown in the details panel, I don’t have any available to choose from.

From what I learned, it just isn’t possible to do it this way and the Unreal way is to create a UPROPERTY to store a reference to the Actor and then get the component in runtime. Although this might work for most cases, it’s still a bit of a memory inefficiency since you might not need the Actor reference after getting the component (yes, I know it’s just a pointer, but still…). Plus, how would you handle multiple components of the same class in the same actor? I assume you have to loop over them and try to find the one you want based on a priori knowledge of their order in the hierarchy or based on some of their known properties (maybe with tags)…

All of this seems kind of messy, so I ask you to please shed some light on what’s the (best) Unreal workflow for setting component references.

Thank you :slight_smile:

1 Like