Asserts that the [param condition] is [code]true[/code]. If the [param condition] is [code]false[/code], an error is generated. When running from the editor, the running project will also be paused until you resume it. This can be used as a stronger form of [method @GlobalScope.push_error] for reporting errors to project developers or add-on users. An optional [param message] can be shown in addition to the generic "Assertion failed" message. You can use this to provide additional details about why the assertion failed. [b]Warning:[/b] For performance reasons, the code inside [method assert] is only executed in debug builds or when running the project from the editor. Don't include code that has side effects in an [method assert] call. Otherwise, the project will behave differently when exported in release mode. [codeblock] # Imagine we always want speed to be between 0 and 20. var speed = -10 assert(speed < 20) # True, the program will continue. assert(speed >= 0) # False, the program will stop. assert(speed >= 0 and speed < 20) # You can also combine the two conditional statements in one check. assert(speed < 20, "the speed limit is 20") # Show a message. [/codeblock] [b]Note:[/b] [method assert] is a keyword, not a function. So you cannot access it as a [Callable] or use it inside expressions.
An optional [param message] can be shown in addition to the generic "Assertion failed" message. You can use this to provide additional details about why the assertion failed.
[b]Warning:[/b] For performance reasons, the code inside [method assert] is only executed in debug builds or when running the project from the editor. Don't include code that has side effects in an [method assert] call. Otherwise, the project will behave differently when exported in release mode.
[codeblock]
# Imagine we always want speed to be between 0 and 20.
var speed = -10
assert(speed < 20) # True, the program will continue.
assert(speed >= 0) # False, the program will stop.
assert(speed >= 0 and speed < 20) # You can also combine the two conditional statements in one check.
assert(speed < 20, "the speed limit is 20") # Show a message.
[/codeblock]
[b]Note:[/b] [method assert] is a keyword, not a function. So you cannot access it as a [Callable] or use it inside expressions.
如果給出了可選的 [param message] 參數,該資訊會和通用的“Assertion failed”消息一起顯示。你可以使用它來提供關於判斷提示失敗原因的其他詳細資訊。
[b]警告:[/b]出於對性能的考慮,[method assert] 中的程式碼只會在除錯版本或者從編輯器運作專案時執行。請勿在 [method assert] 呼叫中加入具有副作用的程式碼。否則,專案在以發行模式匯出後將有不一致的行為。
[codeblock]
# 比如說我們希望 speed 始終在 0 和 20 之間。
speed = -10
assert(speed < 20) # True,程式會繼續執行
assert(speed >= 0) # False,程式會停止
assert(speed >= 0 and speed < 20) # 你還可以在單次檢查中合併兩個條件陳述式
assert(speed < 20, "限速為 20") # 顯示消息。
[/codeblock]