How To Connect Built-In Function To A Custom One?

Godot Version

4.3

Question

I am working on something which involves having a node (a ColorRect in this case) receive input (I want it to be any input), but the ColorRect only emits a signal when it receives ‘gui_input’, being mouse button presses and mouse movement. But I want it to receive anything - like the function ‘_input(event)’.

How can I - without attaching a script to the node - connect the function ‘_input’ to my own function, so I can process input other than those handled by ‘gui_input’?

Thanks.

I think you need to further describe what you’re trying to achieve here. _gui_input() is a specialized version of _input() that should be used in UI contexts where different rules apply. Because of this, only relevant input is captured by _gui_input() (see Custom GUI Controls | Input).

As for how to implement custom behaviour without code: that’s just not possible. _input() is a general purpose function that is meant to be overridden in scripts that need input-related functionality. If you want to send every input event to another function, why don’t you just add _input() to the script where your other function is defined?

func _input(event):
    MyOwnFunction(event)
func MyOwnFunction(event):
    # My code

I need more information.

Thank you, I’ll try to better explain;

I am making a screen to allow for remapping of InputMap actions, for this I make a ColorRect visible when we want to change the given keybind.

I want it so the ColorRect will then accept any of the following inputs - not just the ones under gui_input - so I can then set whatever key was pressed as the new one for the given keybind, I have this working except it will only react to - and therefor set the keybind for - inputs under gui_input.

$ColorRect.gui_input.connect(
func(event): 
  if event.get_class() != "InputEventMouseMotion":
    keybinds[keybind_to_change] = event.keycode # or button index
    $ColorRect.visible = false
)

So I want to be able to do essentially the same thing - when the screen is up, get the next key/button pressed, save it somewhere, hide the screen - but for all inputs, not just ones under gui_input.

So I wanted to connect the built-in ‘_input’ function - since their isn’t an input signal, like gui_input - just like the one you can use if you attach a script to a Node, to code like the code above.

I know I could attach a script to the ColorRect to do this, but it seems bad to have a script which would be a handful of lines long if it could be handled like it is above.

Or as you said, which I didn’t think of, I could have the ColorRect ignore input and have the original script await input using its own ‘_input’ function, if this isn’t possible.

Thank you for the help, and I hope that was clearer.

I think you’re confusing the mental map of an input remapping feature with how you implement it.

In most cases, a Graphical User Interface (GUI) is meant to allow the user to:

  • Perform manual navigation
  • Manually initiate actions
  • Observe the state of an underlying system

UI elements – named Control in Godot – should generally not contain systems that are unrelated to the functionality of the GUI.

The ColorRect you’re using should (and does) act as an indicator which tells the user that the underlying system has gone into “input remapping” mode where a user input is expected. The underlying system, that contains input detection and remapping of inputs, exists somewhere else – it should not reside on the ColorRect indicator.

There’s no reason for you to use _gui_input() for any of this.

Yes. Because it’s not possible – and because of the reasons outlined above – you should use _input() in your “original script”. There’s not even a need to “ignore input” from the ColorRect. You just don’t listen to it, period.


Hopefully this makes sense to you. If you have more questions, let me know.

1 Like

Thank you!
I do think your right where I am just going about it the wrong way, so when I get back to work on this tomorrow I will change it so I don’t use the ColorRect’s input and instead use my original script to handle this.

Though while trying to solve this before making the original post I am surprised that either the base Node class or the Control class don’t have a signal for receiving any input like Controls do specifically for gui_input, especially since any node can have a script and therefor the _input() function so the functionality behind the scenes would already be there I would assume.

That’s because the input events received through _input() are not related to any one particular element – a key press is a key press. On the contrary, inputs received through _gui_input() vary depending on the state of the Control element. As such, you can imagine cases where only the input for a particular Control is relevant – I imagine that’s the reason for the gui_input-signal’s existence.