The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

I'm trying to make a drag and drop "game" but when the main node collides with the other, it emits a signal but I keep getting an error saying that the node cannot be found. Here is all my code so far. I definitely made a mistake somewhere I just don't know where.

extends Node2D

signal raycast_object_hit

var selected = false

var colUP = false
var colDOWN = false
var colLEFT = false
var colRIGHT = false

onready var rayUP = $UP
onready var rayDOWN = $DOWN
onready var rayLEFT = $LEFT
onready var rayRIGHT = $RIGHT

onready var attachment = get_node("RigidBody2D")

func _ready():
    attachment.connect("raycast_object_hit", self, _collision_detection())

func _on_Area2D_input_event(viewport, event, shape_idx):
    if Input.is_action_just_pressed("click"):
        selected = true

func _physics_process(delta):


    if selected:
        global_position = lerp(global_position, get_global_mouse_position(), 25 * delta)

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and not event.pressed:
            selected = false

func _collision_detection():

    var space_state = get_world_2d().direct_space_state
    var collision = space_state.intersect_ray(Vector2(0, 0), Vector2(50, 100))

    if rayUP.is_colliding():
        if colUP == false:
            colUP = true
            colUP = false
    emit_signal("raycast_object_hit", rayUP.get_collider())

    if rayDOWN.is_colliding():
        if colDOWN == false:
            colDOWN = true
            colDOWN = false
    emit_signal("raycast_object_hit", rayDOWN.get_collider())

    if rayLEFT.is_colliding():
        if colLEFT == false:
                colLEFT = true
            colLEFT = false
    emit_signal("raycast_object_hit", rayLEFT.get_collider())

    if rayRIGHT.is_colliding():
        if colRIGHT == false:
            colRIGHT = true
            colRIGHT = false
    emit_signal("raycast_object_hit", rayRIGHT.get_collider())
Godot version 3.2.3
in Engine by (35 points)
edited by

1 Answer

0 votes

The only place in this code you are calling for a node is

onready var attachment = get_node("RigidBody2D")

is this the proper node name in your project, get_node is relative to the current node, so if this node doesn't have the "RigidBody2D" in it you would have to access it from wherever it is.

There is a link to someone wondering about something like this a while back,

Trouble understanding get_node

by (284 points)
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 Frequently asked questions and 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 [email protected] with your username.