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)
    $Sprite.set_texture(theSprite)
    var spriteSize = Vector2($Sprite.texture.get_width() / reduzir, $Sprite.texture.get_height() / reduzir)
    $Area2D/CollisionShape2D.shape.set_extents(spriteSize)

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

func _input(event):
    if event.is_action_pressed("mouse_left") and mouse_on:
        $Timer.start()
        $AnimationPlayer.play("Shake")
        play_sound("Rubber Quick - Kbnevel")

    if event.is_action_released("mouse_left") and dragging:
        $Timer.stop()
        dragging = false
        $AnimationPlayer.play("Drop")
        play_sound("Bed Hit")

func _on_Area2D_mouse_entered():
    mouse_on = true

func _on_Area2D_mouse_exited():
    mouse_on = false
    $Timer.stop()

func _on_Timer_timeout():
    dragging = true
    $AnimationPlayer.play("Pop")
    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.stop()
        $AudioStreamPlayer.stream = sfx
        $AudioStreamPlayer.play()

And here's the project files if you want to mess around:
https://drive.google.com/open?id=1v0SKIZM-3Cd8LR3ydn9nWfgKReXW6Z1D

Please help.
Thanks.

in Engine by (87 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,284 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)
    $Sprite.set_texture(theSprite)
    var spriteSize = Vector2($Sprite.texture.get_width() / reduzir, $Sprite.texture.get_height() / reduzir)
    var colisao = CollisionShape2D.new()
    $Area2D.add_child(colisao)
    var thecolisao = $Area2D.get_child(0)
    thecolisao.shape = RectangleShape2D.new()
    thecolisao.shape.set_extents(spriteSize)

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 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 webmaster@godotengine.org with your username.