|Attention||Topic was automatically imported from the old Question2Answer platform.|
I’m trying to make simple 3D game, where you need to drag-and-drop objects to assemble things mechanically.
What I’ve already accomplished is that you can grab an object, drag it to “Area” node (where it registers a mouse_enter event) and snaps it on mouse button release.
When you want to detach the object, you click on the area, and then the last object snapped becomes animated and follows the mouse.
The problem: When I click on the area and hold the mouse button to drag, other Areas do not receive any event. The strange thing is - if I move the mouse out of the whole window and get it back to Area - it registers.
Any ideas appreciated!
Heres the code detecting events, “unit” is the box where I need to drag and snap objects to:
extends Spatial signal unitSelected signal unitDeselected signal unitMouseEntered signal unitMouseExited var itemName func _ready(): var _err = self.connect("mouse_entered", self, "_on_mouse_entered") _err = self.connect("mouse_exited", self, "_on_mouse_exited") _err = self.connect("input_event", self, "_on_input_event") func _on_input_event(_camera, event, _position, _normal, _shape_idx): if event is InputEventMouseButton: if event.button_index == BUTTON_LEFT: if event.pressed == true: emit_signal("unitSelected", int(self.name.replace("Unit", ""))) #inject unit number as an argument else: emit_signal("unitDeselected", int(self.name.replace("Unit", ""))) func _on_mouse_entered(): $SelectBox.show() emit_signal("unitMouseEntered", int(self.name.replace("Unit", ""))) func _on_mouse_exited(): $SelectBox.hide() emit_signal("unitMouseExited", int(self.name.replace("Unit", "")))
Edit: uploaded behavior (yellowish cube is shown with $SelectBox.show()):
You mean all objects are detectable areas, that can be dragged onto each other and trigger something on combination ?
Areas don’t detect collision when button is held, is it ? Make several print statements to determine if this is because of Input, drag function or object selection.
Make print statements to check what is being blocked - mouse entered signal, or only subsequent code.
Make sure other control nodes, like this selectbox don’t obscure and consume mouse input.
Are your unitselected emitted once per click, or constantly while button is pressed ? I am afraid it is the latter. What is listening to unitselected signal, and can it affect mouse enter detection ?
Inces | 2022-04-19 12:39
You mean all objects are detectable areas, that can be dragged
Not exactly - the areas themselves are static and cannot be moved; the objects can be dragged into and out of those areas (I call them units in the code, but You can imagine just as slots for objects).
Just added the print statements to the beginning of every event, and I can confirm this:
**… or constantly while button is pressed ** , it always calls the _on_input_event() after click.
I learn from prints that this happens:
- I move the mouse onto area.
- _on_mouse_entered() calls $SelectBox.show(), which shows that area reacted now.
- I click on the area and hold.
- _on_input_event() is called on click, which then goes to signaling other nodes. I’m not moving the mouse at this moment.
- I move the mouse (in area) - _on_input_event() calls again.
- I move the mouse outside the area - _on_input_event() keeps calling again.
- No _on_mouse_exited happened!!
- I release the mouse button - _on_input_event() AND _on_mouse_exited calls, which then signals “unitDeselected” and “unitMouseExited”.
I imagine that somehow the frame processing goes to _on_input_event() and it somehow halts enter and exit calls from happening
djlukas74 | 2022-04-19 18:14
no, input processing is fine, since even mouse movement triggers input event. It is good, unless it prints that left mouse click is the only event detected all the time.
Is there anything more that happens after click ? Like some kind of preview icon being picked and dragged on process ?
Inces | 2022-04-22 13:35
There’s “_input()” code in each of the object’s script:
func _input(event): if grabbed and (event is InputEventMouseMotion): var camera = get_node("Camera") var distance_to_frame = camera.translation.distance_to(get_tree().get_root().get_node("Spatial/spawn").global_transform.origin) var to = camera.project_position(event.position, distance_to_frame) self.global_transform.origin = to;
Basically when you click on object and hold, the “grabbed” flag is set to true. When you let go - “grabbed” is set to false, so “self” is not moved.
The code I pasted on my question is from the area script; so when you click and hold an area it pops the object and then sets this script’s “grabbed” flag to be true.
I’ve added more printf statements and it seems that if I click and drag the mouse outside the area - the “_input” code above and the area’s “_on_input_event()” are called simultaneously, but when I stop - no event is happening. And then, same as before, “_mouse_exit()” event calls only when I release the mouse button…
djlukas74 | 2022-04-24 19:19