Godot Version
4.2
Question
I know this might be a beginner question and I did it already in another game, but this time it is not working… I can’t figure out, what I’m missing…
My current game is a Yahtzee-clone. I have a “die”-class and 5 instances of it. In my game there are 2 areas: one for the dice to keep, the other for the dice to re-roll. I want to pick a die and drag and drop it from one area to the other.
The die is a StaticBody2D with an AnimatedSprite, a Timer and an Area2D with a rectangle as CollisionShape2D. Class_name “Die”.
var selected: bool
is used to check if the die is currently selected. At the start of the game it is not selected.
This is what I’m using to drag and drop it:
> func _physics_process(delta):
> if selected:
> global_position = lerp(global_position, get_global_mouse_position(), 25 * delta)
Now the problems start.
This function is used to deselect the die:
> func _input(event):
> if event is InputEventMouseButton:
> if event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
> selected = false
> print("selected = false")
Problem: all 5 instances print “selected false”
This should work, but I get no print:
> func _on_area_2d_input_event(viewport, event, shape_idx):
> if event is InputEventMouseButton and event.pressed:
> print("Mouse click")
> selected = true
> if Input.is_action_pressed("click"):
> print("click")
> selected = true
“click” is defined in project settings as left mouse click.
Also these function don’t show print:
> func _on_area_2d_mouse_entered():
> print("Mouse entered " + str(id))
>
> func _on_area_2d_mouse_exited():
> print("Mouse left " + str(id))
@export var id:int
is the variable to give each die a unique id.
Pickable is set to true.
If I move
> if event is InputEventMouseButton and event.pressed:
> print("Mouse click")
> selected = true
to _input(event), all 5 instances are selected and can be dragged and dropped. But I only want the instance which was clicked to be dragged and dropped.
I’m missing something, but what? Hope that the forum can help me out!
Thanks a lot for looking into this!