Trying to build a weapon pick-up system using Area2Ds, but struggling to make it work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tato64

Hello all, thanks for clicking on my question.

Basically, im making a top-down 2D, think of the style of “Hotline miami”.

I want to be able to pick up weapons with right click.
Basically it works like this:

Character has an area2D, weapon on the ground has an area2D, character has a signal to detect the weapon’s area, player right clicks: weapon on the ground is deleted, character sprite changes (and a weapon node is added to the character, but this isnt in the code yet)

Here’s the relevant code:

var pistol = preload("res://personaje.png")

var in_pickup_area = false
var sprite_texture
var pickup_area

func _on_Area2D_area_entered(area):
    print(area.name)
    if area.name == "Pistol (pickup)":
        in_pickup_area = true
        sprite_texture = pistol
        var pickup_area = area


func _on_Area2D_area_exited(area):
    if in_pickup_area:
        if area == pickup_area:
            in_pickup_area = false
            pickup_area = null

func _process(_delta):
    if Input.is_action_just_pressed("rclick"):
        if in_pickup_area and pickup_area != null:
            $Sprite.set_texture(sprite_texture)
            pickup_area.queue_free()
            in_pickup_area = false
            pickup_area = null

When i stand on the weapon, the console prints “Pistol (pickup)” so it is detecting correctly, but right clicking does absolutely nothing

Please help!

Thanks in advance!

:bust_in_silhouette: Reply From: tuon

You have the pickup area variable defined twice. Get rid of the var in the last line of the _on_Area2D_area_entered function

func _on_Area2D_area_entered(area):
print(area.name)
if area.name == "Pistol (pickup)":
    in_pickup_area = true
    sprite_texture = pistol
    var pickup_area = area # remove var on this line

I removed it, but it’s still not working :S

Im going mad, it’s does not seem that complex but i’ve been stuck with this for days now!

Tato64 | 2020-08-11 20:19

Tato64, is it possible for you to share a link to a github repository or a zip file with all the source code in it? If you make it available, I’ll take another look.

tuon | 2020-08-11 21:45

I uploaded the whole thing here (It’s just almost 2 MB uncompressed)

https://www.mediafire.com/file/z3ph4zgi9khekjz/Juego2D.rar/file

Use “sandbox.tscn” to test it

Thanks in advance!

Tato64 | 2020-08-12 02:47

I downloaded the rar file but I could not uncompress it. I’m sorry, but it looks like I won’t be able to look at the project after all.

tuon | 2020-08-12 22:27