How do i make 1 raycast see only 1 scenenode in multiple scenes

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

Okay so this is sorta confusing so what i want is i have an enemy that has a raycast going off the end of it, and i want it to be able to see everything but only fire if the collider is the player. Normally i would have this

onready var player = get_node("/root/World/Player") 
			
			if $RayCast2D2.is_colliding():
				if $RayCast2D2.get_collider() == node:
					is_attacking = true
					$AnimatedSprite.play("long attack")
					var fireball = FIREBALL.instance()
					if $AnimatedSprite.flip_h == false:
						fireball.direction *= -1
						fireball.set_fireball_direction(-1)
					else:
						fireball.set_fireball_direction(1)
					get_node("Position2D").add_child(fireball)
					$Timer4.start()

and this works just fine… for the first world. it wont work in any other world how would i get it to see that without having to get the nodepath from each world??

:bust_in_silhouette: Reply From: spaceyjase

You want to find the player (or a certain node) regardless of scene? Assign the player to a group and use get_nodes_in_group: https://docs.godotengine.org/en/stable/getting_started/step_by_step/scripting_continued.html#groups

Other methods might include assigning the object to a manager in the editor or the node broadcasting its path via some registration method or an event bus.

How would i do that because when i tried using the

get_nodes_in_group(“Player”)

it comes up with this error

(
E 0:00:02.116 get_tree: Condition “!data.tree” is true. Returned: __null
<C++ Source> scene/main/node.h:274 @ get_tree()
scorpion.gd:22 @ _init()
Start screen.gd:7 @ _on_Button_pressed()
)

all i have is a var named node and it has the get nodes command and it wont work.

I can give you a preview of what i am trying to do because it is hard to explain while writing it. I will try again if you didnt understand fully

I have an enemy scene and a player scene and a world scene. The scorpion has a raycast that sees everything but will only attack if it sees the collider as the player node in that scene. so i use a get_node function to get the player node in that scene and it works just fine but when i go to the next world it no longer works because its not in world 1 anymore its in world 2 how do i make the raycast check for the player node regardless of what scene

Nightmare | 2021-11-27 04:45

Is the player in the scene? The error would suggest not. What are you trying to do? The node you wish to find must be in the scene, or do you mean to create a player node? More information would help, yes.

spaceyjase | 2021-11-27 22:04

i hope this helps explain also the player is not in the exact same scene as the enemy

https://youtu.be/E87F6WsQ4vA

Nightmare | 2021-11-27 22:24