This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I'm trying to make a weapon pick-up system where the player walks in an area2d and then has the option to press F to pickup the weapon that he is currently standing over. The code looks like this:

func _on_PickupArea_area_entered(area):
  if Input.is_action_just_pressed("pick_up"):
     weaponIndex = area.pickupIndex
     weapon = WeaponContainer.get_child(weaponIndex)

The problem is that when I actually play the game and press the key(F), nothing happens. The signal works properly since the pickupText shows as it is supposed to. To test this out, I tried putting the same code in the _process(): function and there it actually detects when I press the action so it seems like the problem has something to do with the signal. Is it not possible to use Input statements in a signal? If so, how else can I do this?

Godot version 3.3 stable
in Engine by (81 points)
edited by

1 Answer

0 votes
Best answer

According to your logic your player will only be able to execute the code as you expect them to if they press the "pick_up" input on the exact frame that they enter the pickup area. This is very unfeasible so you should do something along these lines:

var player_in_area 

func _unhandled_input(event: InputEvent) -> void:
    if player_in_area and event.is_action_pressed("pick_up"):
        weaponIndex = player_in_area.pickupIndex
        weapon = WeaponContainer.get_child(weaponIndex)
        # maybe player_in_area = null

func _on_PickupArea_area_entered(area):
    player_in_area = area

# make sure to connect the area_exited signal
func _on_PickupArea_area_exited(area):
    # pickupText.hide() or whatever your call it
    player_in_area = null
by (3,906 points)
selected by

Ok this explains why it would sometimes work if I spammed the F key just as I was entering the pick-up Area. I got it working now, thanks a lot!

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.