0 votes

I am new to Godot (fluent in python) and I am trying to write my first game. It's a simple physics puzzle game in which you click on some objects and they disappear/get destroyed. After making my research I understood that RigidBody2D was the way to go since it has built in physics. However I can not detect the input on my objects. Following a tutorial, I added a custom item in Input Map: uitouch with left mouse button. I make the object pickable in the inspector then I connect the inputevent of my object to itself and write the following code but nothing happens: (I'm using Godot 3.2.1)

extends RigidBody2D

func ontest1inputevent(viewport: Node, event: InputEvent, shapeidx: int) -> void:
if event is InputEventMouseButton:
if event.is_pressed():
print("Object clicked")

Thank you in advance.
Note: The indentation is correct in my code

in Engine by (18 points)

Your code works fine for me. Have you added a CollisionShape2D to your RigidBody2D? Make it visible during testing under Debug > Visible Collision Shapes to ensure it's positioned correctly and your clicking inside the shape.

njmaster, thank you for your answer. Yes I have added a CollisionShape2D to my RigidBody2D. I followed your suggestion and made the collision shape visible. It is actually visible and I click inside of it. Still no luck.

Have you checked that the callback is connected correctly? Does it e.g. print a line on other events when your code looks like this:

func on_test1_input_event(viewport: Node, event: InputEvent, shapeidx: int) -> void:
    print("Any input event")
    if event is InputEventMouseButton:
        if event.is_pressed():
            print("Object clicked")

Maybe you can upload an example project with the problem?

Hello again njmaster. I followed your advice and nothing is being printed on the output. I would definitely try and upload the project. I just need to figure out how to do it.
Thank you again for your support and patience, I really appreciate it.

I have uploaded the project to Google Drive. You can download it here Thank you so much for your support. For your info, the second file is much easier to explore. Just run the level_template.tscn and you will see my problem.

1 Answer

+1 vote

In your LevelTemplate-scene you have to select the TextureRect-node and set its mouse_filter-property in the inspector to MOUSE_FILTER_IGNORE. Otherwise it will consume the input event before it reaches your player-character.

by (10,221 points)

Thank you soooo much! That worked like a charm, you are my hero.

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 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 webmaster@godotengine.org with your username.