0 votes

I'm sorry for the title. I'm going insane over this.
I'm making a drag and drop thing for my game. The scene I have for the draggable object is organized like this:
enter image description here
This is what it looks like:
enter image description here
The script I have for the Drag and Drop Node2D has one export variant, that asks for a file path of a texture file. This variant is supposed to update the Sprite node's texture with the variant's texture file and change the CollisionShape2D's extents based on the image file's width and height divided by another export variable.
enter image description here

By itself, the scene works perfectly.
enter image description here
enter image description here
enter image description here
(The image's borders are farther away from the drawing itself)

Now, when I instance this Drag and Drop scene into another scene, if I add only one, nothing changes. Now if I instance another one, the code that sets the collision extends breaks. The sprite changes but the collider's shape for both is the same.
enter image description here
I discovered that the order of the instances in the hierarchy matters. The instance that's lower on the scene tree is the one that affects the shape of every other instance's collider.
enter image description here
This is the code the Drag and Drop scene uses. The only part that is breaking is inside the _ready() function:

extends Node2D

var mouse_on = false
var dragging = false

export (String) var spriteRes
export var reduzir = 2

# The only part that is breaking is inside the _ready() function
func _ready():
    var theSprite = load(spriteRes)
    var spriteSize = Vector2($Sprite.texture.get_width() / reduzir, $Sprite.texture.get_height() / reduzir)

func _process(delta):
    if dragging:
        position = get_global_mouse_position()

func _input(event):
    if event.is_action_pressed("mouse_left") and mouse_on:
        play_sound("Rubber Quick - Kbnevel")

    if event.is_action_released("mouse_left") and dragging:
        dragging = false
        play_sound("Bed Hit")

func _on_Area2D_mouse_entered():
    mouse_on = true

func _on_Area2D_mouse_exited():
    mouse_on = false

func _on_Timer_timeout():
    dragging = true
    play_sound("Plunger Quick - Lloydevans09")

func play_sound(var nome):
    var arquivo = "res://Sons/" + nome + ".wav"
    if File.new().file_exists(arquivo):
        var sfx = load(arquivo)
        $AudioStreamPlayer.stream = sfx

And here's the project files if you want to mess around:

Please help.

in Engine by (90 points)

1 Answer

+1 vote
Best answer

Ok, it seems that the collision shape uses the same instance.

Try 1: Make Unique in editor
As first test you could try to click the shape inside the collision shape and select "make unique".

Try 2: Make unique in code
Call this before setting the extents:
$Area2D/CollisionShape2D.shape = $Area2D/CollisionShape2D.duplicate()

by (3,358 points)
selected by

I'm going to try this out and tell you if it works, ok?

So, I managed to make it work by deleting the ShapeCollider from the Drag and Drop scene and creating one through code! This is the code I used for the _ready function:

func _ready():
    var theSprite = load(spriteRes)
    var spriteSize = Vector2($Sprite.texture.get_width() / reduzir, $Sprite.texture.get_height() / reduzir)
    var colisao = CollisionShape2D.new()
    var thecolisao = $Area2D.get_child(0)
    thecolisao.shape = RectangleShape2D.new()

Your first solution would have worked as well, but I wanted to be able to create as many drag and drop objects as I wanted through code, so I couldn't make each one unique through the editor.

I had a similar issue. The "duplicate" trick fixed my problem. Thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.