Download

Can someone please explain this statement?

Hello i was just wondering.
Can anyone please explain this statement for me?


const bool bMatchIsOver = MyGameState && MyGameState->bMatchIsOver;


I dont get whats happening there.
MyGameState is a class and then a bool type the use is totaly new to me.

The full code looks like this.



/** Accept or reject a player attempting to join the server.
 *  Fails login if you set the ErrorMessage to a non-empty string. */
void AShooterGameMode::PreLogin(const FString& Options, 
				const FString& Address,
				const TSharedPtr<FUniqueNetId>& UniqueId,
				FString& ErrorMessage)
{
	Super::PreLogin(Options, Address, UniqueId, ErrorMessage);

	AShooterGameState* const MyGameState = Cast<AShooterGameState>(GameState);
	const bool bMatchIsOver = MyGameState && MyGameState->bMatchIsOver;
	const FString EndGameError = TEXT("Match is over!");

	ErrorMessage = bMatchIsOver ? *EndGameError : GameSession->ApproveLogin(Options);
}

Edit: My bad its a pointer :slight_smile:

Thanks for any explination.

It sets bMatchIsOver to TRUE if MyGameState is not a NULL pointer AND bMatchIsOver from MyGameState is TRUE, otherwise it sets bMatchIsOver to FALSE.

It must first check that MyGameState is not null or it cannot access bMatchIsOver (since it would be pointing to nothing)

Thank you for the explination its a clever expresion :slight_smile:
Cheers!

It is, and I had absolutely no idea that was valid in C++ (i didn’t realize that you could && a pointer and a bool in C++… it’s common to && totally unrelated things in less strictly typed languages, though…)

Ill do you one better.

NULL is actually typically defined as Zero (meaning the number 0). It is only a convention to be more clear when you see tests like :

if (pMyActor == NULL)

You can actually write them as

if (pMyActor)

and get just as far, in most compilers. If you happen to be in a non standard environment you can get in trouble with this, but since we are all windows, mac and linux users … =D