0 votes

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
in Engine by (193 points)

1 Answer

0 votes
Best answer

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", "mouseentered", "mouseexited", etc. Connect the signal to your method, and when mouse pressed, or hovered over, your method will be called automatically.

by (803 points)
selected by

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

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]

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.

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)

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.

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

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.