You'll need to know where the player node is in the scene from the perspective of the enemy scene and then you can retrieve the information by accessing its variables and functions. There are a few ways this can be done. I've listed two of the ways I've handled this below
Method 1 - exported NodePath
This will allow you to set the NodePath to the player scene in the Godot editor. As long as the enemies and player are already place in the scene and do not spawn dynamically, this may be a viable route. The code in the enemy script could look like this (untested):
extends Node2D
export (NodePath) var path_to_player = null
var player_node
func _ready():
# Make sure to set the NodePath in the Godot editor before running!
# The NodePath will be in the inspector when you select the node.
player_node = get_node(path_to_player)
# Now you can access its variables like normal.
print (player.get_name())
# Assuming it has a variable called health...
player.health += 1
print ("The player has: ", player.health)
Method 2 - Using groups
First, you need to add the player node to its own group, which I will assume is called Player
in the example below. This will allow you to utilize Godot's group feature to find the node from anywhere. This method will work with dynamically spawned enemies and a dynamically spawned player, though the player has to be spawned before the enemies for getting the player via groups to work in the _ready
function. The code for the enemy could look something like this (untested):
extends Node2D
var player_node;
func _ready():
for player_group_node in get_tree().get_nodes_in_group("Player"):
player_node = player_group_node;
break;
# Now you can access its variables like normal.
print (player.get_name())
# Assuming it has a variable called health...
player.health += 1
print ("The player has: ", player.health)