How do I refer to a scene in another scene's script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By WorkedPython

So in my game I need the enemy to look at the player and follow it, but because they are separate scenes, I don’t know how to refer to the player from the enemy script.

(less important)
v v v v
In addition to this, how can I make the enemy always look at the player (do I use the “look_at” method or something) and also how do I make it move constantly forwards towards the player

P.S. game is in 2D

:bust_in_silhouette: Reply From: ramazan

3D
enemy script:
var player = get_parent().get_node(“player”) = find player .

look_at(player.global_transform.origin, Vector3.UP)

I forgot to mention, my game is 2D, so how would that go

WorkedPython | 2021-12-26 05:13

var player = getparent().getnode(“player”) = find player
look-at(player.global_position)

ramazan | 2021-12-26 09:32

it says player is not define in the current scope, I tried changing the name to the scene name and it still didn’t work
this is my structure btw:

Main
Player <------- this Node is what i want the enemy to look at
Camera2D
Ring <------ is just kind the background
Enemy <------ tryna write script for this node

btw the Player, Ring and the Enemy nodes are scenes that i have instanced into the main scene

WorkedPython | 2021-12-26 13:46

Main
—Player
—Camera2D
—Ring
— Enemy (script is here)

var player = get_parent().get_node(“Player” , “Camera2D”, “Ring”)

########################
ELSE
get_parent().get_parent().get_node(“Player”)
Main
—Player
—Camera2D
—Ring
— Enemy
--------Another Node(script is here). (child Enemy)

var player = get_parent().get_parent().get_node(“Player” , “Camera2D”, “Ring”)

ramazan | 2021-12-26 14:47

just to clarify the structure goes like this (my bad I didn’t realise the spaces didn’t show up)

Main
–Player
----Camera2D
–Ring
–Enemy

also because the enemy is like a separate scene, the player does not exist with it because when I code for the Enemy, it is assuming the structure of the enemy scene which look like

Enemy
–Sprite
–AnimationPlayer
–CollisionShape2D
–Area2D
----CollisionShape2D

the enemy exists separately as it is in a different, same with the player scene, it is just that I instanced both the player scene and the enemy scene to the main scene, I am pretty sure that is why it isn’t working. I’m very new to GDscript btw

WorkedPython | 2021-12-27 06:32

Thanks man, I’m sure it will help me, but when I said new, I meant I know a bit more than a beginner, but not enough to make a full game solo

WorkedPython | 2021-12-28 14:25

sorry friend. I think maybe I didn’t understand the problem clearly.

ramazan | 2021-12-29 08:40