我的程序因尝试访问空对象的属性而崩溃,即使我之前检查过以确保它不是 NULL
My program crashes for trying to access the attribute of a null object, even though i checked before to made sure it wasnt NULL
我目前正在尝试在godot中制作塔防。
我的每个塔都有一个变量指向他们当前瞄准的敌人。
这用于,例如,动画他们的弓箭向敌人。
但是,当目标死亡并从场景树中释放时,它会导致游戏因尝试访问其属性而崩溃,即使我之前对它进行了空检查
以下是有问题的行:
if not target == null:
$BowAnchor.look_at(target.global_position)
删除对象时,指向它的变量不会自动变为空。相反,他们将指向一个已释放的对象。
要解决这个问题,请使用 is_instance_valid
:
if is_instance_valid(target):
pass
使用 is_instance_valid
您正在检查 null,以及对已释放对象的引用。
您可能感兴趣的其他类似检查是 Node
class 的 is_queued_for_deletion
of the Object
class, and is_inside_tree
。
我目前正在尝试在godot中制作塔防。
我的每个塔都有一个变量指向他们当前瞄准的敌人。 这用于,例如,动画他们的弓箭向敌人。
但是,当目标死亡并从场景树中释放时,它会导致游戏因尝试访问其属性而崩溃,即使我之前对它进行了空检查
以下是有问题的行:
if not target == null:
$BowAnchor.look_at(target.global_position)
删除对象时,指向它的变量不会自动变为空。相反,他们将指向一个已释放的对象。
要解决这个问题,请使用 is_instance_valid
:
if is_instance_valid(target):
pass
使用 is_instance_valid
您正在检查 null,以及对已释放对象的引用。
您可能感兴趣的其他类似检查是 Node
class 的 is_queued_for_deletion
of the Object
class, and is_inside_tree
。