Asserts that the [code]condition[/code] is [code]true[/code]. If the [code]condition[/code] 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 push_error] for reporting errors to project developers or add-on users. [b]Note:[/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. The optional [code]message[/code] argument, if given, is shown in addition to the generic "Assertion failed" message. You can use this to provide additional details about why the assertion failed. [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, "speed = %f, but the speed limit is 20" % speed) # Show a message with clarifying details [/codeblock]
Перевіряє, чи дорівнює [code]condition[/code][code]true[/code]. Якщо [code]condition[/code] дорівнює [code]false[/code], буде згенеровано помилку, і виконання програми буде припинено, поки ви не продовжите його. Ця функція виконується тільки у діагностичних збірках, або коли гра запускається в редакторі. Використовуйте її для діагностики, щоб переконатися, що вираз дорівнює [code]true[/code] протягом розробки. Якщо задано необов'язковий аргумент [code]message[/code], то він буде показаний у додаток до повідомлення «Assertion failed». Ви можете скористатися цим для надання додаткових відомостей щодо того, чому перевірку не було пройдено. [codeblock] # Припустімо, що ви хочете, щоб швидкість (speed) була між 0 і 20 speed = -10 assert(speed < 20) # Істина, програма продовжить виконання assert(speed >= 0) # Хибність, програму буде зупинено assert(speed >= 0 && speed <20) # Ви можете комбінувати два умовних вирази в одній перевірці assert(speed < 20, "speed =%f, але обмеження швидкості дорівнює 20" %speed) # Показує повідомлення з уточненням подробиць [/codeblock]
[b]Note:[/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.
The optional [code]message[/code] argument, if given, is shown in addition to the generic "Assertion failed" message. You can use this to provide additional details about why the assertion failed.
[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, "speed = %f, but the speed limit is 20" % speed) # Show a message with clarifying details
[/codeblock]
Якщо задано необов'язковий аргумент [code]message[/code], то він буде показаний у додаток до повідомлення «Assertion failed». Ви можете скористатися цим для надання додаткових відомостей щодо того, чому перевірку не було пройдено.
[codeblock]
# Припустімо, що ви хочете, щоб швидкість (speed) була між 0 і 20
speed = -10
assert(speed < 20) # Істина, програма продовжить виконання
assert(speed >= 0) # Хибність, програму буде зупинено
assert(speed >= 0 && speed <20) # Ви можете комбінувати два умовних вирази в одній перевірці
assert(speed < 20, "speed =%f, але обмеження швидкості дорівнює 20" %speed) # Показує повідомлення з уточненням подробиць
[/codeblock]