Reference to a component for another component

Hey, community

So, I’ve decided to become the ue4 guru, but I’ve stuck at the beginning :frowning:
Keep in mind following

class UCustomMovement : public UActorComponent
    UPROPERTY( Instanced, BlueprintReadWrite, Category = Impl)
    UMovementComponent* Movement;

In a character editor I’m creating this custom movement and movement component. Here’s the issue: I want to pass a reference of this movement component to my custom movement, but I can’t find a way to do this.

I’ve been developing using Unity for some time, hence the behaviour I want to get is a SerializedField property.

Patiently waiting for a master to help soon-to-be-master :slight_smile:

1 Like

I’m still trying to figure this out as well

I should add that I’m also looking for this. :slight_smile:

You mean something like this?

Movement = Cast<UMovementComponent>(GetOwner()->GetComponentByClass(UMovementComponent::StaticClass()));

I think that what vivalavladislav was trying to know, based on the mention of Unity’s SerializedField, is if it’s possible to add a reference, in the Details panel, to a certain component that belongs to a world actor.

I was able to do it by declaring a reference to the actor and the type of component to get.

ACustomActor* ActorReference;

TSubclassOf<class USceneComponent> TypeOfComponent;

These should now appear on the Details panel. Then, based on your GetComponentByClass suggestion, I did this:

UCustomComponent* comp = Cast<UCustomComponent>(ActorReference->GetComponentByClass(TypeOfComponent));

It works! :slight_smile: However, I’m still looking for better alternatives and perhaps a way to get a specific component reference if an actor has multiple components of the same type (like colliders). I wish to avoid getting all such components (using GetComponentsByClass) and then using their string names to differentiate them, though that might not be possible…

Ok, but basicly you are getting the Component of an actor with the mentioned function. I have no idea if this can be done in another way. I’m getting my component refs like this and it works well :open_mouth: But for now i didn’t need to get a component that exists more than once on an actor.

Don’t know if you’ve found a better way, but I do it through setting the reference manually using BP nodes.

For example, in my BeginPlay event, I manually get a reference to both components, and then use the “Set [Name of Variable]” node and pass the reference. I’d love for metadata to be able to describe where to find the component and allow direct setting in the details window, but that’s a good workaround.

Edit: Oh yeah, you might also want to the property “VisibleAnywhere” so that you can’t set it to something invalid from the Details panel.

I’m also interested in this. I didn’t want to go through the event graph because I want those references be set in the property window. So far all I can do is set the component’s name and wire the references in code.

I think this is the correct answer. i.e. the requested functionality does not exist but can be mocked to some degree. I’m also looking for a solution for exactly the multiple collider case.

Just to add a little explanation to this for beginners:
You create both variables (actor and component references). The component reference is not an particular instance, but after executing this you will have to find if that actor has that type of component or not.

If you want to do it just with “actor” and not asking for a component type, I suggest you call the same to get “comp” but instead of TypeOfComponent, you call YourComponentClass::StaticClass(). You can’t use “YourComponentClass” name directly on GetComponentByClass.

Maybe somebody finds this useful:

Using an FComponentReference instead.

If you add the class specifier EditInlineNew to one component you can create a reference to it from another component in the property window. However, it will create a new component, instead of referencing a pre-existing one.

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent), EditInlineNew)
class UFirstComponent : public UActorComponent
class USecondComponent : public UActorComponent
     UFirstComponent* FirstComponent;