How to pickup weapons in 3D game.

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

Hello, i am creating an action game which is based on TPS model. So, my question how do i pickup guns that are lying on floor.

That depends on the flow of the game. If it’s an action-oriented game, the player usually runs over the weapons, and they collect them. If it’s a bit more slow-paced, the player could walk up to the weapon, press an action button, and the player collects the weapon.

Ertain | 2020-11-01 00:42

Ertain, yes game is action oriented game where player runs over the weapon and then to collect them they have to press key. But the thing is i am totally new so i don’t know how to create these types of functions in game with scripts.

StarX1709 | 2020-11-02 09:30

:bust_in_silhouette: Reply From: CharlesMerriam

You have a Game Design Question?

  • Are weapons automatically picked up on walk over? (like many real-time games)
  • Do you switch automatically if the weapon is ‘better’? (like Dungeon Siege).
  • Do you stop and weigh choices immediately? (like turn based games)

You have a Scripting Question?

  • Are you using a Singleton, Godot’s method for having a cross-scene global-ish variable for things like the character inventory?
  • Are you using your scripting idea of a location and comparing your player location versus the location of objects?
  • Or, are you using collision maps, and catching a signal when your player collides with a weapon?

Let me know what exactly you are asking. Keep working, you will get there.

Keep hacking. Keep notes.


Actually you described a lot, i am just asking about how to pickup a weapon in game when player press a key and it will be more helpful if there is any tutorial related to this query. By the way thanks for answering my question.

StarX1709 | 2020-11-02 09:24

Guessing a bit. You probably want this one on InputEvent.

CharlesMerriam | 2020-11-05 07:11

:bust_in_silhouette: Reply From: Ertain

If you want the player to immediately pick up the weapon when the player walks through the weapon, then set up the weapon with an Area node. When the player walks through the weapon, have a script on the weapon node react to the player walking through the weapon. The script could have some like this in it (though not exactly this code because your project setup will probably be different):

 # Don't forget to connect the Area signal "body_entered" to the weapon node
      # The player's script has the function "pick_up_weapon()".
      if body is player and body.has_method("pick_up_weapon"):
             # The "pick_up_weapon()" function would add the weapon to the player's inventory.

If the player needs to press a key/button to pick up the weapon after walking through it, use something like this code (though not exactly this code because, again, your game is probably setup differently):

  # In the weapon script, the weapon has to tell the player script that the weapon's Area has been entered. Again, don't forget to connect the "body_entered" signal.
      # The player's script needs to keep track of whether it has entered the weapon's Area.
      if body is player and player.has("weapon_area_entered"):
          # This assigns "true" to the "weapon_area_entered" variable in the player's script.
          body.weapon_area_entered = true
          # We also need to have a variable to save which weapon it is.
          body.weapon_currently_selected = "mcguffin"

# In the player's script.
    # The player presses the key/button to pick up the weapon.
    if event is InputEvent and event.is_action_pressed("action_key"):
        # The variable "weapon_currently_selected" needs to have something assigned to it.
        if weapon_currently_selected:
            # Add the weapon to the inventory.

I hope this is a good starting point for developing the “pickup stuff” mechanic in your game.

Thanks! Ertain

StarX1709 | 2020-11-17 23:11