+1 vote

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!

in Engine by (302 points)

1 Answer

+1 vote

You have the pickup area variable defined twice. Get rid of the var in the last line of the onArea2Dareaentered 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
by (212 points)

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, 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.

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!

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.

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.