0 votes

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??

Godot version 3.3.3 stable
in Engine by (29 points)

1 Answer

0 votes

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.

by (382 points)

How would i do that because when i tried using the

getnodesin_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

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.

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

https://youtu.be/E87F6WsQ4vA

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.