Struggling with Grabber Component and Enhanced Input System (Crypt Raider)

I’m following the UE5 course and I’m trying to draw the Debug Sphere at the End vector of the Grabber component. I’m using UE5.2 and because of that, I want to use the Enhanced Input System. Currently I implemented a AddOnScreenDebugMessage() functionality in CryptRaiderCharacter.cpp that triggers if the Grab action started.

The two functions in CryptRaiderCharacter.cpp:

void ACryptRaiderCharacter::Grabbed(const FInputActionValue& Value)
{
	const bool Grabbed = Value.Get<bool>();
	if(Grabbed){
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Grabbed"));

	}
}

void ACryptRaiderCharacter::Released(const FInputActionValue& Value)
{
	const bool Released = Value.Get<bool>();
	if (!Released) {
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Released"));
	}
}

The code in SetupPlayerInputComponent():

void ACryptRaiderCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
	// Set up action bindings
	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
	{
		...

		//Grabbing
		EnhancedInputComponent->BindAction(GrabAction, ETriggerEvent::Started, this, &ACryptRaiderCharacter::Grabbed);
		EnhancedInputComponent->BindAction(GrabAction, ETriggerEvent::Completed, this, &ACryptRaiderCharacter::Released);
	}
}

The problem is that the Scene Component Grabber does all the job when it comes to drawing the debug line:

void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction); 
	FVector Start = GetComponentLocation();
	FVector End = Start + GetForwardVector() * MaxGrabDist;
	
	DrawDebugLine(GetWorld(), Start, End, FColor::Red);
	//DrawDebugSphere(GetWorld(), End, 10, 10, FColor::Blue, true, 5);

	...
}

Since I only want to draw the Debug Sphere if I click on a button, how can I access

FVector End

from UGrabber.cpp in my CryptRaiderCharacter.cpp? Or is there a better way to do this?

I tried to implement the Input System in Grabber.h and Grabber.cpp but the build fails with the reason, that SetupPlayerInputComponent is not a member of USceneComponent.

You would need to add the grabber component in C++

ACryptRaiderCharacter::ACryptRaiderCharacter()
{
    //...
    Grabber = CreateDefaultSubobject<UGrabberComponent>(); //declared in header
}

Then use that how you wish

void ACryptRaiderCharacter::Grabbed(const FInputActionValue& Value)
{
	const bool Grabbed = Value.Get<bool>();
	if(Grabbed){
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Grabbed"))
        Grabber->//..do something;
	}
}

Thanks for the answer, I just tried it out and ran into the following problems:

Adding the Grabber component:

no instance of overloaded function “ACryptRaiderCharacter::CreateDefaultSubobject” matches the argument list

Using Grabber like this:

void ACryptRaiderCharacter::Grabbed(const FInputActionValue& Value)
{
	const bool Grabbed = Value.Get<bool>();
	if(Grabbed){
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Grabbed"));
		Grabber->DrawDebugSphere(GetWorld(), End, 10, 10, FColor::Blue, true, 5);
	}
}

causes

class “UGrabber” has no member “DrawDebugSphere”

Grabber doesn’t have that function. You’d need grab and release functions to call on it.

You also need to name the component(sorry I missed it)

CreateDefaultSubobject<...>(TEXT("GrabberComponent"));

Thank you, naming the component solved this problem, I think I’m almost there, now I only get the error:

CryptRaiderCharacter.cpp(54): error C4458: declaration of ‘Grabber’ hides class member.
CryptRaiderCharacter.h(80): note: see declaration of ‘ACryptRaiderCharacter::Grabber’

Edit:

In the header file I declared Grabber like this:

class UGrabber* Grabber;

You likely have the class name in the constructor which means you are creating a local variable instead of assigning your member variable

1 Like

That fixed it, thank you very much!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms