Assert statement

From Unreal Wiki, The Unreal Engine Documentation Site
(Redirected from Assert)
Jump to navigation Jump to search

The assert statement can be used to add a check for critical prerequisites. It should probably not be used in production code, as a failed assertion will intentionally make the engine crash with a message stating the source file and line number of the failed assertion.


An assertion can be added anywhere in executable code via the following syntax:


The condition must be an expression that evaluates to a bool value. The assertion fails if the condition evaluates to False at runtime. The term assert(false); will always fail and thus crash, while assert(true); never fails.

For Unreal Engine 3 it is recommended to use the built-in preprocessor macro `assert(condition); instead. This macro is only translated to the assert statement when the code is not compiled for final release. (similar to `log)

Usage considerations

Failed assertions always cause the game to crash immediately. This might be annoying for game clients, but it could be a serious problem on dedicated servers. Limit the use of assertions to special testing versions of your code, e.g. by using a preprocessor macro. If your code contains assertions, it might be a good idea to document them properly so your testers know about them.

Note that no code after the failed assertion statement will be executed, no matter how deeply nested your function calls are at that point. A failed assertion results in an immediate, but "semi-clean" shutdown. That means the game log file is properly closed, unlike when the game has a "hard" crash or is killed via the task manager. Network connections are not properly shut down though, so the affected client will time out, while clients of an affected server suddenly experience "infinite" lag.