I'm currently in the process of making a screen that has ability slots, and it is up to the player to mix and match them and change the order at will. The Drag and Drop functionality worked just fine when it was one-way, but when I tried adding the process of actually swapping the data between the two nodes (should both have any), I keep running into errors.

I have a custom resource called Ability and the slots have a variable called "selected_ability" that takes these and uses it to set the textures and other information.

extends TextureRect
class_name AbilitySlot

@export var selected_ability: Ability

@onready var slot: AbilitySlot = $"."


func _process(_delta):
	if selected_ability:
		slot.texture = selected_ability.icon
	else:
		slot.texture = null
	
func _get_drag_data(_at_position):
	set_drag_preview(get_preview())
	global.origin_slot = self.get_name()
	global.origin_ability = self.selected_ability
	print(global.origin_slot)
	print(global.origin_ability)
	return slot.selected_ability
	
func get_preview():
	var preview_texture = TextureRect.new()
	if selected_ability:
		preview_texture.texture = slot.selected_ability.icon
		preview_texture.expand_mode = 1
		preview_texture.size = Vector2(64,64)
	var preview = Control.new()
	preview.add_child(preview_texture)
 
	return preview
	
func _can_drop_data(_at_position, data):
	global.target_slot = get_name()
	if selected_ability:
		global.target_ability = selected_ability
	else:
		global.target_ability = null
	print(global.target_slot)
	print(global.target_ability)
	return true

func _drop_data(_at_position, data):
	global.origin_slot.selected_ability = global.target_ability         < ERROR
	selected_ability = global.origin_ability

The program crashes at Line 45 (the second from the bottom) with the error "Invalid set index 'selected_ability' (on base: 'StringName') with value of type 'Resource (Ability)'."

If I comment the problematic line out, the program successfully copies the desired contents of the origin slot to the target slot, but the origin slot remains unchanged, so it acts as an infinite duplicate source. I know this because the abilities that slots set up successfully pass onto the next screen, it is only the swap that doesn't work.

I'm a little bit unsure as to why setting the index is not possible here as it's correctly getting done in the line below that. What would be the correct way to handle the origin slot if I want to switch data?

  • SnapCracklins replied to this.
  • I found the solution. The problem was that I mistakenly used get_name() instead of get_node() so it only returned a string with the node name in it and wanted to assign a variable to that which is not possible.

    l8r_allocator Have you considered using different variables for each spot in your UI? If you have a value for each "slot" in the array, you can use this to update in real time.

    I found the solution. The problem was that I mistakenly used get_name() instead of get_node() so it only returned a string with the node name in it and wanted to assign a variable to that which is not possible.

    l8r_allocator changed the title to [Solved] Difficulty implementing Drag and Drop swap with custom resources .