0

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.

What would be the correct way to handle the origin slot if I want to switch data?

2
  • could you share relevant code from your global (I'm assuming it's an autoload script)? it seems the property you're setting origin_slot is a StringName i.e. pretty sure it's just a simple mistake of setting the wrong variable from global Commented May 12, 2024 at 10:58
  • 1
    @M.R.M. Thank you, this was a really good lead! I mistakenly used the get_name() function, not realizing it only returns the value as a string. Changing it to get_node() fixed the issue and now everything works as intended. Commented May 14, 2024 at 12:04

1 Answer 1

0

Thank you, I mistakenly used get_name() instead of get_node(). Fixing that made the program work as intended.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.