+3 votes

I have a structure with Viewport:

https://s4.postimg.org/t1nl26d7x/viewport.png

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

in Engine by (58 points)

1 Answer

+4 votes

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.

by (1,556 points)

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.

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 :D

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

You save my life

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.