How can I call a function from an other file?

Hey Guys. I am kinda new to unreal engine, and I have troubles calling a function from an other file.

CamPawn.cpp

#include "GridCreator.h"

void ACamPawn::Pause()
{
     AGridCreator::SetPaused();
}

GridCreator.cpp

void AGridCreator::SetPaused()
{
	UE_LOG(LogTemp, Display, TEXT("asd"));
}

The code gives an error while compiling, and I can’t figure out what seems to be the problem here.

The error:

error C2352: ‘AGridCreator::SetPaused’: illegal call of non-static member function
note: see declaration of ‘AGridCreator::SetPaused’

I would be really thankful if you guys could help me.

C++ doesn’t work that way. (well, it can, but that’s probably not what you want to do)

You really need to have some understanding of Object Oriented Programming (OOP) to work with C++.

Basically, though – each file defines a class. A class cannot (generally) call functions inside another class, and even if that is what you did, it’s probably not what you want to do.

Inside Unreal, whenever you create an object, it creates that object based on it’s class.

Usually, you want to call a function on an object in the world, not on a class.

You might have any number of ACamPawn or AGridCreators in the world. You need to know which one of each one you want to access. So, to call a function in AGridCreator, you need a pointer to a specific instance of the AGridCreator, and then you can call it in that instance.

You can achieve what you are asking, using static functions. But if that’s what you actually wanted to do, you’d probably have already known how to do it, so I can only assume that you actually need to call inside an instance, not inside the class.

Unfortunately, these concepts are things that are not easily explained in a few short paragraphs. But I hope I have given you some search terms to help :slight_smile:

1 Like