Allowing actor component to only be added to a class of ACharacter

Hello everyone,

Currently I’m making a component in C++ but I only want to allow it to be added to C++ and blueprint classes that inherit from ACharacter since this class is required by the component to make it work. Is there some sort of way to enforce this?

Not sure if it will work, but try the Within=ACharacter class specifier.

Hey dbuchoff, thanks for your answer. Sorry I’m responding so late! I tried to do the Within=ACharacter but I seem to get the error “Within class ‘ACharacter’ not found”. Including the Character header does not seem to help either. Any ideas?

Try using the name presented in the editor (just “Character”) because in the example, instead of using “APlayerController” it just uses “PlayerController”. Just a shot in the dark.

To save anyone else time testing: this doesn’t work; you’ll get an error message when you try to add the component saying that components with “within” cannot be attached.

Took me maybe five minutes to test this; why was it even suggested in the first place?

Hey Cyborgking-

Creating a component that can only be recognized by a specific class/blueprint type is somewhat counter intuitive to the reason for components, which is to define functionality that can be added anywhere. I have entered a feature request for a UCLASS specifier to allow the behavior you’re seeking here: Unreal Engine Issues and Bug Tracker (UE-36617) . You can track the report’s status as the issue is reviewed by our development staff.

Cheers

Thanks for your answer. I understand that it generally goes against the idea of components. Sure, a lot of these dependencies can be fixed by passing through some variables from the actor that is holding them to the component when the component is set up. You can send interfaces through that setup as well which contains functions that the component might need. But sometimes you may just want to get the owner of the component and work with what you have there. In that case, to prevent errors, I think it would be usefull if you could set something up like that.

Exactly what I was looking for. I guess it can be considered counter intuitive, but it also can be considered as a specialized component designed to work only if attached to a ACharacter for example. If you have Unity experience, the equivalent is the [RequireComponent] attribute. Looking forward to seing this in Unreal too.

Similarly knowing that the functionality you’re adding is for a Controller as opposed to a Character could be very important as well. You have these restrictions built into your own engine. Look at BehaviorComponent and how all the Behavior Tree nodes require GetAIOwner to return a valid AAIController that is a cached value of the Actor the Component is attached to.

If I’m creating a Component, I’m creating functionality for a subset of Actors, but that doesn’t mean it’s appropriate for all Actors. Sometimes that subset is much easier to define as Character or Controller or even AIController.

This isn’t counter intuitive at all to how Components should work. It’s a required feature in order for Component Systems to function properly.

I also agree with this request. Creating a component for a (possibly large) subset of actors totally makes sense (and would actually clean up my project)
The issue link does not appear to work. Do we have any news on this? Was it rejected?

Hey -

The reported feature request is still open and is undergoing a review process by the development team for consideration.

I completely agree with Steven. Actor is simply to broad for practically any components to be considered to fit freely anywhere. This isnt much of an issue i guess, but could be nice to say filter out components that are visible in the components dropdown on actors in BP. Put a vote in on the issue in the name of everything in its place

What’s the resolution of this? The issue doesn’t exist anymore so I assume it was shot down?

Sorry for reviving this topic, but… Is there any modern way to achieve this?

Hi, just passing by. Any news on this topic?

I have recently seen ‘UPawnComponent’, being part of the Modular Gameplay Plugin, but it is in Beta so…

But it exists anyway.