How to Determine if Player is Nearby Using Beehave?

Godot Version

Godot 4.3

Question

I’m trying to create a conditional that checks if the player is nearby an enemy (CharacterBody3D).

IsPlayerClose Script:

extends ConditionLeaf
var player = Global.player

func tick(actor: Node, _blackboard: Blackboard) → int:

var actor_position = actor.global_position
var player_position = player.global_position

if actor_position.distance_to(player_position) < 5 :
	return SUCCESS

return FAILURE

When I try to run the scene, I get the following error message: “Invalid access to property or key ‘global_position’ on a base object of type ‘Nil’.”

The player probably isn’t ready at that point. Maybe you could add @onready to your player var.

I would recommend using an Area to detect proximity instead of checking distance often.

1 Like