根据当前碰撞改变玩家的变量

Changing Player's variable depending on current collision

我正在尝试根据玩家是否在灌木丛中来修改玩家的移动速度。这是我要完成的要点:

const Grass = preload("res://World/Grass/Grass.tscn")
onready var grass = Grass.instance()

func move():
    grass = Grass.instance()
    if grass.player_in_grass():
        velocity = move_and_slide(velocity / BUSH_FRICTION_MULTIPLIER)
    else:
        velocity = move_and_slide(velocity)

我遇到的问题是我不知道要检查的代码应该是什么。我尝试为草地创建玩家检测区,在其中切换其值:

var player = null

func player_is_visible():
    return player != null

func _on_PlayerDetectionZone_body_entered(body):
    player = body

func _on_PlayerDetectionZone_body_exited(body):
    player = null

我的 Grass.gd 看起来像这样:

onready var playerDetectionZone = $PlayerDetectionZone

func player_in_grass():
    if playerDetectionZone.player != null:
        return true
    else:
        return false

所有这一切之后,我遇到了错误:

Invalid get index 'player' (on base: 'Nil').

错误将我转到 'if playerDetectionZone.player != null:'。

我做错了什么?我是否应该用一种您知道的更简单的不同方法来检查 this/doing?所有反馈表示赞赏。谢谢。

总结一下:

错误是

Invalid get index 'player' (on base: 'Nil').

而出现这个错误的代码行是:

if playerDetectionZone.player != null:

(不幸的是,这不是发布代码的一部分)。

此错误意味着变量 playerDetectionZone 的值为 null。假设其余代码已发布,问题应位于此处:

onready var playerDetectionZone = $PlayerDetectionZone

节点路径($PlayerDetectionZone)有问题。拼写、大写,可能在树中的位置错误。可能是很多东西。

编辑:根据您的评论,这可能是树中的错误位置。仅当 PlayerDecetionZone 节点是草节点(附有 grass.gd 的节点)的子节点时,上面的行才有效。