Identical Casts: one works; one doesn't

I have a class with a function where I Cast(), which works fine.

#include "GatherBall.h" 
#include "Wed3182Character.h"		 
//function 
void AGatherBall::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) 
{ 
AWed3182Character* MainCharacter = Cast<AWed3182Character>(OtherActor);	 
if ( (MainCharacter) )	//<-This works fine. 
}  

Then I have a second class with a function where I Cast(), which does not work.

#include "GameInfo.h" 
#include "Wed3182Character.h" 
//function	 
void UGameInfo::CheckForWin() 
{ 
class AActor* OtherActor; 
AWed3182Character* MainCharacter = Cast<AWed3182Character>(OtherActor);	 
if ((MainCharacter))      //<-This doesn't work. 
}  

I have been going round-and-round on this for a while. Any ideas?

I see the AActor * GetOwner() const syntax in the documentation. I am unclear on how to implement this. And on a understanding level. I thought that the point of casting was that that step is what found and linked the character in the world to the variable.

To my understanding

#include "Wed3182Character.h" 
class AActor* OtherActor; 
AWed3182Character* MainCharacter = Cast<AWed3182Character>(OtherActor);

is the method of creating the reference that allows GameInfo to know about MainCharacter. MainCharacter being a stand in for AWed3182Character. What more is needed? I do not know what other details to tell you.

on the first function, you are passing in a pointer to an Actor.

on the second function, you are creating a blank Actor pointer out of nothing. The only thing you could cast it as would be AActor and UObject.

Before you can cast, you would have to find your other actor in the world, then set OtherActor equal to it

For example:

OtherActor = GetOwner();

then cast it.

GetOwner() was just an example, you don’t need to use this.

I think we need more details.

How does GameInfo know about MainCharacter? Where does the reference come from?

You should do research about functions and references and gain a general understanding of how they work.

Here is how you could fix this.

On your GatherBall class, you need to pass a reference to the GameInfo class, so you could add a parameter in your CheckForWin functions

In your header:

void UGameInfo::CheckForWin(AActor* OverlappedActor)

In your cpp:

 #include "GameInfo.h" 
 #include "Wed3182Character.h" 
 //function     
 void UGameInfo::CheckForWin(AActor* OverlappedActor) 
 { 
 class AActor* OtherActor;
OtherActor = OverlappedActor; 
 AWed3182Character* MainCharacter = Cast<AWed3182Character>(OtherActor);     
 if ((MainCharacter))      //<-This doesn't work. 
 }

I feel that I have a pretty good understanding of functions and references, I have done considerable research on this particular issue, and I considered going to a forum to get help one of the last resort steps to take, as oft answers are useless, insulting, or what ever. By the way your recommended solution did not work. Seemingly, your solution is to use the needed reference to obtain the needed reference.

When you call the function CheckForWin, you need to give it a reference to the Actor you are checking, this is where OverlappedActor is used.

I’m not sure where or how CheckForWin is getting called so its hard for me to give advice here.

Hi, what casting does is checking whether a reference to something is of the type you’re casting it to. If you just do class AActor* OtherActor;
then this will not point to anything valid, so casting it to anything will again output something invalid. In other words if you have OtherActor and want to cast it to AWed3182Character, then this will only succeed if OtherActor is a reference to an AWed3182Character. So casting won’t give you a reference to an AWed3182Character, it just checks whether a given reference points to an AWed3182Character.

One way you could get a reference to an existing AWed3182Character would be via UGameplayStatics::GetActorOfClass.