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]
Memastikan bahwa [code]kondisi[/code] yang di berikan benar. Jika kondisi yang diberikan hasilnya salah, sebuah galat (error) diberikan dan program akan berhenti sampai Anda meneruskannya lagi. Hanya tersedia di jenis build untuk pemeriksaan (debugging) atau saat menjalankannya pada editor. Gunakan hanya untuk pemeriksaan apakah sebuah kondisi menghasilkan [code]true[/code] saat pembuatan program. Tidak hanya memberikan galat "Assertion failed", beberapa dapat memberikan suatu argumen [code]message[/code] yang akan ditampilkan untuk mengganti galat sebelumnya. Ini dapat digunakan untuk dijadikan alasan mengapa galat terjadi. [codeblock] # Coba bayangkan kita ingin variabel speed hanya berisi 0 - 20 speed = -1 assert(speed < 20) # Betul, program tetap berjalan assert(speed >= 0) # Salah, program akan berhenti assert(speed >= 0 && speed < 20) # Anda dapat memberikan dua kondisi dalam satu kali cek assert assert(speed < 20, "speed = %f, but the speed limit is 20" % speed) # Mengisi argumen message untuk menambah detail [/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]
Tidak hanya memberikan galat "Assertion failed", beberapa dapat memberikan suatu argumen [code]message[/code] yang akan ditampilkan untuk mengganti galat sebelumnya. Ini dapat digunakan untuk dijadikan alasan mengapa galat terjadi.
[codeblock]
# Coba bayangkan kita ingin variabel speed hanya berisi 0 - 20
speed = -1
assert(speed < 20) # Betul, program tetap berjalan
assert(speed >= 0) # Salah, program akan berhenti
assert(speed >= 0 && speed < 20) # Anda dapat memberikan dua kondisi dalam satu kali cek assert
assert(speed < 20, "speed = %f, but the speed limit is 20" % speed) # Mengisi argumen message untuk menambah detail
[/codeblock]