Announcement

Collapse
No announcement yet.

Can someone please explain this statement?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Can someone please explain this statement?

    Hello i was just wondering.
    Can anyone please explain this statement for me?
    Code:
    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.
    Code:
    /** 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

    Thanks for any explination.
    Last edited by WCode; 04-15-2014, 11:11 AM.

    #2
    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)
    Game Developer @ LODZERO - My Twitter | tomlooman.com UE4 samples & tutorials | C++ Survival Game (Open-Source)

    Comment


      #3
      Thank you for the explination its a clever expresion
      Cheers!

      Comment


        #4
        Originally posted by WCode View Post
        Thank you for the explination its a clever expresion
        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..)

        Comment


          #5
          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
          About Me | JavaScript and UE4 | Exodus | Code Notes

          Comment

          Working...
          X