Click event inside sub-viewport

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By qazwsx
:warning: Old Version Published before Godot 3 was released.

I have a structure with Viewport:

I want to have object (like button) in viewport which allows me to click on it. How I can achieve this?
The events to not work for the Button here

:bust_in_silhouette: Reply From: mollusca

You can forward the input events from one of the Viewport’s parent nodes with the Viewport.input(InputEvent local_event) function. In the parent node:

onready var viewport = get_node("Viewport")

func _ready():
    set_process_input(true)

func _input(event):
    viewport.input(event)

You might also have to offset the position of mouse events if your ViewportSprite isn’t in the top left corner.

Very nice tip. In the past I set up a variable inside the sub-viewport, and had the parent viewport pass along the information I needed.

avencherus | 2017-11-30 10:01

I got it to work with a ViewportContainer:
set the ViewportContainer mouse settings to ignore
then forward the UNHANDLED input to the viewport

onready var viewport = get_node("Viewport")

func _ready():
    set_process_input(true)

func _unhandled_input(event):
    viewport.input(event)

I hope i prevent someone the 4 hours of desperately looking everywhere for a fix i spent :smiley:

Hasen | 2020-01-22 04:26

Thanks for this code snippet!!! It did save me! :slight_smile:

darkmarmot | 2021-01-06 19:59

You save my life

The_Moye | 2021-07-20 16:02