Access member function of another C++ component

Hi. I am learning C++ development in Unreal and wanted to exercise a bit with programming patterns but I am stuck with a seemingly simple problem.
I have a Grabber Scene Component that I use to grab an object. My goal was to reuse component method that was doing SweepSingleByChannel() in other components. So I can have one component for grabbing and another for using objects (for door/chest opening, push button etc.). I’ve created an ObjectHandler Scene Component that was attached to the Players camera (the same way as the previous Grabber component) and moved this method there from Grabber:

bool UHandInteraction::CheckObjectInReach(FHitResult& HitResult) const
{
	FVector Start = GetComponentLocation();
	FVector End = Start + GetForwardVector() * MaxGrabDistance;
	DrawDebugLine(GetWorld(), Start, End, FColor::Purple);

	
	FCollisionShape Sphere = FCollisionShape::MakeSphere(GrabRadius);
	return GetWorld()->SweepSingleByChannel(
		HitResult,
		Start, End,
		FQuat::Identity,
		ECC_GameTraceChannel2,
		Sphere
	);
}

But now when I try to access it from Grabber with

UHandInteraction* HandInteraction = NewObject<UHandInteraction>();
	HandInteraction->CheckObjectleInReach(HitResult);

I have this error:

FObjectInitializer::Get() can only be used inside of UObject-derived class constructor.

This is strange because USceneComponent inherits from UActorComponent and that from UObject.

Can You point me to some good pattern here. My idea is to display a message to the Player when he points at something that might by grabbed (“Grabb object”) or used (“Use object”).

Thanks

I’m not sure what the actual issue is, but when you create a component in runtime, you have to register it. Maybe after you do, it will be available.

UHandInteraction* HandInteraction = NewObject<UHandInteraction>();
HandInteraction->RegisterComponent();
1 Like

The type of interaction possible should be tied to the object you’re interacting with. The interacting character does not need to do anything other than sending the interaction request. This way you only need 1 interaction component, and you can reuse this component across all actors that are interactable.

Any object that is interactable should be marked as such, ideally by creating a component or interface for interactables that can be added to any actor that you wish to interact with. I personally use a component, since I can tie widgets to it.
This component can hold multiple types of interactions. You could have a series of booleans exposed to blueprint called “canGrab” or “canBasicInteract” and so on. Add a UMG widget to this interactable component to display information to the player if needed.
The benefit here is that opening a chest and pressing a button are basically the same interaction, but you can for individual actors have an interaction text that says “Open Door” and “Press Button”. You can also have different interactions happen depening on the interactable actor. Again, the interacting character does not need to care about anything other than sending the request, so it’s important that we keep it that way.

When interacting with an interactable, you can get the actor in question from your HitResult. I would suggest using a LineTrace, so you interact with whatever you’re looking at. From there you can call whatever functionality needs to be done. Call the actors through the interaction component.

If you do it like this, any actor that has an interactable component, can be interacted with, it will show a widget to the player and it will be easy to customize.

Hope this makes some sense.

EDIT:
To put this into some perspective, imagine you have a game with multiple weapons.
When the player presses left click, they expect their gun to shoot, their sword to swing and their bow to draw. The character does not care what weapon is equipped, it just wants to tell the weapon to attack. Same sorta deal here, just with interaction.

1 Like

Thanks for that tip. I am quite beginner in both C++ and Unreal but I will give it a try. The problem I imagine right now is that (I guess) I will have to get reference to player’s camera component from Grabable objects so I can handle it properly.