How do I call a function from Another Class?

Hello Unreal Community,
I’m new to C++ programming any advice or examples of my question would be very appreciated.
I have 2 classes, - Class A extend Pawn and - Class B extend Actor.
I’ve created a function in Class B (Actor) and in trying to call that function from Class A (Pawn);
Is this possible?
I tried few Methods which can Compile but when start Play - UE4 Crash.

Thanks for your help in advanced.


#include “MyPawn.h”
#include “MainShip.h”
#include “Kismet/GameplayStatics.h”

void AMyPawn::Tick( float DeltaTime )
// MainShip Movement

	 AMainShip* BaseShip = Cast<AMainShip>(GetClass());
	SpeedFactor = BaseShip->GetShipSpeedFactor();

	//  Method 2
	// AMainShip BaseShip;
	// SpeedFactor = BaseShip.GetShipSpeedFactor();

	//  Method 3
	// AMainShip* BaseShip = Cast<AMainShip>(GetMovementBaseActor(this));
	// SpeedFactor = BaseShip->GetShipSpeedFactor();

	//  Method 4
	// AMainShip* BaseShip = Cast<AMainShip>(UGameplayStatics::GetPlayerPawn(this, 0));
	// SpeedFactor = BaseShip->GetShipSpeedFactor();

	//  Method 5
	// AMainShip* BaseShip = Cast<AMainShip>(UGameplayStatics::GetObjectClass(this));
	// SpeedFactor = BaseShip->GetShipSpeedFactor();

            //  Method 6
	// AMainShip* BaseShip = Cast<AMainShip>(GetController());
	// SpeedFactor = BaseShip->GetShipSpeedFactor();

	if (bSailsUp)
		FVector NewLocation = GetActorLocation() + GetActorForwardVector() * SpeedFactor;


float AMainShip::GetShipSpeedFactor()
return ShipSpeedFactor;

what class is it and how do you spawn AMainShip? You use default pawn? This is single player game?

if Class B is derived from Class A, simply call the function you want directly (i.e. SpeedFactor = GetShipSpeedFactor():wink:

If Class A and Class B are unrelated:

AMainShip* BaseShip = Cast<AMainShip>(*here goes a reference to an object that AMainShip derived from*);

So that reference to that actor can be obtained through a uproperty or an actor iterator.

Calling functions from another class by including the class in your current one should be fine. Some of the other stuff you’re doing is a little strange, though.

First, as far as I know, you don’t need to include things that are in the base engine code, e.g. the kismet library. As an aside, the kismet library is mostly for blueprints and BP nodes, as far as I know. They’re usually just C++ functions wrapped in another function.

Second, compiling just means you don’t have any syntactic errors. There can still be a number of problems with your code, especially in the case of null pointers. It would be helpful if you posted your crash report, but I’m guessing it has to do with that. The issue I see is your setting of BaseShip. GetClass(), if I remember, just returns the class of the passed object. It looks like you want to create a new AMainShip object. To do that, check out this link.

Last, I’m not entirely sure you want to do that, either, though. Is the ship supposed to be your main character- the thing you play as in your game? If so, you should probably either place an instance of the actor into your actual level and possess it or make a spawn point and set the ship as your default actor.