Problem with Cast

I have an object that is programmed to interact with interfaces so we can test it. So far it has been fine, but when I tried to cast the game mode to the interface it freaked out.

return Cast<IFighterWorld>(GetWorld()->GetAuthGameMode());

I decided to try an intermediate step.

return Cast<IFighterWorld>(Cast<UObject>(GetWorld()->GetAuthGameMode()));

Thing is, it fails on the cast to UObject with the same error.
Here’s everything it spits out to stderr on it:

2>D:\programs\Epic Games\UE_4.17\Engine\Source\Runtime\CoreUObject\Public\Templates/Casts.h(193): error C2664: 'UObject *TCastImpl<From,To,ECastType::UObjectToUObject>::DoCast(UObject *)': cannot convert argument 1 from 'AGameModeBase *' to 'UObject *'
2>          with
2>          [
2>              From=AGameModeBase,
2>              To=UObject
2>          ]
2>  D:\programs\Epic Games\UE_4.17\Engine\Source\Runtime\CoreUObject\Public\Templates/Casts.h(193): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>  D:\home\xxxx\documents\Unreal Projects\BeatBoxers\Source\BeatBoxers\FighterCharacter.cpp(51): note: see reference to function template instantiation 'To *Cast<UObject,AGameModeBase>(From *)' being compiled
2>          with
2>          [
2>              To=UObject,
2>              From=AGameModeBase
2>          ]

So now I am confused.
It says it can’t cast an AGameModeBase into an UObject, but it is derived from UObject.
What is going on here?
I am thinking of just setting it to

return Cast<IFighterWorld>((UObject *)(GetWorld()->GetAuthGameMode()));

If I do that it proceeds past that point in the compilation and then fails on some of the unresolved external references to interface functions I have yet to implement, so I think it works, but it feels dirty.

Can anyone explain to me why I am unable to cast an AGameModeBase * to an UObject *?

Hi BigDub
You can definitely cast your game mode to UObject, I think your problems is with the cast concatenation and your original cast to interface, try to Cast your game mode to your real class instead of your interface, and make your methods overrides as public:

UObject* test = Cast(GEngine->GetWorld()->GetAuthGameMode()); // this works

Hope that helps
Cheers!

I am a little confused by your answer… but it got me to mess around with it some more and now I can not reproduce the problem.
Cast<IFighterWorld>(GetWorld()->GetGameMode()) is now working just fine. :confused:

Since 4.15, many objects are declared in the .h file with a forward declaration like this one:

class AGameModeBase myGameMode;*

When you try to cast myGameMode to a UObject (in your .cpp) the compiler don’t know it’s a UObject derived class until you add the specific .h header in the beginning of your .cpp file. Something like:

#include “GameFramework/GameModeBase.h”

You will encounter many problems like this one if you’re using older (??? prev 4.15) tutorials or forum entries. But you’ll get over it.

1 Like

“…so I think it works, but it feels dirty.” == half of all programming ever