InputEventMouseMotion turns Deleted object to InputEventMouseMotion

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

I need this function to select/unselect objects that the cursor hovering over.
the mark variable is only used by this function.
But as soon the object in the mark variable get deleted it turns into a InputEventMouseMotion anyone that know why or how to avoid it, this is causing constant crashes.
i tried by another variable but same thing happens.

i cant just do if mark is InputEventMouseMotion cuz this is crashing too

var mark
func _input(event):
	if Input.is_action_just_pressed("exit"):
		get_tree().quit()
	elif event is InputEventMouseMotion:
		var s = get_pointed_object()
		if s:
			if is_instance_valid(mark) and mark.has_node("selection"):
				mark.get_node("selection").deselect()
			if s.object.has_node("selection"):
				s.object.get_node("selection").select()
			mark = s.object
:bust_in_silhouette: Reply From: Sween123

The problem may have to do with s.object
Anyway, you don’t really need to use _input(event) to check if your mouse is hovering over something.
If you want an object to be able get detected when mouse is hovering over or when mouse pressed, you can add a node “Button” to it. Try to use signals, there are signals like “pressed”, “mouse_entered”, “mouse_exited”, etc. Connect the signal to your method, and when mouse pressed, or hovered over, your method will be called automatically.

I didn’t wrrote, its in 3D (3D RTS game), the buttom solution wont work well :slight_smile:

The thing is the mark variable becomes InputEventMouseMotion after the if line, even its not set, so its not set by the code, i suspect this is a confuse by the event variable from the engine.

so:

*mark* is now [Deleted Object]
   if Input.is_action_just_pressed("exit"):
        get_tree().quit()
    elif event is InputEventMouseMotion:
*mark* is now [InputEventMouseMotion]

AiTechEye | 2020-02-11 21:53

I see. Try using StaticBody.
About your code, if you never set the value for mark, it should be a null. You can use a breakpoint and do stepover to see what’s happening there.

Sween123 | 2020-02-12 00:25

I tested this in a new project, same thing happens if test = true, if you are trying this, note in the output:
[StaticBody:1211] c

[InputEventMouseMotion:1461] a

[InputEventMouseMotion:1461] b

it doesn’t happends every time the game are running

    var test = true
    var m
    var b
    var a = load("res://a.tscn")
# a.tscn = Staticbody + MeshInstance + CollisionShape
    onready var camera = $Camera

func get_pointed_object():
	var mp = get_viewport().get_mouse_position()
	var f = camera.project_ray_origin(mp)
	var to = f + camera.project_ray_normal(mp) * 100
	var s = get_world().direct_space_state.intersect_ray(f,to)
	if s:
		return s.collider

func _input(event):
    print(m," a")
	if event is InputEventMouseMotion:
		print(m," b")
		var s = get_pointed_object()
		if s:
			if m and is_instance_valid(m) and m.has_node("node"):
				pass
			m = s
			print(m," c")
			m.queue_free()

func _physics_process(delta):
	if test or b == null or is_instance_valid(b) == false:
		b = a.instance()
		add_child(b)

AiTechEye | 2020-02-12 09:10

I mean… Use StaticBody in 3D like how we use Button in 2D. There are signals like mouse_entered and mouse_exited. Just connect the signal to your mehods. It’s similar to Button in 2D.

Sween123 | 2020-02-12 10:29

ahh got it, and it seems to work faster than advanced calculations every time the cursor is moving, thanks

AiTechEye | 2020-02-12 12:58