Random beginner-question #12: Is it possible to pack a function into another function?

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

Hi everyone, my

func _process(delta):
	timer += delta

	and then some ifs

works wonderfully when clicking anywhere on the screen. Now I’d like it to react only within an Area2D, so I put all the ifs into a func _on_HoldButtonBase_input_event(viewport, event, shape_idx):. But then it doesn’t work right anymore (in my case, for some reason I have to move the mouse around within the CollosionShape2D to have everything work, but there are flaws if I hold still…).

Now my abstract thought is: What if I could put the whole func _process(delta): “into” the input_event-function? Shouldn’t then everything be fine?
Alas, I wouldn’t know how to achieve this. Any ideas?

:bust_in_silhouette: Reply From: jgodfrey

First, no, you can’t put the _process() function inside another function.

Assuming this question is related to the “timer chaining” question we worked on earlier, here’s one idea.

Why not simply connect the mouse_entered and mouse_exited signals of your Area2D. In those signal handlers, just set a simple boolean flag indicating whether the mouse is currently inside the Area2D. For example, mouse_over_button = true in the mouse_entered and ... = false in the mouse_exited.

Then, use that flag to skip the code you have in _process if the value is false?

As I mentioned in the other thread, there are lots of ways to do the same thing. While this might not be the cleanest overall (based on a better understanding of what you’re doing), it’s probably one of the easier ways to get from what you have to what you want…

Another simple way my beginner-mind couldn’t come up with yet. This does the trick! Thanks again, Jgodfrey!

pferft | 2020-09-02 14:49