+1 vote

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.

in Engine by (37 points)

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

2 Answers

+1 vote

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.

Charles

by (335 points)

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.

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

+1 vote

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
 _on_area_entered(body):
      # 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.
             body.pick_up_weapon(some_weapon)

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.
  _on_area_entered(body):
      # 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.
_unhandled_input(event):
    # 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.
            inventory.push_back(weapon_currently_selected)

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

by (3,144 points)
edited by

Thanks! Ertain

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.