如何停止 EditorScript 的执行?
How to stop the execution of an EditorScript?
考虑这个 EditorScript
:
tool
extends EditorScript
func _run() -> void:
_check_everything()
_do_something()
func _check_everything() -> void:
assert(self.get_scene() != null, "You have to create a scene...")
## How to exit if assert has failed?
func _do_something() -> void:
self.get_scene().add_child(Node.new())
和输出:
<built-in>:9 - Assertion failed: You have to create a scene...
<built-in>:13 - Attempt to call function 'add_child' in base 'null instance' on a null instance.
断言失败如何停止执行?
GDScript 中没有办法。如果失败,不仅 assert
(See also assert
) 不会向脚本返回报告。它仅适用于调试版本。此外,该语言的设计无一例外。
改为先发制人:
func _run() -> void:
if !_can_run():
return
_do_something()
func _can_run() -> bool:
return self.get_scene() != null
如果要检查代码是否在调试版本中,可以使用 OS.is_debug_build
. You might also be interested in Engine.editor_hint
.
您可以结合 assert
或 push_error
or push_warning
. By the way, there is a breakpoint
keyword.
检查条件
考虑这个 EditorScript
:
tool
extends EditorScript
func _run() -> void:
_check_everything()
_do_something()
func _check_everything() -> void:
assert(self.get_scene() != null, "You have to create a scene...")
## How to exit if assert has failed?
func _do_something() -> void:
self.get_scene().add_child(Node.new())
和输出:
<built-in>:9 - Assertion failed: You have to create a scene...
<built-in>:13 - Attempt to call function 'add_child' in base 'null instance' on a null instance.
断言失败如何停止执行?
GDScript 中没有办法。如果失败,不仅 assert
(See also assert
) 不会向脚本返回报告。它仅适用于调试版本。此外,该语言的设计无一例外。
改为先发制人:
func _run() -> void:
if !_can_run():
return
_do_something()
func _can_run() -> bool:
return self.get_scene() != null
如果要检查代码是否在调试版本中,可以使用 OS.is_debug_build
. You might also be interested in Engine.editor_hint
.
您可以结合 assert
或 push_error
or push_warning
. By the way, there is a breakpoint
keyword.