Hi, i normaly reach any node like

onready var node = get_node("node_name") or if outside of the scoop

onready var node = get_node("../node_name")

but i need to get position of a node belong the diffrent scene,

the thing is:

player scene -root ----player | kinematic body -------something -------something -------something

enemy scene --enemy | kinematic body -----something -----something -----something

i have script in my enemy node, and i m try to get player position, so that i can use look_at and also follow the player. i did try some thing like

get_tree().get_root().get_node("node_name") etc...

didn't work.

are player scene and enemy scene instanced at the same scene?

Player is in the main scene as a node. But enemy is Get instanced at the main scene. And more then once.

While perhaps not the most optimal, what you could try doing is adding the player to a group and then using the get_nodes_in_group function to retrieve the player from the enemy script. I tried this on a jam game and it worked okay.

Thank you for anwser, yeah i also think about that, but couldn't be sure cause, i think i need the position data of the player. when enemys need to follow the player, i need to have a vector data, and if im not wrong, for vector data i need thevector_data = player_position - enemy1_position. if i use a group for reacing the player from enemy1 script, get i get the position data from it ?

and this is a clear way to expres what im tryging to do:

enemy scene :

main scene :

im trying the get player position from the script attact to enemy1.

so that i can made enemys to look at the player, and follow them. whlie im not sure if it will be work or not, cause there are more then 1 instanced enemy1 at the scene. they are randomly instancing.

You can try something like this:

Step 1 - Add the Player node to a group called "Player" either through code, or in the Godot editor. (code is add_to_group("Player"), which you could add to the _ready function).

Step 2, in the enemy script, use the following code to look at the player (untested):

var possible_players = get_tree().get_nodes_in_group("Player")
if (possible_players.count <= 0):
	print ("Cannot find player in Player group!")
else:
	var first_player = possible_players[0]
	# then, you can just use first_player like an ordinary node, like if you
	# got it from get_node. For example:
	# (assuming this script is on the enemy itself)
	vector_data = first_player.global_transform.origin - global_transform.origin
	
	# though, if you are just wanting to look at the player, you can use the
	# look_at function! It will work as long as your enemies face the negative Z
	# axis (I.E their "face" points to the negative Z axis)
	# Code below:
	# look_at(first_player.global_transform.origin, Vector3.UP)

thank you its works.

var vector_data : Vector3 = Vector3(0,0,0)
var first_player
onready var possible_player = get_tree().get_nodes_in_group("player")

	if (possible_player.count(0)) :
		print ("cant find any player")
	else:
		first_player = possible_player[0]
		look_at(first_player.global_transform.origin, Vector3.UP)
		
	vector_data = first_player.global_transform.origin - global_transform.origin 
	move_and_slide(vector_data,Vector3.UP)
2 years later