I keep trying to make it work, but it can only be opened and not closed.

var inventory_e = Input.IsActionJustPressed("toggle_inventory");
var inventory = GetNode("InventoryGui");
var etime = 0;

if (inventory_e) {
etime = 1;
((Control)inventory).Show();
}
else if (etime = 1 && inventory_e) {
((Control)inventory).Hide();
etime = 0;
}

    TheEpicCookie I think the second condition will never be fulfilled because in this test the variable "etime" will always remain with the value 0.

    Try this, it might work:

    var inventory_e = Input.IsActionJustPressed("toggle_inventory");
    var inventory = GetNode("InventoryGui");
    var etime = 0;
    
    if (inventory_e) {
        etime = 1;
        ((Control)inventory).Show();
    }
    else {
        etime = 0;
        ((Control)inventory).Hide();
    }

      No idea about C# or the Godot API under C#. But there is an obvious flaw in the logic of your code. Since the if branch already covers inventory_e==true, the else branch is never entered and Hide is never called.

      What you should do is on the action being pressed check if the window is visible. In GDscript there is a member variable named visible. Probably called Visible or something in C#. If this variable is true, then hide the control, else show it.

      That all of course assumes you want the action to act as a toggle, which isn't entirely clear from your problem statement.

      btw. the etime variable seems redundant.

        TheEpicCookie

        I see you are trying to ise “etime” to track if the inventory is open or closed. However, the way the code is written that variable is always reset to its initial value when the function is called (we say it’s a “local variable”).

        One solution is to make etime a member variable.

        Another is to skip it entirely and use a toggle instead of show/hide. Try this:

        Inventory.visible = !inventory.visible

        The “!” Is “not”, so that line will show the inventory if it’s hidden, and hide it if it’s showing.

          inventory.visible = !inventory.visible

          Or you could use:
          inventory.visible = not inventory.visible

          which I think is clearer.

          Zini I am not sure if i typed this right:
          var Visible = false;

          if (inventory_e) {
          if (Visible = true) {
          ((Control)inventory).Hide();
          }
          }
          else {
          ((Control)inventory).Show();
          }

          When i started testing the gui was visible but when i press e it started to flicker.

          • Zini replied to this.

            axolotl Tried it but then this error popped up:
            'Node' does not contain a definition for 'visible'

            I managed to make it work in a different way:

            var inventory_e = Input.IsActionJustPressed("toggle_inventory");
            var inventory = GetNode("InventoryGui");
            var escape = Input.IsActionJustPressed("Escape");

            if (inventory_e) {
            ((Control)inventory).Show();
            }
            else if (escape) {
            ((Control)inventory).Hide();
            }

            Thank you all for your suggestions!

            TheEpicCookie No, that is not what I meant. Your if (inventory_e) still has an else. It must not have an else, if you want to use the same key for opening and closing. The show part belongs into the else of the if (Visible==true) instead.

            Edit: Also, you are declaring your own Visible variable. That is wrong. The control already has one (might be a get function, not sure about C#).

              Zini I tried this:
              if (inventory_e) {
              if (visible == true) {
              ((Control)inventory).Hide();
              }
              else
              {
              ((Control)inventory).Show();
              }
              }

              The GUI can only be opened and not closed. I probably didn't write it correctly
              like you said. It's ok, the escape key can be used to close it.