Download

Call function from another Class

Hi guys,
i thing i’m missing something fundamental about c++…

I’m trying to call a function declarated in Class A from Class B, but without any success.

This is the .h file with my function definition (FunctionTest):


UCLASS()
class ABot1Controller : public AAIController
{
	GENERATED_UCLASS_BODY()
public:
	UFUNCTION()
	virtual  void FunctionTest();
	


	virtual void Tick(float DeltaTime) OVERRIDE;

	UPROPERTY(EditAnywhere, Category = Location)
    FVector PosizioneTarget;


};

This is the .cpp file of my other class from which i’d like to call the function(FunctionTest)


#include "FPSProject.h"
#include "AIBot1.h"
#include "Bot1Controller.h"


AAIBot1::AAIBot1(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	AIControllerClass = ABot1Controller::StaticClass();
	PrimaryActorTick.bCanEverTick = true;
}


void AAIBot1::BeginPlay()
{
	Super::BeginPlay();
	
	if (GEngine)
	{
		//FString nomecontroller = GetController()->GetName();

		if (Controller)
		{
		FString nomecontroller = Controller->GetName();
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, nomecontroller);
		
		}
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Instanziato BOT 1"));
		
	}
}

void AAIBot1::Tick(float DeltaTime)
{
	Super::Tick(1);
	

}

I’m trying to call the function inside the Tick method, but i dont know how to reference to the function, i tried to use AIControllerClass, that should contain the reference to the class containing the function to be called, but without any success.
Any help?
Thanks in advance

Yeah, it’s better to read few books about C++ before trying to make something with it.
AIControllerClass is UClass in this case, which is obviously not ABot1Controller.
You should declare pointer to ABot1Controller in your AAIBot1, and either initialize it inside AAIBot1 or set to an initialized one, before calling function.
I also believe that’s not how controllers/pawns work, pawn should have reference to its controller. I didn’t deal with controllers/pawns yet actually, just basic stuff, so can’t tell how it’s done. First you tell your pawn that it’s controlled by specified controller, then when you want to call function of your custom controller, you have to do something like that(inside your AAIBot1)



ABot1Controller* Controller = Cast<ABot1Controller>(GetController());
Controller-YourFunction();


There’s info on controllers/pawns, but once again, i suggest you to start with reading C++ books.
https://docs.unrealengine.com/latest/INT/Programming/Gameplay/Framework/Controller/index.html
https://docs.unrealengine.com/latest/INT/Programming/Gameplay/Framework/Pawn/index.html

Regards.

Really Thanks BiggestSmile!

ABot1Controller* Controller = Cast<ABot1Controller>(GetController());
Controller-YourFunction();

It’s work thank you so much :smiley:

I like how you tell him to not try to learn something BEFORE you try to help him with it.