0 votes

I just started godot and I have a problem. I have made a 2D game and for example I want to add an inventory. But i want the inventory to open every time the player presses the button "e" or "i". How can I do this??

in Engine by (20 points)

1 Answer

0 votes

You can do this in a few steps:

Bind the key:

Click "Project" (at the top menu bar), then "project settings" on the dropdown and then go to the "input map" tab. In the "Action" field give your action a name (eg "open_menu") and click "Add". Then find it at the bottom of the list and click the "+", selected "key" and press the key you want to bind.

Make a scene

No, not like at the office xmas party. ;) On the tab above the panel click "+". Click "Other Node" and now make your menu tree. You might want a "Control" node, "NinePatchRect", whatever works for you. On the menu bar select "scene" and "Save Scene as", give it a name / folder.

Add a key press event

Let's add a script to your "World" scene (whichever scene runs by default). Right click on the root node (top left) and select "Attach Script".

In this script you need a reference to your scene. You can do this like this as a member variable (not in a method):

var menu_scene = preload()

In the brackets you put the path to your scene. You can literally drag it from the bottom left to the brackets and it will autocomplete for you or you can pick it from the dropdown.

The let's add the action.

var is_menu_open = false

func _input(event):
    if event.is_action_pressed("open_menu"):
        is_menu_open = not is_menu_open
        toggle_menu()

func toggle_menu():
    if not is_menu_open:
       var menu = menu_scene.instance()
       add_child(menu)
    else:
       $Menu.queue_free()

Good luck!

by (2,159 points)
edited by

Omg thank you so much this is very useful. Have a nice day!!

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.