I'm trying to make a simple drag and drop function, and it works when dragging from one area to another, but if I accidentally drop the sprite outside the allowed areas, it just disappears. I've been trying to have it set up so that the sprite, if dropped outside the area, returns to its last known location. Here is the code I have, although it does not work as the sprite still disappears:
extends TextureRect
var original_position : Vector2
func _get_drag_data(at_position):
original_position = position
var preview_texture = TextureRect.new()
preview_texture.texture = texture
preview_texture.expand_mode = 1
preview_texture.size = Vector2(90,90)
var preview = Control.new()
preview.add_child(preview_texture)
set_drag_preview(preview)
texture = null
return preview_texture.texture
func _can_drop_data(at_position, data):
return data is Texture2D
func _drop_data(at_position, data):
if _can_drop_data(at_position, data):
texture = data
else:
texture = data
position = original_position
My node tree looks like:
- Node2D
- TextureRect
- Panel
- TextureRect
- Panel
- TextureRect