Download

Calling a method from Pawn inside of MovementComponent

Hello. I’m trying to call a function from my Pawn inside the Pawn’s attached MovementComponent.
Here’s a simplified version of what I’m trying to do:

MyPawn.h




#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"

class GAME_API AMyPawn : public APawn
{
	GENERATED_BODY()

public:

	float Get_MovementInputForward() const;

protected:

	float MoveInputForward;
	void AddMoveForwardInput(float AxisValue);



Pawn.cpp




#include "MyGame.h"
#include "MyPawn.h"
#include "MyPawnMovementComponent.h"

AMyPawn::AMyPawn()
{
	// Create an instance of our movement component, and tell it to update the root.
	OurMovementComponent = CreateDefaultSubobject<UMyPawnMovementComponent>(TEXT("MyPawnMovementComponent"));
	OurMovementComponent->UpdatedComponent = RootComponent;
}

void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	InputComponent->BindAxis("MoveForward", this, &AMyPawn::AddMoveForwardInput)
}

void AMyPawn::AddMoveForwardInput(float AxisValue)
{
	MoveInputForward = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}

float AMyPawn::Get_MovementInputForward() const
{
	return MoveInputForward;
}



MyPawnMovementComponent.h



#include "GameFramework/PawnMovementComponent.h"
#include "MyPawn.h"
#include "MyPawnMovementComponent.generated.h"

UCLASS(ClassGroup = Movement, meta = (BlueprintSpawnableComponent), ShowCategories = (Velocity))
class MyGame UMyPawnMovementComponent : public UPawnMovementComponent
{
	GENERATED_UCLASS_BODY()
		//GENERATED_BODY()

public:
	virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;

protected:
	float PlayerMoveInputForward
	virtual void GetPlayerInput() const;
	virtual void PreformMove(float playerInputForward) const;


MyPawnMovementComponent.cpp



#include "MyGame.h"
#include "MyPawnMovementComponent.h"

void UMyPawnMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	GetPlayerInput();
	PreformMove(PlayerInputForward);
}

void UMyPawnMovementComponent::GetPlayerInput() const
{
	PlayerMoveInputForward = // <-- This is the point where I don't know what to do.
}



Does anyone know how I can call Get_MovementInputForward() from AMyPawn inside of UMyPawnMovementComponent? I’ve tried googling around trying to find the proper way to do this. But every method I’ve found results in a compile error.

Any help would be much appreciated!

-Headstub

You can get the actor the component is attached to with GetOwner(), then cast that to your pawn class and call the function.

e.g.



#include "MyPawn.h"

...

void UMyPawnMovementComponent::GetPlayerInput() const
{
	auto* myPawn = Cast<AMyPawn>(GetOwner());
	if (myPawn)
	{
		PlayerMoveInputForward = myPawn->Get_MovementInputForward();
	}
}


If you’re calling this in the tick function though, probably better to store the AMyPawn pointer as a member variable in the component, to save getting and casting every frame. The AMyPawn could even give the component a reference to itself to store in that case, just another option.

Okay, thank you. I’ll try the pointer idea. It seems similar to the way the documentation tutorial teaches you to attach the MovementComponent using a pointer. I also got some good suggestions from cool a guy on the AnswerHub forums saying that a better practice for me would be to call the function from the pawn and not the other way around since movement components are supposed to be modular and able to be used by different types of pawns.

-Headstub