Error "Non-static function can only be called from an instance"

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

I’m trying to make a function that moves the camera as far as the mouse moves when middle clicking (like how it works in Inkscape for example). Within a Camera2D script I have this code:

func move_camera():
   position + InputEventMouseMotion.get_relative()

When writing this I get the error in the title. I was wondering how to fix this error, or if there is a better way to approach what I want to accomplish.

:bust_in_silhouette: Reply From: Lola


First off, you are not modifying anything with your current code. What you should do to set the new position is: position = position + ...
Doing what you do now (position + ...) simply calculates the value of this sum and discards it right away.

Regarding the movement logic, I see there is a misconception on what InputEventMouseMotion means here.
InputEventMouseMotion is an extension of InputEvent, which are data containers that encapsulate user input events. The name InputEventMouseMotion is one of a type and not of a global object.
To intercept and react to InputEvents you should use the builtin _unhandled_input function:

func _unhandled_input(event):
    if event is InputEventMouseMotion:
        print("mouse moved by ", event.relative, " pixels.")

You can see the InputEvent docs here.

Worked great. Thank you :slight_smile:

EliTheBeeli | 2021-10-23 15:47