Problem with Interface

Good morning everyone.
I have 2 Actor classes called Red and Pink ghosts.
Both use the same MoveGhost feature.
I would like to create a GhostInterface interface where to put this function. and inherit it from the 2 actor classes of the Red and Pink ghosts.
My problem is as follows: in the MoveGhost function there is the following line of code:

AMyGameMode* GameMode = (AMyGameMode*)(GetWorld()->GetAuthGameMode());

Inside GhostInterface.cpp this line of code gives me an error in the word GetWorld (unidentified).

MoveGhost needs to be able to access information from the AMyGameMode class so I need to use the above line of code but it seems that within the interfaces I can’t.

I also tried replacing the line with:

AMyGameMode* GameMode = Cast<AMyGameMode>(this);

but it returns a null pointer.

Can you help me?
Thank you

game mode doesn’t exist on client

How can the interface access the variables that are in the MyGameMode class?

From the symptoms and code snippet I’m guessing you are working on multiplayer game. Game mode exists only on server. If you want access it’s data on client you can either share it through player controller (client’s controller can send a request to server and receive data in response) or store data in class that exists on both client and server like GameState.

The game I’m developing is Pacman.
i control pacman and 2 ghosts move to eat it.
This is what I would like to do in the IGhostInterface.cpp class:

FVector IGhostInterface::SelectWay(FVector2D PosizioneGhost){
AMyGameMode* GameMode = Cast<AMyGameMode>(this);
FVector UpSpazio = GameMode->GMaze->GetRelativeLocationByXYPosition(PosizioneGhost);
GameMode->PositionPacmanGrid=UpSpazio;
}

Return null pointer.

I tried to insert the data I need inside an AActor class (Maze) but even then I get a null pointer.

AMaze* Maze= Cast<AMaze>(this);
FVector UpSpazio = Maze->GetRelativeLocationByXYPosition(PosizioneGhost);
Maze->PositionPacmanGrid=UpSpazio;

In the IGhostInterface I should both access functions and data.

Hi,
I will have to make a couple of assumptions in order to attempt to answer your question.
I am assuming that what you want is a common implementation for the two actors and not an interface that allows you to implement different logic for the two actors.

For this purpose you should implement a common class e.g. GhostBase which inherits from actor and inherit you Red and Pink Ghosts from this common class. There are many other options for doing this but I think this will be the easiest one.

Your specific problem is that an interface does not have access to the world, which is why you are getting the compiler error.
In the second code line you are giving I am assuming that this is either the ghost or the interface which are unrelated to the game mode. The Unreal Cast template is a soft cast and will return nullptr when unable to cast.

I hope this helps you.

Good morning,
I try the GhostBase solution and let you know about the result.
Thank you

Good morning,
I solved it with the GhostBase suggestion.
I created a GhostBase where I implemented the 2 functions used by all ghosts (SelectWay and ChangeGhostDirection).
Also in the GhostBase I have added other functions which will then be implemented by the individual ghosts (ControlGhostPosition()).

virtual void ControlGhostPosition() {};
	FVector SelectWay(FVector2D PosizioneTarget, FVector2D PosizioneGhost, int32 DirezioneGhost);
	int32 ChangeGhostDirection(FVector2D PosizioneGhost, FVector NewPositionGhost);

A thousand thanks