x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

When should I use Check()?

When should I use Check() ?

For example:

if (AMyGameMode* MyGameMode= UGameplayStatics::GetGameMode())

in this condition I use if() to check whether the cast succeeded. Can I use Check() to do the same thing? And if I can, then what's the difference between these two kind of check? And can I put everything I wanna check in the if() and never use Check()?

Thanks!

Chenglin

Product Version: UE 4.10
Tags:
more ▼

asked May 12 '16 at 04:17 AM in C++ Programming

avatar image

wcl1993
111 32 23 37

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort oldest

Hi Chenglin,

check() is like assert() in C. If the condition is false, it will stop your program dead with a log message, an error window and - if a debugger is attached - a break into the debugger. You cannot continue execution after this point. It should be used to trap programmer errors that violate assumptions made by the following code - for example, indexing into a TArray with an invalid index will cause a check failure, because indexing outside of an array is always a programmer error. Additionally, the compiler can optimise code following a check() with the assumption that the condition is guaranteed true.

In contrast, if() should be used to handle conditions that are possible (no matter how unlikely) during the execution of your code.

So in the above code, you should use an if(), as null is a valid result from UGameplayStatics::GetGameMode(), e.g. you're running on a client.

There is also the ensure() macro which is halfway between the two - i.e. you expect the condition to always be true, you want to handle it if it isn't, but you also want to be told about it. For example:

 AMyGameMode* MyGameMode = UGameplayStatics::GetGameMode();
 if (ensure(MyGameMode))
 {
     MyGameMode->Whatever();
 }

This should be read in the same way as if (MyGameMode) except that it will log and break into the debugger if MyGameMode is null, i.e. ensure() will return the same thing (converted to bool) as it has been passed. Unlike check(), you may continue execution of your program after this break.

Each use of ensure() will also only fire once, so future failures will not keep popping up in the debugger or log. If you want a message, you can use ensureAlways().

check() and ensure() also come in checkf() and ensureMsgf() flavours which allow you to write out a printf-style helpful message to indicate the nature of the failure, e.g.:

 if (ensureMsgf(MyGameMode, TEXT("Unexpected null game mode! (SomeState: %d)"), SomeState))

Hope this helps,

Steve

more ▼

answered May 12 '16 at 09:53 AM

avatar image

Steve Robb STAFF
2.8k 58 31 101

avatar image Henrik May 12 '16 at 11:29 AM

Good answer and thanks for letting me know about ensure()

Chenglin, if you want more information on the topic I'd recommend googling "C++ when to use assert" and such, since as Steve pointed out check() is like assert().

avatar image wcl1993 May 12 '16 at 03:00 PM

Thank you so much Steve and Henrik! The answer is very clear and now I think I need to check out the relative docs. Thanks again!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question