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.
Khẳng định [param condition] là [code]true[/code]. Nếu [param condition] là [code]false[/code], lỗi sẽ phát sinh. Khi chạy từ trình chỉnh sửa, dự án đang chạy sẽ dừng lại đến khi bạn tiếp tục nó. Hàm này có thể được sử dụng như một dạng mạnh hơn của [method @GlobalScope.push_error] để báo cáo lỗi cho nhà phát triển dự án hoặc người sử dụng tiện ích bổ sung. Tùy chọn [param message] có thể được hiển thị thêm thông báo: "Assertion failed"(Xác nhận thất bại). Bạn có thể sử dụng thông tin này để cung cấp thêm chi tiết về lý do tại sao xác nhận thất bại. [b]Cảnh báo:[/b] Vì lý do hiệu năng, mã bên trong [method assert] chỉ thực thi trong gỡ lỗi hoặc khi chạy dự án từ trình biên tập. Đừng chứa mã có tác dụng phụ trong lệnh gọi [method assert]. Nếu không, dự án sẽ hoạt động khác khi được xuất bản ở chế độ phát hành. [codeblock] # Ví dụ chúng ta luôn muốn giá trị của speed ở giữa 0 và 20. var speed = -10 assert(speed < 20) # True, chương trình sẽ tiếp tục. assert(speed >= 0) # False, chương trình sẽ dừng. assert(speed >= 0 and speed < 20) # Bạn có thể kết hợp hai điều kiện lại vào một lệnh kiểm tra. assert(speed < 20, "the speed limit is 20") # Hiển thị thông điệp. [/codeblock]
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.
Tùy chọn [param message] có thể được hiển thị thêm thông báo: "Assertion failed"(Xác nhận thất bại). Bạn có thể sử dụng thông tin này để cung cấp thêm chi tiết về lý do tại sao xác nhận thất bại.
[b]Cảnh báo:[/b] Vì lý do hiệu năng, mã bên trong [method assert] chỉ thực thi trong gỡ lỗi hoặc khi chạy dự án từ trình biên tập. Đừng chứa mã có tác dụng phụ trong lệnh gọi [method assert]. Nếu không, dự án sẽ hoạt động khác khi được xuất bản ở chế độ phát hành.
[codeblock]
# Ví dụ chúng ta luôn muốn giá trị của speed ở giữa 0 và 20.
var speed = -10
assert(speed < 20) # True, chương trình sẽ tiếp tục.
assert(speed >= 0) # False, chương trình sẽ dừng.
assert(speed >= 0 and speed < 20) # Bạn có thể kết hợp hai điều kiện lại vào một lệnh kiểm tra.
assert(speed < 20, "the speed limit is 20") # Hiển thị thông điệp.
[/codeblock]