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.
Memastikan bahwa [param condition] adalah [code]true[/code]. Jika [param condition] adalah [code]false[/code], sebuah error akan dihasilkan. Ketika menjalankan dari editor, proyek yang sedang berjalan juga akan dihentikan sementara sampai kamu melanjutkannya. Ini dapat digunakan sebagai bentuk yang lebih kuat dari [method @GlobalScope.push_error] untuk melaporkan error kepada pengembang proyek atau pengguna add-on. Sebuah [param message] opsional dapat ditampilkan sebagai tambahan dari pesan umum "Pernyataan gagal". Kamu dapat menggunakan ini untuk memberikan detail tambahan tentang mengapa pernyataan gagal. [b]Peringatan:[/b] Untuk alasan performa, kode di dalam [method assert] hanya dieksekusi di dalam debug build atau ketika menjalankan proyek dari editor. Jangan menyertakan kode yang memiliki efek samping dalam pemanggilan [method assert]. Jika tidak, proyek akan berperilaku berbeda ketika diekspor dalam mode rilis. [codeblock] # Bayangkan kita selalu ingin kecepatan berada di antara 0 dan 20. var speed = -10 assert(speed < 20) # Benar, program akan berlanjut. assert(speed >= 0) # Salah, program akan berhenti. assert(speed >= 0 and speed < 20) # Kamu juga dapat menggabungkan dua pernyataan kondisional dalam satu pengecekan. assert(speed < 20, "batas kecepatan adalah 20") # Menampilkan pesan. [/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.
Sebuah [param message] opsional dapat ditampilkan sebagai tambahan dari pesan umum "Pernyataan gagal". Kamu dapat menggunakan ini untuk memberikan detail tambahan tentang mengapa pernyataan gagal.
[b]Peringatan:[/b] Untuk alasan performa, kode di dalam [method assert] hanya dieksekusi di dalam debug build atau ketika menjalankan proyek dari editor. Jangan menyertakan kode yang memiliki efek samping dalam pemanggilan [method assert]. Jika tidak, proyek akan berperilaku berbeda ketika diekspor dalam mode rilis.
[codeblock]
# Bayangkan kita selalu ingin kecepatan berada di antara 0 dan 20.
var speed = -10
assert(speed < 20) # Benar, program akan berlanjut.
assert(speed >= 0) # Salah, program akan berhenti.
assert(speed >= 0 and speed < 20) # Kamu juga dapat menggabungkan dua pernyataan kondisional dalam satu pengecekan.
assert(speed < 20, "batas kecepatan adalah 20") # Menampilkan pesan.
[/codeblock]